0

I use Drupal Commerce and the rules module to send email which works OK. But it doesn't send the email to author node of product.

Here is an export of my current rule:

{ "commerce_checkout_order_email" : {
    "LABEL" : "Send an order notification e-mail",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "4",
    "OWNER" : "rules",
    "TAGS" : [ "Commerce Checkout" ],
    "REQUIRES" : [ "rules", "mimemail", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "mimemail" : {
          "USING" : {
            "key" : "emailrenzo",
            "to" : "[commerce-order:mail]",
            "from_name" : "Regalalo",
            "from_mail" : "info@regalalo.pe",
            "subject" : "Pedido # [commerce-order:order-number] en [site:name]",
            "body" : "\u003Cdiv\u003E\r\n\u003Cimg src=\u0022\/http:\/\/34.214.32.99\/sites\/default\/files\/logo-regalalo.png\u0022\u0022\u003E\r\n\u003C\/div\u003E\r\n\r\nHola [commerce-order:owner]!!\r\ngracias por realizar un pedido en [site:name], su n\u00famero de pedido es # [commerce-order:order-number].\r\n\r\nSi este es su primer pedido con nosotros, pronto recibir\u00e1 otro correo electr\u00f3nico con las instrucciones para acceder a nuestro sitio con su nombre de usuario y contrase\u00f1a. Puede ver su historial de pedidos en cualquier momento ingresando a nuestro sitio web:\r\n\r\n[site:login-url]\r\n\r\nPuede consultar el estado de su pedido en:\r\n\r\n\u003Ctable cellspacing=\u00220\u0022 cellpadding=\u002210\u0022 border=\u00221\u0022\u003E\u003Ctr\u003E\u003Ctd\u003E\u003Ctable cellspacing=\u00220\u0022 cellpadding=\u002210\u0022 border=\u00221\u0022\u003E\u003Ctr\u003E\u003Ctd\u003E[commerce-order:commerce_line_items]\u003C\/td\u003E\u003C\/tr\u003E\u003C\/table\u003E\u003C\/td\u003E\u003C\/tr\u003E\u003C\/table\u003E\r\n\r\n[commerce-order:customer-url]\r\n\r\nPor favor comun\u00edquese con nosotros si tiene alguna duda.\r\n\r\n\u00a1Gracias por comprar en nuestra plataforma! Vuelve a visitarnos aqu\u00ed: Regalalo.PE\r\nSi tienes alguna pregunta acerca de tu pedido, contacta la tienda del producto que tendr\u00e1 el agrado de atenderte. \r\nPara otra consulta de asistencia env\u00edanos un correo a \u003Ca href=\u0022mailto:info@regalalo.pe\u0022\u003E info@regalalo.pe\u003C\/a\u003E\r\n",
            "language" : [ "commerce-order:state" ]
          },
          "PROVIDE" : { "send_status" : { "send_status" : "Send status" } }
        }
      },
      { "mail" : {
          "to" : [ "commerce-order:owner:mail" ],
          "subject" : "Pedido # [commerce-order:order-number] en [site:name]",
          "message" : "Hola [commerce-order:owner]!!\r\nGracias por realizar un pedido en [site:name], su n\u00famero de pedido es # [commerce-order:order-number].\r\n\r\nSi este es su primer pedido con nosotros, pronto recibir\u00e1 otro correo electr\u00f3nico con las instrucciones para acceder a nuestro sitio con su nombre de usuario y contrase\u00f1a. Puede ver su historial de pedidos en cualquier momento ingresando a nuestro sitio web:\r\n\r\n[site:login-url]\r\n\r\nPuede consultar el estado de su pedido en:\r\n\r\n[commerce-order:commerce_line_items]\r\n\r\n[commerce-order:customer-url]\r\n\r\nPor favor comun\u00edquese con nosotros si tiene alguna duda.\r\n\r\n\u00a1Gracias por comprar en nuestra plataforma! Vuelve a visitarnos aqu\u00ed: Regalalo.PE\r\nSi tienes alguna pregunta acerca de tu pedido, contacta la tienda del producto que tendr\u00e1 el agrado de atenderte. \r\nPara otra consulta de asistencia env\u00edanos un correo a \u003Ca href=\u0022mailto:info@regalalo.pe\u0022\u003E info@regalalo.pe\u003C\/a\u003E",
          "language" : [ "commerce-order:state" ]
        }
      }
    ]
  }
}

