How to use the subtotal including taxes / vat in Magento when using promotion coupons

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 …

7 comments

  1. Hi Geoffrey ,

    I applied this fix everything seems to work when the order is placed in checkout it doesn’t take de discount and
    price for order change.

    Did you have this problem?

    Thanks.

  2. …or you could have just set up the promotion to work on orders over £100 minus the VAT.

    So you set up the promotion to to give the discount on baskets over £83.33 in this instance.

    Also, please don’t encourage people to edit the core code, you should have recommended replicating the file in to the local folder (so create
    app/code/local/mage/SalesRule/Method/validation.php <– that's the wrong path also by the way, I can only assume you actually mean app/code/core/Mage/SalesRule/Model/Validator.php).

    Perhaps you should educate yourself a little further with the product you are using before giving "advice" and throwing judgements around.

    I hope you didn't charge your client for the "days" you spent on this incredibly easy task.

  3. Hi, we’ve come across the same problem, and have tried using your solution in Magento 1.9, but it doesn’t work. Do thou have any ideas for this? Many thanks in advance…

  4. Hi! Can you please update your blog? I am in need of the following:1. A full condo tour including (but not limited to) Fridge, Bedroom, Living Area, Workspace2. A walk about of your neighbourhood including places to eat dessert. 3. A recap of all of the fun things you’re doing in the big CITY! xoxoxSee you at ChristmasB

Leave a Reply

Your email address will not be published. Required fields are marked *