I recently have had to work on the opensource e-commerce CMS « Magento » through work. It was my first time using the application and had no knowledge of the inner workings of it all, so it was a real delight when I was given the easy task of adding promotion coupons / vouchers to the website.
All I had to do was to create a coupon that would reduce the grand total by a given percentage when the total of the basket was bigger than £100. Easy right? Well so I thought until I came to find out that magento, in all it’s V 1.3.2.3 glory, applies the promotion rule (having to be higher than £100) to the subtotal excluding taxes / vat rather than the opposite way!
Because of that, although a user may have had a basket worth £110, the promotion script would only see the price without taxes, in other words around £85 and therefore would not apply the discount. Because all prices throughout the website are displayed including VAT, this would have definitely been really confusing for visitors.
After spending countless hours first looking in vain for a fix on google, then spending days going through the horribly scattered pile of horse shit that is magento’s code, I finally came up with a fix! And because I’m nice and don’t want to put someone through what I went through, I will share it here with the rest of the internet.
It is in itself very straight forward. Locate the « validation.php » file in « app/code/core/mage/SalesRule/Method » and open it. Look for the following code:
$quote = $item->getQuote();
if ($item instanceof Mage_Sales_Model_Quote_Address_Item) {
$address = $item->getAddress();
} elseif ($quote->isVirtual()) {
$address = $quote->getBillingAddress();
} else {
$address = $quote->getShippingAddress();
}
Right after the closing bracket, simply add this little line of code;
$address->setBaseSubtotal(number_format($address->getSubtotal() + $address->getTaxAmount() - $address->getShippingTaxAmount(), 2));
All this does is that it add the total tax amount (while not including shipping tax tax) to the base_subtotal variable within the $address object which is the variable used by the promotion code. This change will however not change to front end value of the subtotal on your website so your basket will still display the correct subtotal without tax !
Let me know if you are having any problems or if you have any comment about my fix.
Till next time …