This rule works with user customer but not sellerr or authors, what´s your recommendation?

  • hi Pierre I share mi rule dropbox.com/s/yewp23pol32h5y0/rule.txt?dl=0 this rule works with user customer but not sellerr or authors, what´s your recommend ? greetins – renzo2015 Sep 14 '17 at 6:34
  • ok i done below – renzo2015 Sep 14 '17 at 6:43
  • ok, merci for taking my advice. Please check my edit of your question to fix formatting, grammar, etc. If you don't like my edit at all, just perform a rollback, ok? Next question about "doesn't work": edit your question (not extra comment!) to explain if you mean the 1st or 2nd Rules Action which "doesn't work". Also, have you tried any kind of Rules Debugging, or can you do an experiment to (temporary) add a "show message on site" to display values of relevant tokens that you are using in the existing Rules Actions? FYI: debugging is the art of taking away all possible problems ... – Pierre.Vriens Sep 14 '17 at 6:58
  • thanks for your answer, yes i tested many tokens in my rules but it works only by the customers but not auhotr or sellers – renzo2015 Sep 14 '17 at 7:03
  • Please note: I did not "answer" (yet ...), I only posted a comment. Also, your prior comment does not help "me" (or anybody) to better understand your question. You really need to try to improve (edit!) your question to "document" things like (a) the tokens you tested (b) which of the 2 rules actions is the one causing the problem ("does not work" does not mean anything for anybody reading your question). Please read my prior comment again, and try to EDIT your question to address all of them. Just trying to help, ok? – Pierre.Vriens Sep 14 '17 at 7:08
1

In short; you should loop over all commerce order line items, obtain the product owner and add it to a list. Then in a new loop, go through the created list to send all product owners a main.

First you need a rules component that checks if a provided line item contains a product field and pushes the author to a given list (enforcing uniqueness makes sure every product owner is only added once). You can import below code on the components tab:

{ "rules_product_on_line_item" : {
    "LABEL" : "get product owner from line item",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "line_item" : { "label" : "Line item", "type" : "commerce_line_item" },
      "owners" : { "label" : "Product owners", "type" : "list\u003Cuser\u003E" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "list_add" : {
          "list" : [ "owners" ],
          "item" : [ "line-item:commerce-product:creator" ],
          "unique" : "1"
        }
      }
    ]
  }
}

Now for the main rule, create a new variable of type list of users. This is where we will store all product owners in.

Create a loop over commerce-order:commerce-line-items. In this loop, use the component that was created above. This will fill the list of product owners.

Finally, loop over the list of product owners, and send each of them a mail. The complete rule would look something like this:

{ "rules_mail_all_product_owners" : {
    "LABEL" : "Mail all product owners on an order",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "5",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "mimemail", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "list\u003Cuser\u003E" },
          "PROVIDE" : { "variable_added" : { "product_owner_list" : "Product owner list" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "component_rules_product_on_line_item" : { "line_item" : [ "list-item" ], "owners" : [ "product-owner-list" ] } }
          ]
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "product-owner-list" ] },
          "ITEM" : { "productowner" : "Current product owner" },
          "DO" : [
            { "mimemail" : {
                "key" : "mailkey",
                "to" : [ "productowner:mail" ],
                "from_mail" : [ "site:mail" ],
                "subject" : "",
                "body" : ""
              }
            }
          ]
        }
      }
    ]
  }
}
  • This seems indeed the way to go to answer the question here. And even though I've not "tested" this rule+component, but by performing a code review (eyeballing only), I wonder if it doesn't contain a minor (though critical) mistake. Have you tested this rule+component, and if so did it produce the desired (expected) result? – Pierre.Vriens Sep 14 '17 at 10:34
  • I have a slightly modiefied version of this running in production. But I had to clean it up for here. What would you think the mistake is? – Neograph734 Sep 14 '17 at 10:36
  • Hint: Is "owners" in your Rules Component really a "Parameter"? – Pierre.Vriens Sep 14 '17 at 10:38
  • 1
    ok, I "believe" you then (as per your extra test result). I always thought that in such scenario you must define that variable as "Provided", so I learned something, Merci (oeps: Dank-je-wel, oeps-oeps: thank you) ... And +1! – Pierre.Vriens Sep 14 '17 at 11:09
  • 1
    ok I got it, one more time thanks @Neograph734 for this code – renzo2015 Sep 15 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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