I might be picky, but I think this is a bug.

If I create a "minimum quantity" discount for a product (or all products) that gives 25% off. (lets say its 10 or more items)...

I go and add 10 of that product to my cart, but I also add 5 of another product. When I check out, since one of the products in my cart qualifies for the discount, it gives a 25% discount to the WHOLE ORDER, not just the product that has 10 or more items. I would expect that the discount would only be applied to the product that qualified for the discount, not the whole order.

Nice module by the way. :)

Comments

ryangroe’s picture

StatusFileSize
new8.63 KB

You're definitely not just being picky, I think this is a bug too. I don't have time to fully test this but I think I know what's wrong so I attached an updated uc_discounts.module (zipped) for you to test.

swill’s picture

StatusFileSize
new60.4 KB
new31.77 KB
new114.96 KB

That definitely did not work. Here are the resulting screen shots after using that version of the module.

ryangroe’s picture

I'll look into this more over the weekend.

swill’s picture

thanks :)

if you need me to test stuff just send it my way and i will get you feedback...

swill’s picture

i have just downloaded Ubercart Discounts (Alternative) 6.x-1.0-beta7.

i am confirming this is still a bug.

ryangroe’s picture

Sorry, swill. I am way behind on bug fixes. I will get this working but not until this weekend.

swill’s picture

no problem, it is not a show stopper for me. :) thanks again for all of your hard work...

chrisschaub’s picture

Ryangroe, I need to get this bug fixed pretty quickly, and so I'm happy to take a crack at it and submit a patch. Do you have an idea of where the problem exists? You submitted a zip, but some pointers to give some direction would be great. Not trying to be pushy, just trying to help!

Thanks.

chrisschaub’s picture

Ok, here's my initial stab at this issue. You need to get the total of the qualify products for the discount. The qty discount was just applying to the whole order, not the products affected by the discount. Just replace the entire contents of the appropriate "case" statement to test it.

case DISCOUNT_TYPE_PERCENTAGE_OFF:

                                //Always apply once
                                $discount->times_applied = 1;

                                // Get qualifying products -- ignore "all products" selection
                                $discount_product_ids = get_product_ids_for_discount($discount->discount_id, TRUE);

                                // Do we have any products
                                if (count($discount_product_ids) > 0) {
                                  // Create array of product objects related to this discount
                                  $products = array();
                                  foreach ($discount_product_ids as $product_id) {
                                    $products = array_merge($products, $order_product_id_product_array_map[$product_id]);
                                  }
                                  $discounted_products_amount = 0;
                                  foreach ($products as $product) {
                                    $discounted_products_amount += $product->price * $product->qty;
                                  }
                                  $discount->amount = round($discounted_products_amount * $discount->discount_amount, 2);

                                // Discount the subtotal so far
                                } else {
                                  $discount->amount = round(max($order_subtotal - $total_discount_amount, 0) * $discount->discount_amount, 2);
                                }
                                break;

This seems to work for me regarding percentage discounts. I'd need to check to see if other places need this fix. Any help and feedback is greatly apprectiated.

*** UPDATED: I added a round() function to make sure the rounding happens consistently for percentage discounts, otherwise the total after discounts might not match the sum of the discounted amounts. Use at your own peril, not thoroughly tested!

chrisschaub’s picture

Hey, if this looks like an ok approach, I'd be happy to roll a patch against beta 10. If it would help. Not sure if this is the best way though.

Thanks.

chrisschaub’s picture

StatusFileSize
new2.21 KB

Ok, here's a patch against beta10.

ryangroe’s picture

Looks good. I am working on a bunch of changes, so I am going to use the same product object gathering code for both "free items" and "percent off". The next step after I fix this bug is to determine how to not discount an item more than 100% over multiple discounts. I will release a beta 11 tonight with a fix for your problem in a couple hours.

chrisschaub’s picture

Thanks. Any way to slide in my "max" quantity items patch? Thanks for your hard work, I'll test it when it's up. Let me know if I can help in any other way.

ryangroe’s picture

I think I need to keep track of the amount each item is discounted. So that will probably supersede the patch you sent me because I need to fix your problem without breaking the bug here: http://drupal.org/node/384844.

chrisschaub’s picture

I think my patch handles that, fixes the combo problem and multiple discounts. The percentage code was always discounting the total, not the individual products or groupings. It needs to discount by discount type. Well, hopefully what I posted is of some help.

ryangroe’s picture

I think your path certainly works well enough for now. I will apply it and release a new version. The issue is making sure no product is discounted more than 100%. To do that we need to keep track of how much each product has been discounted. The value in $total_discount_amount is too coarse to tell us this. We instead need a map of product IDs to the amount it has been discounted. Then we can limited the amount each product is discounted to 100% of the product's subtotal (quantity * price).

ryangroe’s picture

Assigned: Unassigned » ryangroe
Status: Active » Needs review

OK, try beta 11

ryangroe’s picture

there was a problem with beta 11. Please use beta 12.

chrisschaub’s picture

Hmmm, this patch as implemented (a bit diff from my original) is causing the discounts to not calculate correctly. To test, create percentage off discounts for two different terms assigned to two different products. The discount amounts for the terms are not correct, wildly off. Not sure why.

UPDATED: My original patch got all possible products for the discount code and merged them with the products in the current discount object. This version of the code doesn't do that correctly. It also doesn't fetch any products for some reason. Take a look at my original patch to see the difference.

chrisschaub’s picture

Ok, the problem is this line in the current code returns nothing and it should (used to) ...

$discount_product_ids = get_product_ids_for_discount($discount->discount_id, TRUE);

Using the object function instead works, returns products for the discount ....

$discount_product_ids = get_product_ids_for_discount_object($discount, TRUE);

Somehow, I think get_product_ids_for_discount got screwed up along the way, doesn't return any products.

BTW, I don't see any problem with "or the case where the cart consists only of the products included in this discount." The discount works just fine if there are products of only one type.

ryangroe’s picture

Sorry, I've been working too quickly without enough testing. The function get_product_ids_for_discount() doesn't work for discounts that filter on terms. So it should only be used in the insert/edit form. I have made the correct above (see beta 16). Thank you for your help.

ezra-g’s picture

Status: Needs review » Fixed

This has been committed, so I'm marking it as fixed ;).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

zahor’s picture

I don't know if this was supposed to be fixed, but it still does not work. The discount is applied to the entire order instead of just that product.

rich.yumul’s picture

subscribing

loparr’s picture

I came across this bug as well. What can be done with it?
After some testing it seems that if you want to get separate discounts for separate products, you have to create separate discount for EVERY product.