Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have coupon that validate if a minimum order amount is reached. I used a new field and a rule, it works ok in checkout, but after redeem the coupon, the user can change product quantity (in other browser tab) and the validation doesn't be called again. I tried to delete coupons in order when line items changed but the order doesn't have coupons referenced in that instance (Event: Line items add/update/delete). Can you tell me how we can ensure the cart isn't changed after coupon is added or delete coupons if cart changes.
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#6 | commerce_coupon-validate-on-refresh-1831338-6.patch | 3.35 KB | justanothermark |
Comments
Comment #1
bmcclure CreditAttribution: bmcclure commentedJust chiming in to mention that I have this same issue. Created a field for minimum order amount and a validation rule that works great initially. But after you start checkout, you can go back to the cart and remove products or change quantities until the order amount goes lower than the minimum, and the coupon still applies.
It seems that validation should fire for all existing coupons when line items are changed as the OP indicates.
Comment #2
alienzed CreditAttribution: alienzed commentedsame issue, this seems like a deal breaker.
subscribing
Comment #3
pcambraThis is not simple business here, you probably need to implement that yourself using hook_commerce_order_refresh and invoking the rule event validation again there... hopefully in the 2.x version we can properly fix this with commerce discount.
If someone wants to take a look there and do a backport we can bring this back to live.
Comment #4
alienzed CreditAttribution: alienzed commentedYou can achieve this with rules, it's pretty straightforward actually.
just create a rule that fires whenever a line item gets updated or removed and call the coupon validation again...
(should be built in, but we're developers after all...)
Comment #5
pcambraPatches are welcome
Comment #6
justanothermark CreditAttribution: justanothermark commentedPatch attached to revalidate all coupons on hook_commerce_cart_order_refresh and remove invalid ones.
The patch uses a new function commerce_coupon_code_is_still_valid() which is identical to commerce_coupon_code_is_valid() but without the check for existing coupons on the order (as we're not adding a new one and all codes we're checking should be on the order). This probably needs tidying up later to reduce duplication but one step at a time.
Comment #7
justanothermark CreditAttribution: justanothermark commentedOops, forgot to update status.
Comment #8
pcambraWow, amazing effort, thanks! Moving this to task as it seems something we would need.
So, reviewing the patch, it's a great start and the hook used seems correct, even though we could evaluate the possibility of using hook_commerce_order_presave instead.
Removing seems fine, but what the patch doesn't do is update the value of the coupon, what is needed here is IMHO what commerce discounts do in the refresh hook, first it loops all the line items and remove all the price components related with discounts, then the line items and then reattaches the discounts related with the order.
Comment #9
loze CreditAttribution: loze commentedConfirming that the patch in #6 works. Thanks.