How do I use data selector for Billing Address > First Name in Rules for Commerce?
I want to use the order owner's first name in automatically generated emails.
For example:
Hey Scott,
Thanks for your order...
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThe solution likely depends on the billing customer profile's address field using a split first and last name field instead of the combined "full name" field. I've noticed on my Commerce site that there appears to be a bug in Address Field (hope to fix soon) that prevents a full name field from being split up into first and last name fields or vise versa.
Assuming your customers are entering first and last name separately, then all you have to do is drill down from the order through the profile to the first name on the address field. To do this using a Rules data selector, you'd first need to add two "Entity has field" conditions - one to ensure the commerce-order entity has the commerce_customer_billing field and a second to ensure the commerce-order:commerce-customer-billing entity has the commerce_customer_address field. IF those are in place, using the data selector you can drill down to the following token:
commerce-order:commerce-customer-billing:commerce-customer-address:first-name
The problem comes when you actually want to embed this value in the body of an e-mail. Right now, as far as I can tell, there is no chain of tokens (replacement patterns in Rules UI parlance) that will drill down to the first name using that same pattern. Looking in the code for addressfield.module, I don't see any token related code. This may just need to be opened as a feature request for the Token module.
Found the solution. My rule was already sending an email but I could not see any token for the customer first and last name.
Add an action "Fetch entity by id":
Entity type > Value: Commerce Customer Profile
Identifier > Data selector: commerce-order:commerce-customer-billing:profile-id
Provided variables
Variable label: Customer Billing Profile
Variable name: customer_billing_profile
Add an action "Send email"
(if you installed Mime Mail module for sending email within Rules you can choose "Send HTML e-mail" like I did)
Use those tokens for first name and last name:
[customer-billing-profile:commerce-customer-address:first_name]
[customer-billing-profile:commerce-customer-address:last_name]
Note that it's taking the first and last name from the order itself NOT in the address book or profile. So if a user change its name in his address book it won't update in the order. I tried it.
I was inspired by a rule set found here:
https://gist.github.com/ogredude/3956560