0

I need to add a new event to rules triggered by the successful completion of an order. The action is to grant a user a role which will expire in a fixed duration.

I've tried following the Rules documentation and adding a module with a *.rules.events.yml file. But modifying it to use the order_complete event which I got from this tutorial.

Unfortunately, even though it DID add an option to the events drop down, it doesn't trigger an action.

I've tried a grep through the commerce code for order_complete but it doesn't yield anything.

The action I've set to the rule is just to print a message.

3
  • 1
    That tutorial does not hook into the order_complete event. That is just the name of your custom event listener service (it could be anything). It listens to the commerce_order.place.post_transition event. Commented Jun 27, 2017 at 21:45
  • Neograph734, Yes you are right, apparently the order_complete event does not exist. Following the rules documentation and then changing the event to commerce_order.place.post_transition worked! Commented Jun 27, 2017 at 21:54
  • Nice, good to know you have figured this out. I might need it later as well :-) Commented Jun 27, 2017 at 22:05

2 Answers 2

1

After trying out combinations and with the helpful comments, the solution was to follow the Rules documentation but change the event to commerce_order.place.post_transition which I got from the tutorial.

The final code was to create a MODULENAME.rules.events.yml file and add the following code:

commerce_order.place.post_transition:
  label: 'Order has been completed'
  category: 'Order'

This created the option in the dropdown and allowed me to create the rule that triggered the action.

4
  • Your question contains (at the end) "The action I've set to the rule is just to print a message" ... I cannot believe that just creating the yml file makes such message show up (eg: you didn't even specifiy which message should be shown). Please edit your answer to also add that part of it. If you do please let me know (I might +1 it then) ... Commented Jul 20, 2017 at 13:14
  • Hi, the yml file jusr creates an event option in the rules create ui. But you have to make the rule that actually uses it. And the message content is irrelevant since I just used the 'Show a message' action when I created the rule. My solution is not a full rules component, just an event. Commented Jul 20, 2017 at 18:37
  • merci for your prior comment. That part I understand, but your answer here also includes "This created the option in the dropdown and created the action". Sorry to say, but I doubt the "*... created the action"-part of it (even though it may be just an irrelevant message content). In other words: your answer seems to me like either incomplete or misleading. Commented Jul 20, 2017 at 18:43
  • Good point. I've updated it. Commented Jul 20, 2017 at 18:44
0

Interesting, this is the first time I've heard of someone trying to use Rules with Commerce 2 ... definitely not a lot of documentation on that. Likely you need to rebuild your cache and confirm the event is firing by using a very simple (not prone to breaking) action and/or enabling the debugging mode of rules (to see if the rule processed as requested).

Likely, your event is firing just fine but your action, which you provided no information on, is the culprit.

2
  • Hi joshmiller, yeah documentation is sparse at best, just bits and pieces here and there. So the action I've added is just to print a message, which it doesn't do. Commented Jun 27, 2017 at 21:52
  • Yeah, you're going to have to dig a little deeper to get at the root problem. Printing messages, especially in checkout, can be a dicey business ... lots of screen loads and totally dependent on payment gateway selections ... better to have rules fire off a log message or something similar to see if the event fires. Also, don't forget to look at a debug mode, which came in the D7 version.
    – joshmiller
    Commented Jun 28, 2017 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.