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.
Hi,
Would be good to have a built in rule that prevents a coupon being applied to an order if there's already one applied.
It is possible to do this with a rule, so I have included it here for anyone else that needs this functionality.
{ "rules_check_for_existing_coupon" : {
"LABEL" : "Check for existing coupon",
"PLUGIN" : "reaction rule",
"TAGS" : [ "coupon" ],
"REQUIRES" : [ "php", "rules", "commerce_coupon" ],
"ON" : [ "commerce_coupon_validate" ],
"IF" : [
{ "php_eval" : { "code" : "$line_items = $commerce_order-\u003ecommerce_line_items['und'];\r\nforeach ($line_items as $key =\u003e $line_item){\r\n$line = commerce_line_item_load($line_item['line_item_id']);\r\nif ($line-\u003etype == 'coupon') return TRUE;\r\n}\r\nreturn FALSE;" } }
],
"DO" : [
{ "drupal_message" : {
"message" : "Sorry, you can only use one voucher per order.",
"type" : "warning"
}
},
{ "commerce_coupon_action_is_invalid_coupon" : [] }
]
}
}
Comments
Comment #1
pcambraI like this, as for today we have a fixed code to check if a coupon has been already redeemed in the context of an order:
It may be a good idea not to force this and use a rule.
Comment #2
pcambraSorry, I misunderstood a little what you were doing here, the correct API function call to check if a coupon has been applied to an order is commerce_coupon_get_coupons_by_order, I've made some work on this in here (including a rule) #1538158: Limit number of coupons applied to the order.
Let's mark this as duplicate and keep the conversation in the other issue.