It currently does not seem to be possible to hook into the coupon removal action, e.g. to remove a free product that was granted with the coupon. Anything planned here, or is there a different approach recommended?

Comments

pcambra’s picture

Do you mean that when manually removing a coupon through the UI it should be an event/hook or both to be able to react and remove also whatever extra stuff was granted with the coupon?

pvhee’s picture

Exactly. For example, to allow to take off a free product granted via a previously applied coupon, or something of that sort.

pcambra’s picture

Category: feature » task

Makes sense, patches are welcome :)

We could use rules_invoke_all to have both a hook and a rules event.

pvhee’s picture

Status: Active » Needs review
StatusFileSize
new1.91 KB

Here you go, a first try :)

I am using rules_invoke_all, I created the rules event, and also documented the hook in the api.php. Tested with some dummy rule that removes all products from the cart after the coupon is removed.

My use case really is to remove a product that was added through a coupon to the cart (in the style of "buy 3 get 1 free" coupon), but for this I need to sort out #1267156: Create a "Remove single item from cart" action or go with code for the free product removal.

pcambra’s picture

Status: Needs review » Needs work

Looks good, but if you want to expose it both as a hook and an event, you need to use rules_invoke_all instead of rules_invoke_event, the later would just expose an event and you're documenting it as a hook as well.

pvhee’s picture

Status: Needs work » Needs review
StatusFileSize
new1.91 KB

Updated patch attached, thanks for the review!

pcambra’s picture

Status: Needs review » Needs work

Ok, took a closer look to this and I have second thoughts.

You could use the default entity delete hook/rules event for this the way that this is implemented, I don't see a difference between this event and using "After deleting a commerce coupon" from Entity API.

What needs to happen here is to execute this event *just* when the coupon is deleted from the form that the customer sees.

I guess you could accomplish this using the event above and the user id, but it might not be the case if someone is managing the order on behalf of the customer (i.e. customer service)

Scott Robertson’s picture

Issue summary: View changes
Status: Needs work » Needs review

I think there may have been some confusion on what this event would be for. Based on pvhee's patch, I believe he wants an event to be fired when a customer manually removes a coupon from their order during checkout, not when you delete a coupon entity through the admin interface.

If that's the case, then I believe the patch he provided would still be useful.