Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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...

share|improve this question

2 Answers 2

The 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.

share|improve this answer
    
Is there any update on this answer? –  jsheffers Aug 29 '13 at 15:12
1  
Actually, yes, Address Field has core token support nowadays. : ) –  Ryan Szrama Mar 19 at 13:36

Found the solution. My rule was already sending an email but I could not see any token for the customer first and last name.

  1. Add a condition "Entity has field" > commerce_customer_billing
  2. 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

  3. 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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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