Discount on Amount in Cart does not work properly

I try to add simple discount: 7% if total sum is grater than $60
At admin/commerce/config/taxes/types i have new type: discount
At admin/commerce/config/taxes i created new tax rate Discount 7% (component)
In this component i created condition “Data comparsion” and seleced option “commerce-line-item:order:commerce-order-total:amount”
And lets test it: order for total sum $58.88: http://i.imgur.com/FIfVe.png Looks ok, no discount.
But when i went back to “view cart” and click on checkout i saw the same product list with discount 7% http://i.imgur.com/QDghA.png It happens randomly. Sometimes the discount appears sometimes does not appear. I did not change anything in rules settings.

Another strange thing: total sum is $67.88 http://i.imgur.com/mp7AI.png but in debugging variable commerce_order_total this value is set to 63.13 so after discount. In previous case (58.88) this value was set to 58.88 so before discount calculation.

I was reading http://www.drupalcommerce.org/discussions/1123/discount-amount-cart but this didn’t help me.

Discount rule settings: http://i.imgur.com/q2ONU.png http://i.imgur.com/Au0qj.png

List of contrib modules: http://i.imgur.com/5Sp4h.png
Drupal 7.14
Commerce 7.x-1.3

Can anybody help me with this problem?

Posted: Sep 12, 2012


memil on September 14, 2012

I have very very similar problem. So I tried to use VAT hack to calculate discounts. I would like to make several type discounts: if the subtotal of the cart is greater than 200 EUR and lower than 400 EUR, then the discount would be 10%. If the subtotal of the cart is greater than 400 EUR and lower than 600 EUR then the discount is 20% and so on... I created rules like grzegorz.bartman, but it doesn't work correctly. It calculates discount even the subtotal is lower than that price what I typed in. Also it is very important, that the VAT should calculate from the discounted price and not from the subtotal price. Actually the VAT is calculated from the subtotal price (which is price without any discounts), and not from the discounted price. Can anyone help to us, how to create discounts in this way?

grzegorz.bartman on September 16, 2012

I tried to use hook_commerce_tax_type_calculate_rates and i found this:
My case:
1. Cart: Total $63.38 http://imgur.com/BtV5W
2. Checkout: http://i.imgur.com/jGVVn.png - Discount 7% work OK (no title, but this is no big problem)
3. Go back to Cart and remove one product for $4.50 http://i.imgur.com/gfTno.png
4. Discount 7% is still in summary http://i.imgur.com/qcdAB.png
5. commerce_cart_order_load($user->uid) object still has information about discount http://i.imgur.com/IS4BE.png
6. Now when i refresh checkout page discount disappear http://i.imgur.com/9ZMHz.png and commerce_cart_order_load($user->uid) object works ok http://i.imgur.com/BSfG9.png

So problem is in function commerce_cart_order_load - this function do not refresh commerce_order_total attribute when user change something in cart and click on Checkout button.

Now i used custom code and custom module: http://pastebin.com/75JCuR7f

Maybe the same problem is when i try use rules?

memil on September 25, 2012

Wow! It sounds great. Do you know, which is the argument of subtotal? Because the discount is calculated from the "Order total", but I need that the discount be calculated from subtotal. (I need it because I have a VAT calculation after the discount)

grzegorz.bartman on September 30, 2012

I don't have VAT so i can't help you.

My problem with discounts still occurs. Evidently there is a problem with the cache when user change values in cart and click on button Checkout. Order total is cached and has old value and discount value is not changing.

jsimonis on October 18, 2012

I added the code to my commerce_discount.rules.inc file; however, I don't see the condition in my Rules. I even uninstalled the module, reinstalled it, and cleared the cache.

nicodv on November 5, 2013

The module solved my problem! I spent my afternoon hitting my head agains all walls figuring out why the tax rates would not do what I wanted them to do... ouch!

Thanks a lot @grzegorz.bartman

jsimonis on October 16, 2012

I was looking at the solution at pastebin, but wasn't certain what to do with it since I don't have a file named that. I have something similar - commerce_discount.rules.inc - but it's not the same file.

jsimonis on October 18, 2012

For others who may look at this and need to know:

I added the code to my commerce_discount.rules.inc file at the bottom. There is one line right at the top that has to be edited:

function commerce_discounts_rules_condition_info() {

It too has the extra "s" at the end of discount. Fix that and then subtotal becomes an option inside Rules.