Little-known discount issue in Magento and how to fix it

Table of Content

Little-known discount issue in Magento and how to fix it
Posted in: Magento 2 Guides

Hello, my name is Pavel, and the most of my work with Magento is connected with discounts. Sounds like a confession! I can say I know a lot about them, but our sophisticated customers never let me rest and ask more and more questions about this topic.

A couple of days ago I’ve encountered a problem with discounts in Magento, and today I wanted to explain this issue to you, also giving tips on fixing it fast.

How we found the issue

All of a sudden we faced some rather unexpected behavior of Amasty Special Promotions Pro extension, which also caused issues for Auto Add Promo Items. In short, discounts were calculated all wrong.

Since these two extensions are very reliable, and all of our updates are carefully tested, I became very concerned with the issue and started investigating right away.

I found out that it’s not a compatibility problem, as the issue was reproduced on a clean local Magento installation with only two Amasty extensions.

Later it came out that the issue hides inside Magento logic, and in fact it can spoil any extension that works with discount.

The rundown of the issue

To show a discount, Magento uses a cycle, which runs through all the items in the cart and applies all the discount rules to each and every item.

Different items initiate rules for each product, while several pieces of the same product initiate the rules collection only once.

The issue was hidden in the "Maximum Qty Discount is Applied To" field. Here’s a simple example.

We want to give 50% off for all the items, plus a 10% coupon for two items as well. The customer bought 10 beers, $10 per beer, $100 total.

Let’s do some maths: 50% from $100 is $50, 10% for two items ($5+$5)*10%=$1. Please remember that a beer is not $10, but $5, because we have already reduced the price.

Now, we calculated the total discount, which is $51. Let’s see the number Magento calculates.

What is happening in reality

Entry data:

The first rule: 50% off for the whole cart

The second rule: 10% off the cart, and Maximum Qty Discount is Applied To = 2

Magento calculates discounts with the following formula:

[php]$discountAmount   = ($qty * $itemPrice - $item->getDiscountAmount() ) * $_rulePct;[/php]

In this case qty is the number of products, if it’s not restricted by "Maximum Qty Discount is Applied To" field, otherwise it’s "Maximum Qty Discount is Applied To" field value.

$item->getDiscountAmount() is the discount, which has been already applied to the product.

Here’s how Magento calculates it:

The first rule: ($10*10 - 0) * 50% = $50

The second rule: ($10*2 – 50$) * 10% = -$3

In this situation Magento tries to take the applied discount into account, but its algorithm has a serious issue when counting applied discounts for several pieces of a product, and we suddenly get a -$3 discount when applying the second rule.

And the total discount appears to be $47. Yes, you’re right, it’s even less than 50% off. Which is, of course, totally wrong.

But that’s not all. If we step down a bit, we’ll see a discount check, which is performed to ensure that the store won’t give a discount higher than the item price.

[php]$discountAmount     = min($itemDiscountAmount + $discountAmount, $itemPrice * $qty);[/php]

But we remember that $qty was restricted by "Maximum Qty Discount is Applied To", and its value is 2 in this case.

Hence, we have the following when the second rule is applied.

$discountAmount     = min( 50 + (-3), 10*2)

The answer is $20. Tada!

Magento discounts issue description

Where you can find this issue

Looks like this bug is at least five years old, the earliest version that I checked was ver - Added Feb 19, 2010. It seems to be working fine in the previous versions.

Also this bug can be reproduced on Magento 2 version (as of 23rd of October, 2015).

The temporary fix of the issue

To fix this issue, make sure that all the rules with "Maximum Qty Discount is Applied To" field, different from zero, should be applied in the very beginning, use the Priority field for that.

Magento discounts issue fix

If you have any questions about this topic, I’d be happy to discuss them in comments.

October 26, 2015
December 9, 2015
October 6, 2015
Ashish Nayyar
October 28, 2015
Haha, now i know that someone else already faced the similar issue. Somehow we both ended up using the same trick. Correct me if i am wrong but in the latest stable version doesn't have this issue is it?
Ksenia Dobreva
October 28, 2015
Hey Ashish, which version are you using? I mean, we have tested and Magento 2, and it was still there.
September 23, 2019
This is an old article but definitely helped me! I have free shipping on all my subscription boxes but wanted to include a 25% off on your 1st box and it was saying it was applied but it wasn't being reflected in the total. Changed the priority to 1 and BOOM, problem fixed. Thanks!!
Polina Litreyeva
September 27, 2019
Hello, Sarah! Thanks for your feedback. We are glad that our article helped you to fix this issue!
Jubilee Shoals
December 31, 2019
Well done to the author. A great article. May I ask you to contact us so that we can talk?
Leave your comment

Your email address will not be published

This blog was created with Amasty Blog Pro

This blog was created with Amasty Blog Pro