0

I am working with placing an order with the Commerce module in Drupal. When the order is confirmed, a notification will be send to the user with order details. Here I am using a rule like when an order is confirmed, customer will receive an email.

The email content will be from the Message module based on the Rules module. Also there is a content from the Commerce module regarding order details.

My doubt is that how to get this order details content from the Commerce module to this Message module content.

Below is the mail content that I saw in the backend when the rule is applied. But the content doesn't have any order details. But when the mail is received order details are present. I want to find out how to get these order details along with this content.

Dear [message:message-commerce-order:commerce-customer-billing:commerce-customer-address:first_name], Thank you for purchasing through [site:name]. You can view your order history by logging into our website at:[message:message-commerce-order:view-url] Sincerely, Order Number: [message:message-commerce-order:order-number]

2 Answers 2

0

Part 1: About the concept of using Replacement tokens

By using the Rules module, you should be able to make any relevant token concerning the order details content (as in your question ...) available by using the Replacement patterns for each message type created with the Message module.

So the answer to your question is "Whatever token you're missing, just make it available via Rules, by creating a replacement token for them". Refer to the answer to What's the purpose of "Replacement tokens" of a Message type? for way more details on that.

These Replacement tokens are really a hidden gem about how Rules integrates with Message. For an example to see them at work, refer to my answer to "Which user related token(s) can be used to create a Rule for setting up a message type?".

Part 2: Including some order detail in your message type

1. Scenario

Let's assume you want allow the user to enter some (custom) order detail. I.e. let's just call it "order remark" (some free format text, which any customer can use for whatever they want).

We want the related order confirmation message (actually an eMail) to have an eMail subject that includes the value of the free format text entered as "order remark".

Read on for the steps needed to get this to work ... Starting from a vanilla "Commerce Kickstart" installation.

2. Add an additional order field

Use relative path admin/commerce/config/order/fields to add another (custom) field within "Order settings". In my case, I use a text field, with machine name field_order_remark.

3. Define a replacement token

Use relative path admin/structure/messages/manage/commerce_order_order_confirmation to edit the configuration of Message type "Commerce Order: order confirmation". Within the field labelled "Replacement tokens", enter something similar to @remark_related_to_order (= this will be the name of the token we want to use / populate in the next step).

On the same (Message type) form, you can start to use (= refer to) this replacement token, and this anywhere you want to do so. As a sample, I just added (remark: @remark_related_to_order) at the end of the eMail subject (within the Message Text labelled "View modes: Notify - Email subject").

4. Custom rule

The only thing left, is to create an appropriate rule (or tune an existing rule), so that when a Message is created (of type "Commerce Order: order confirmation"), a value is assigned to the replacement token @remark_related_to_order, whereas the actual value is the value entered for the (custom) order field with machine name field_order_remark.

To do so, just consider the rule below (in Rules export format):

{ "rules_commerce_order_message_order_notification" : {
    "LABEL" : "Commerce order message: order notification e-mail",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "3",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "message_notify", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-order" ], "field" : "field_order_remark" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "message",
            "param_type" : "commerce_order_order_confirmation",
            "param_user" : [ "commerce-order:owner" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:message-commerce-order" ],
          "value" : [ "commerce-order" ]
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:arguments:remark-related-to-order" ],
          "value" : [ "commerce-order:field-order-remark" ]
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ], "immediate" : 1 } },
      { "message_notify_process" : {
          "message" : [ "entity-created" ],
          "save_on_fail" : 0,
          "save_on_success" : 0
        }
      }
    ]
  }
}

The above rule is actually a minor variation of the same named rule shipped with Commerce Kickstart, in which these are the specific changes I applied to get it to work:

  • the extra Rules Condition "Entity has field". This is to make rules aware of the (custom) order field with machine name field_order_remark. And so that we can refer to it in a subsequent Rules Action.
  • the extra Rules Action to "Set a data value". I.e. for the replacement token remark_related_to_order, whereas the actual value is the value entered for the (custom) order field with machine name field_order_remark. Note the special syntax used for that entity-created:arguments:remark-related-to-order

Voilà, that's really it. Just repeat the steps above for any other "order detail" you want to be processed in a similar way (each of them will require an additional replacement token, and the rule will have to assign an appropriate value using some order related token.

PS: I'm assuming your question is about D7.

6
  • Can we add custom template for this message?Especially for that token section commerce module regarding order details? Commented Mar 8, 2018 at 5:33
  • not sure what you mean by that custom template. Please EDIT your question to add an export of a rule that has a rules action to show a drupal system message in which you show 1 of the order details. I will then try to improve my answer to use that as a replacement token. Commented Mar 8, 2018 at 6:00
  • BELOW IS THE MAIL CONTENT THAT I SAW IN THE BACKEND WHEN APPLIED THE RULE.BUT THE CONTENT DOESNT HAVING ANY ORDER DETAILS.BUT WHEN THE MAIL IS RECEIVED ORDER DETAILS ARE PRESENT.I WANT TO FIND OUT HOW TO GET THIS ORDER DETAILS ALONG WITH THIS CONTENT.CAN YOU HELP ME? Dear [message:message-commerce-order:commerce-customer-billing:commerce-customer-address:first_name], Thank you for purchasing through [site:name]. Yyou can view your order history by logging into our website at:[message:message-commerce-order:view-url] Sincerely, Order Number: [message:message-commerce-order:order-number] Commented Mar 8, 2018 at 8:57
  • sorry am not getting what you are saying Commented Mar 8, 2018 at 9:26
  • @user3487918: writing entire phrases with all uppercases is commonly perceived as "yelling". Also, your original question includes "based on Rules" (which is indeed the way to dowhat you try to achieve). But you still didn't mention at least "1 of the order details" you want to use/include in your message type. If I may ask: did you already carefully review/study the content of those 2 links in my answer? Trust me: the solution for your question will be similar. But if you don't mention at least 1 order detail you wonder about, I cannot improve my answer to teach you about that one. Get it? Commented Mar 8, 2018 at 9:38
1

You just goto http://localhost/project/admin/commerce/config/checkout/rules/manage/commerce_checkout_order_email/edit/3

enter image description here

then as screenshot into the message value you can set your custom message and also use "Replacement patterns" for order detail to the message.

thank you..

3
  • Can we add custom template to this message ?? Especially that token from commerce module? Any hooks for that? Commented Mar 8, 2018 at 5:28
  • Also try hook_mail_alter().. api.drupal.org/api/drupal/modules%21system%21system.api.php/… Commented Mar 8, 2018 at 7:04
  • Hello thanks for your help.Because i got the reference field message_commerce_order based on your suggestion.Thanks a lot.The data referred by "message_commerce_order" is order details.This content is in table format.Then how to find out how this data is calculated? Commented Mar 8, 2018 at 9:17

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.