Rules, View Bulk Operations and some fancy actions in Drupal

Rules, View Bulk Operations and some fancy actions in Drupal

Profile picture for user alberto
drupal rules components screenshot

Working on large projects, usually we are requested to some specific features that are not provided out of the box in the modules that extend our drupal installation.
Here we have a particular scenario of a Business-to-Business e-commerce website in which both Clients and their Suppliers are allowed to order their product, but we want also that Suppliers are informed when a Client of their made an order on the site.
Well, this is a simplification of my case, where Clients have to be considered as Consumers and Suppliers as Clients, but we don't need to over-complicate things, right?

Now we have all elements to set the question:

  • we have an e-commerce system, built with drupal 7 and Commerce module;
  • we have two profile types: Clients and Suppliers and we need Profile2 as they act differently on our site (this could not be required in your use case, but it should be common if you're referencing users).
  • Clients and Suppliers are referenced each other (in my case they reference each other filling the registration form), this implies also that only registered user are allowed to make an order on the site.

As we have Commerce installed on our drupal 7 distribution, we also have Rules and Views, cause they are dependencies of drupal commerce. We need Profile 2 to separate Clients and Suppliers profiles and we need Entity Reference to reference Clients to their Suppliers. But what do the magic here is View-Bulk-Operations (VBO).

The inspiration to this post came from Pierre Vriens (thank you, Pierre!) who wrote some tips for VBO http://drupal.placeto.be/question/90, and a reply to a drupal question on Stackexchange.

Let's go with the tutorial and install all the modules mentioned and their dependencies.

1) Use Profile 2 to create your users's profile types and some example profiles then add your custom fields to them. Assumed you have "Supplier" and "Client" profile types,  you may want at least an entity reference field to create a reference between a Supplier and his Clients and eventually let the Client reference his Supplier too.

2) Assign the user "Client" to the user "Supplier" by the reference field. You may found that you can select users just by their username or other default field. So you may want to create an Entity Reference View to make the selection easier for an end user by selecting one of your custom profile field. To create such a view is quite simple: create an Entity Reference View, choose your profile type in Filter Criteria and show the custom field you want be referenced. Now you can set your reference field in the Content Type to be filled up by your custom field.

3) Create a new View Master (it will be used only by rules and not shown to site users), to do 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".

a) Show: Profiles, of type All, Unsorted.
b) Add a Contextual Filter: Profile User UID.
c) Add Relationship: Entity Reference (your entity referenced field - Be sure you're not selecting "referencing" field, then select Require this relationship).
d) Add Fields: Bulk Operation: Profile (use relationship with your referenced field- don't know if it make difference, but worked for me).
e) 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 him will receive an e-mail notification.

Be sure that your rule is in the right place in the workflow, if not, you can adjust the weight of the rule and the component on your needs.

I covered a specific B2B ecommerce scenario, but I hope this would be helpful with other use cases with some more customization. Feel free to ask questions, report your case and add more information to the article!

Aggiungi un commento

Solo a uso interno: questo campo non verrà mostrato.

HTML ristretto

  • Elementi HTML permessi: <br> <p> <h2> <h3> <h4> <h5> <h6> <strong> <em> <code> <blockquote> <ul> <ol start> <li>
  • Linee e paragrafi vanno a capo automaticamente.
Dichiaro di aver preso visione dellaPrivacy Policy del sito e acconsento alle finalità ivi espresse.