I was not sure if I had to post my help request in the drupal commerce forum, but I thought that my case could suite other similar Rules requests.

I have a business-to-business ecommerce system built with drupal7, commerce (module, not a kickstart distro) and using Profile2, Entity Reference and Rules to provide all my needed features.

I created two profile types (and relatives roles): let's call them "Clients" and "End user". End Users should be intended as real user of the goods sold by the site. Clients are most similar to Contractors or Suppliers.
To be more specific a "Client" could have multiple "End Users" related to it, and an "End User" just have one "Client" associated with him. I use Entity reference+Profile2 to let Clients and End User reference themselves. For example, the End User can reference the Client by inserting the Identification VAT number. For other purpose the Client can reference his End Users by inserting a personal code.

Quite simple scheme:
Client A
--End User1 (referenced by a personal code field)
--End User2 (referenced by a personal code field)

End User1
- Client A (referenced by VAT-ID field)
End User2
--Client A (referenced by VAT-ID field)

Both of them can buy from the store, but "Clients" should be notified when their referenced "End Users" complete an order on the site.

Here, in my thoughts, come Rules module, but I'm in trouble to get what I want. I read about using multiple actions or loops or even component, but all topics have some little but significant differences from my case.

I'm trying again with a simple Rule:
Event > After completing checkout
Condition > Entity has field (site:current-user:profile-end-user), Field: field_client_reference

I expected to fetch the referenced user by field_client_reference but I cannot sort the user from data selection in the action of the Rule. I'm not sure if I should select User or Profile as Entity, in any case I didn't sort it out.

Maybe I should add some condition or choose another action, but in a certain point I get lost with it...

Thank you!

Comments

tiikeri’s picture

Maybe going little further...

Following this post: http://drupal.stackexchange.com/questions/213964/which-tokens-can-i-use-...
and the referenced guide http://www.wunderkraut.com/content/using-vbo-to-load-list-of-objects-int...

I used View VBO and created a View that list all "Clients" profiles. Then I was able to create a Rule Component "action set" that load the VBO list and send an e-mail to the clients. Of course it send the e-mail to all profiles of type "client".
I need to send the e-mail only to the referenced user of type "client"

I edited the existing rule on Event "After completing checkout" and configured the condition to:
Entity is of type> Parameter: Entity: [commerce-order:owner:profile-end-user] >Type:Profile
And
Entyt has field > Parameter: [commerce-order:owner:profile-end-user:field-client-reference] >Field: field_client_reference

But after completing a checkout for testing, the mail is not sent.
Any idea of what I'm missing?
many thanks

tiikeri’s picture

Finally got it! I think I should edit the title and some content to explain better the goal and the solution.

We need the system send an email to a "supplier" after a referenced "client" completed the checkout.

Requirements:
Commerce
Profile 2
Entity reference
Views Bulk Operations
Rules (of course)
and all their dependencies.

1) Use Profile 2 to create your users's profiles and profile types and add your custom fields to them. Assume you have a "supplier" and "client" profile types, in this case you want at least an entity reference field to create a reference between a "client" and a "supplier" and eventually let the client reference the supplier.
2) Assign the client to the supplier by the reference field (you may want to create an Entity Reference View to make the selection easier for an end user)
3) Create a View Master (it will be used only by rules), so uncheck "page" formatter and give the view a properly name and description. With this View, we will filter the "supplier" profiles referenced by "clients".
Show: Profiles, of type All, Unsorted.
Add a Contextual Filter: Profile User UID
Add Relationship: Entity Reference (your entity referenced field - Be sure you're not selecting "referencing" field then select Require this relationship)
Add Fields: Bulk Operation: Profile (use relationship with your referenced field- don't know if it make difference, but worked for me)
Save your View.

4) Go to your Rules admin UI. Tab "Components" and add a new component of type "Action Set"
add action "Load a list of entity objects from a VBO View."
Value: Select your VBO view you created before
Arguments: [site:current-cart-order:uid]
Provide variables, for exemple "List of referenced suppliers"

Add a Loop
Parameter: the Item List you created befor, i.e. [suppliers_list]
List item: current element of the list (referenced_supplier)

Add an action to the loop> Send mail: to referenced-supplier:user:mail
Subject: anything you want
Message: The mail's message body
From: you may leave empty to use site-wide e-mail.

Save the component and go to Rules.

5) Add a new Rule, name it properly. Event "after completing checkout".
Add Condition: User has role(s) - User: [commerce-order:owner] - Role: Supplier (if we have a role named "supplier" to identify suppliers)
Add Action: select your action set component you created before.
Additionally you can add another action "show a message on the site" to inform the user that the supplier associated with it will receive an e-mail notification.

Et voilà ;)
Maybe I discovered the boiled water, but feels good anyway.