Percentage discounts for specific roles are applied twice during checkout / review (possibly other types of discounts as well but we don't use them).
To fix this issue I patched uc_discounts to reload the line items from the database in the uc_discount implementation of hook_order -> save. If this is not done, previously applied discount lines aren't actually removed from the order if uc_order -> save is called twice on the same page using the same order object as $arg1. The reason they don't have an ID that can be used to delete them unless they are loaded from the database after creation.
There is also a small bug when existing line items are loaded from the database. uc_order_load_line_items apparently expects an order object rather than an order id as a parameter.
Could the attached patch please be applied to the uc_discounts_alt code?
| Comment | File | Size | Author |
|---|---|---|---|
| fix.reload_line_items_after_save.patch | 2.04 KB | rhmtts |
Comments
Comment #1
jrust commentedI committed the part that called uc_order_load_line_items() but could not reproduce the issue of duplicate discounts. Created a discount that applied only to a certain role and went through checkout without seeing any duplication. Can you provide your exact steps to reproduce?
Comment #2
rhmtts commentedUsing the latest dev version (the one from 2011-07-21) I still get discounts added twice on the review page. Steps taken are:
- create a discount percentage for a specific role
- assign the specific role to a customer
- log in as the customer with the discount role
- purchase something
- fill in the form on the checkout page
- click the review button
Voila, the discount is applied twice.
The reason is the latest dev version does not call uc_order_load_line_items at the very end (as happens in the patch).
This call is necessary to get the line_item_id for every line item. Without a line_item_id, previously created discount line items can't be deleted before new discount line items are created anew. This becomes a problem when hook_order is called more than once on the same order object, which does happen (and which shouldn't cause a problem - implementations of this hook simply need to make sure they function correctly, IMHO).
I've re-patched the code to make it work again in the site I'm working on, but I think this is in fact a bug that needs to be fixed in the UC Discounts Alt module itself.
Comment #3
karangarske commentedI also needed the " Reload order from database to make sure all line items have IDs" part of the patch to stop the duplication of the discounts.