All posts in the “code” category

Magento Upgrade Issues with old shipping and payment methods

I’ve noticed a few times when upgrading to to newer versions of Magento we get a few errors with delivery and payment methods that have been previously installed.. On a recent upgrade the default ‘protx’ and ‘amazon’ payment gateways that were bundled with magento < 1.3 were not been included in later releases and threw up a few errors in the customer area of the frontend and admin…

To remove the old rules, you need to get access to SQL and input these lines which should remove old rules… Always backup your database before doing this, just in case

This is for various shipping rules I had on the install..

[code]
DELETE FROM `core_config_data` WHERE path LIKE 'carriers/controlshipping%'
OR path LIKE 'carriers/export3j%'
OR path LIKE 'carriers/colissimo%'
OR path LIKE 'carriers/byweight%'
OR path LIKE 'carriers/byitem%'
OR path LIKE 'carriers/chronopost%'
OR path LIKE 'carriers/byprice%'
OR path LIKE 'carriers/australiapost%'
[/code]

The below is to remove void payment methods…

[code]
DELETE FROM `core_config_data` WHERE path LIKE 'payment/protx_standard%'
OR path LIKE 'payment/amazonpayments_asp%'
OR path LIKE 'payment/amazonpayments_cba%'
[/code]

more readable method to show print_r output

We’re working on a lot of projects at the moment that require tons of data being shifted around, usually asynchronously. During the development stage its sometimes useful to print all your available data to the screen so you can see if anything is being sent over incorrectly (or not at all). The usual print_r function dumps the output of an array to the screen in a giant block with no linebreaks or intentation, making it difficult to scan read.

After a quick browse i came across a few neat functions which will allow a more human readable version to be printed to the screen, and after some small modifications to the syntax, we arrived at this…

function print_r_html($data)
{
	echo '< pre >' . print_r($data, TRUE) . '< / pre >';
}

I had to put some spaces in my pre tags because the syntax highlighter didnt like it, so make sure you take those out

Slip this into your common/misc functions file (we use a functions file specifically for debugging during development so we can activate/deactivate it for use in a live environment) and then simply call it with

print_r_nice($data)

Some examples i found had checks in to make sure the variable being passed was an array but thats almost always redundant for our purposes, so out it goes. In case anyone was wondering the ‘TRUE’ just prevents the output of print_r from being returned before we’ve had a chance to do anything with it.

Calculating VAT automatically with jquery (and displayng it)

We were recently working on a small-scale ecommerce site where our client wanted to be able to show prices both including and excluding VAT. Due to shipping rules and the methods we were using to calculate the cart totals, it would have been impractical to have both prices stored in the product database. So instead we turned to the ever reliable jquery javascript library.

I wrote the following script to calculate and display the including VAT price using the already available excluding VAT price as the input. The jquery code required was simply…

$(document).ready(function(){

	$("div.prod").each(function(i)
	{
		var calcvat =  Number(($(this).find("div.excvat").html())* 1.15).toFixed(2);
		$(this).find('div.incvat').html(calcvat);
	});

});

Because we had pages where multiple products were visible (e.g. cateogry pages), it wasnt as straightforward as simply attaching the calculation event to a specific div, so instead the function had to be written in a way that it would traverse the document and find all instances of the class “prod” and perform the calculation on the “excvat” span within each of them. The result is then fed into the empty span “incvat” within each product box. So the HTML is pretty minimal;

5000

Our shopping cart / checkout page calculates vat based on the cart totals, so we only really needed this function on category and product pages.

Only a few lines of code, but took a good 30 minutes to get it working the way I wanted it to, and to make sure it wasn’t producing a crazy amount of decimal points.

You can find us on Twitter & Facebook