I am struggling with the following problem that involves Rules and Commerce.
SUMMARY:
User will register for a course [Content Type: Course] and pay registration fee using Commerce module (among others). At the completion of the transaction, Rules will trigger an email to the user that includes:
* Product information - product = registration
(name of event user registered for and the registration price)
* Details of the event (fields on the Content Type = Course)
* Personal information about user (Name and salutation = "Dear Doctor Smith:")
PROBLEM:
I'm able to access Product Information if I use a rule triggered by event = "Completing the checkout process"
I'm able to access Event and User information if I use a rule triggered by = "After updating an existing registration"
But, I can't access all this information using just one event.
QUESTIONS:
How do I include "product," "event," and " user (registrant)" information in a single custom email triggered by rules?
* Will this require the use of components?
* Can it be done with "Fetch entity by ID"?
- I've spent most of my time trying to make
it work with Fetch entity by ID.
* Any suggestions that might at least point me in the right direction?
Comments
Try a condition of "Entity has field"
Hey Tim,
Not sure if this will work since I haven't spent much time with Commerce + Rules, but I have used Rules pretty extensively.
When I need to bring in information from other entities that aren't directly related to the action, I use the "Entity has field" condition to access that data. Also, I don't know how you're connecting everything, but Rules plays nicely with Entity Reference as well, which allows you to access all kinds of additional data from the referenced entity.
EDIT: Upon looking at this further and doing a little testing, you might need to use a component. I'm guessing Commerce uses Entities whereas the event is a Node?
Good luck!
Thanks
Thanks for the advice. I've also heard that the "Entity has field" condition might help me, but I haven't had much luck using that. I've tried.
I'm also thinking that I may need to use components - but, I have yet to figure out how to use them for this problem. I've made a little progress through using "Fetch Entity by ID", but this is where I"m stuck now
I have the following token available to me in one entity and want to use it to Fetch another entity.
[item-fetched:commerce_display_path] = registration/5153
"5153" is the registration ID I need that would give me access to the registration I'm trying to bring into focus. However, rules is expecting data of type "integer."
How do I convert item-fetched:commerce_display_path into an integer representing the registration ID - or am I going about this in the wrong way?
(NOTE: It's happy hour for the local Drupal User Group tonight, I'll try to get someone to help me and post the results).
using a rule set
I'm gong from memory but I believe you can have components that are rule sets - so you can set a condition and then list your actions/rules
I've also used entity has field and then that field has a value
I'm also wondering what you are trying to do --
on one of my sites, a 90-day email notification is sent out to the registered user upon completion of certain steps in a particular course - we set up user roles that are updated during certain steps in the process -- the logic is as follows:
-load user id
-send an email (recipient is the loaded user id)
-populate content field "field-email-notify-date"
-add user role
Also, the email is addressed with a token: [user_loaded:content-profile-profile-first_name] (this is a ubercart site but you should be able to reference the profile for drupal 7 commerce).
also, Drupalize.me has a whole section on rules that is free:
http://drupalize.me/series/learning-rules-framework-series
Yes, you can accomplish it with components
What I struggled with was the lack of being able to have the right tokens exposed during a triggered event.
For example, I wanted to send a custom email when a Commerce order was updated or completed. I specifically wanted to expose tokens from the customer profile, however since the event trigger was "After updating an existing commerce order", the only exposed tokens available were for the order and order-unchanged. I needed more, like the first and last name of the customer.
What you need to do, is create a component "rule". (if you need more complexity, feel free to do a rule set, or you can probably get away with an action set).
Create the component rule and add the two data types you need. In my case I needed:
- Commerce Customer Profile
- Commerce Order
Inside the component rule you can set the condition, or you can use the condition in the main event triggering rule. This shouldn't matter. What does matter, is that your email message MUST be setup as the action on the COMPONENT rule because this is the ONLY place that will have access to both token sets. The main event triggered rule will not.
Once the component rule is setup to your liking, create your normal trigger rule. When you setup the action on the triggering rule, set it to the newly created component rule (Scroll to the bottom of the list). Here's where it gets very confusing and very specific...
When you create an action on the regular rule which is pointing to the component rule, it needs to know which data selectors to use. In my case, since I was triggering on an order update, it was able to find the right data selector for Commerce Order. It did NOT however, find the right data selector for Commerce Customer Profile and there was no way to select it normally through the dropdown. This is the whole reason we need to set it up as a component... The profile isn't being exposed on the order event.
Instead, what you have to do is click the "Switch to the direct input mode" button. From here, you need to use the physical ID number of a customer profile. Any customers profile ID should work: "Specify an identifier of a commerce Customer profile." I was able to get away with using 1 / superadmin to get mine to work. In other words, since it can't find the data selector based off the trigger event, you have to tell it an ID number of the entity that was setup in the component rule, so it knows what to look at to form the data.
So, regular rule with an action that points to a component rule. Setting up that action needs to have the customer profile ID as the data selector instead of the dropdown selector type. Inside component rule, you setup the condition and the email and inside the component rule, you'll have both order and customer profile tokens exposed. If you need the line items, add the line items to the component rule. The same will go for the data selector in the main event rule; you'll have to switch to direct input and use the ID number of an existing line item.
On a side note, if you want an email to be sent but only when the status changes (not every single time the order is saved), check out http://drupal.stackexchange.com/questions/138894/drupal-7-commerce-send-... for an idea on the proper rule condition logic.