1

I have a Drupal 7 site running including Commerce. I'm asked to make the webshop work in a way that the mail send after completing payment is send to the product owner(s).

So when creating a product via /admin/commerce/products/add the admin fills in the email address of the product owner/seller in a custom field called field_seller_mail.

When the visitor of the website pays its order, the site should send one or more mails to the owner(s) of the products zo that they can handle the delivery of the products.

In rules I choose the when payed in full trigger.
Then I choose to loop in the action part and loop trough the line items, but the field field_seller_mail is nowhere to be found.

What is the best way to solve this problem?

1
  • This is a common support request for Rules in that custom fields need to be "discovered" first via an "Entity has field" condition. For more info, please refer to the FAQ on DrupalCommerce.org: Why can't I access my custom fields in Rules? Commented Jul 17, 2018 at 19:08

1 Answer 1

1

If the data about a product are not available when processing a "line item", you can make them available. To do so, use a technique similar to what is explained in my answer to "How to access commerce_product field of line-items using Rules to flag the Commerce Product?", which includes a Rules Component that looks like so:

{ "rules_perform_an_action_on_a_selected_line_item" : {
    "LABEL" : "Perform an action on a selected line item",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "selected_line_item" : { "label" : "Selected Line Item", "type" : "commerce_line_item" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "selected-line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "selected-line-item:commerce-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "product_fetched" : "Fetched Product" } }
        }
      },
      { "drupal_message" : { "message" : "Some details about this product: Product ID = [product-fetched:product-id], Product SKU = [product-fetched:sku], Product Title = [product-fetched:title], Product Creator = [product-fetched:creator]" } }
    ]
  }
}

Here is a blueprint of what you need to do:

  1. Create a Rules Component, similar to the above Rules Component. Just an import of that Rules Component should be enough (to get started and further improve it as detailed below).
  2. Rework the Drupal Message shown via the last Rules Action in the Rules Component (Starting with "Some details about this product"), e.g. by also using any of the available tokens that fit your needs.
  3. Execute that Rules Component inside your Rules Loop of the rule in which you want to loop over all line items.
  4. Improve the Rules Component from Step 1 by creating whatever additional Rules Action(s) you need. Make sure to place these Rules Action(s) AFTER that Rules Action (in the Rules Component) that performs "Fetch entity by ID".
2
  • 1
    Hi, thanks for the extensive answer on my question. It works great, but I have a little (new) problem. I want to add the users billing/shipping information to the message. But I can't see to find out how. When I add a new parameter of the Commerce Customer Profile it breaks my rule. How can I solve this?
    – Joost
    Commented Jul 24, 2018 at 12:02
  • I did: drupal.stackexchange.com/q/266296/41860
    – Joost
    Commented Jul 24, 2018 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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