Answers.Body: <p>This is not hard to implement in Commerce if you're ok with all the content going in one email (which I think is a better approach from the customer's point of view - we used to do this as separate emails years ago but customers got irritated that one order results in a whole bunch of emails). If you do want to trigger multiple separate emails, that's a tiny bit harder - but would not be difficult to implement in a plugin.</p>
  
  <p>(Sketch of that approach - listen to <code>onOrderComplete</code> and loop through cart items.  For any that match your desired triggers, parse a template and then email it off with Craft's email functions - simple stuff as far as plugins go - let me know if you want me to expand on this).</p>
  
  <p>But back to the one email approach - your order emails (receipts or any triggered by any other status) - are full twig templates - that means you can use conditionals, includes etc, to achieve what you want.</p>
  
  <p>Basically, in your email template, you can loop through the products in the order and set content into some 'storage' variables (with the content being directly written in that template, or stored in fields on your products, or stored in entries in a section, or using include of other templates - anything really!).... and then output that content later in the email.</p>
  
  <p>We do this to produce a 'Super Receipt' - basically a receipt that includes a whole lot of additional 'How To Get The Best From Your XXX' type content.  This content comes, in our case, from 3 levels - the product category for stuff that applies to e.g. all monitors, the brand, for brand specific stuff (how to get support from NEC, for example), and finally from product specific content.  This allows for very quickly building up libraries of this content, and a nice hierarchy of concerns.  Categories, Brands and Products each have a field <code>autoResponder</code> that holds this content.</p>
  
  <p>Clients love it.</p>
  
  <p>Here's the code.</p>
  
  <pre><code>    {# Store items we've seen already so we don't get duplicate AR content #}
      {% set seenItems = [] %}
      {% set seenProductCategories = [] %}
      {% set seenProductBrands = [] %}
  
      {# And store the actual content we need to print out at the end in groups #}
      {% set arProductContent = {} %}
      {% set arBrandContent = {} %}
      {% set arCatgeoryContent = {} %}
  
      {% for lineItem in order.lineItems %}
  
          {# No need to look at the same products twice... #}
          {% if lineItem.purchasable.product.id not in seenItems %}
  
              {% set seenItems = seenItems|merge([lineItem.purchasable.product.id]) %}
  
  
                  {# Category AR content #}
                  {% if lineItem.purchasable.product.id not in suppressedIds %}
                      {% for category in lineItem.purchasable.product.categories %}
                          {% if category.autoResponder|length %}
                              {% set arCatgeoryContent = arCatgeoryContent|merge({(category.title):category.autoResponder})  %}                             
                          {% endif %}
                      {% endfor %}  
                  {% endif %} 
  
                  {# Brand AR Content #}
                  {% set brand = lineItem.purchasable.product.brand.first() %}
                  {% if brand|length %}
                      {% if brand.id not in seenProductBrands %}
                          {% if brand.autoResponder|length %}
                              {% set seenProductBrands = seenProductBrands|merge([brand.id]) %}
                              {% set arBrandContent = arBrandContent|merge({(brand.title):brand.autoResponder})  %}
                          {% endif %}
                      {% endif %}
                  {% endif %}
  
                  {# Product AR Content  #}
                  {% set product = lineItem.purchasable.product %}
                  {% if product.autoResponder|length and product.type != "service" %}                        
                      {% set preamble = '&lt;small&gt;See also: &lt;a href="' ~ product.url ~ '#wiki"&gt;' ~ product.title ~ ' wiki&lt;/a&gt;&lt;/small&gt;' %}
                      {% set arProductContent = arProductContent|merge({(product.title): preamble|raw ~ product.autoResponder})  %}
                  {% endif %}
  
           {% endif %}
  
      {% endfor %}
  
      {# Now, print it all out, most general &amp; needed to most specific #}
  
      {% for key, content in arCatgeoryContent %}
          {{ emailLib.paraWithHeader ( key, content|raw )}}
      {% endfor %}
  
      {% for key, content in arBrandContent %}
          {{ emailLib.paraWithHeader ( key, content|raw )}}
      {% endfor %}
  
      {% for key, content in arProductContent %}
          {{ emailLib.paraWithHeader ( key, content|raw )}}
      {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-10T07:27:25.403Z
Answers.Text: Thanks Jeremy! Huge help. We started working through the one email approach, which was working out well. However, is there a way to conditionally send to different emails? The product that we need the new email for needs to go to a third party for logistics. We of course don't want them receiving all orders.
Answers.UserDisplayName: Arren Vidal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-13T07:42:49.900Z
Answers.Text: Well, you could use a plugin and ‘commerce_email.onBeforeSendEmail’ or ‘onSendEmail’ events...this gives you the chance to 'intercept' the email sending and change/add/removes addresses, and/or go nuts and use Craft's email function to do whatever you like really.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-09T03:03:13.933Z
Answers.LastActivityDate: 2016-12-09T03:17:14.720Z
Answers.LastEditDate: 2016-12-09T03:17:14.720Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I am looking for a way to send an email based on the products ordered.</p>
  
  <p>I was thinking this needed to be done using custom order statuses. Although, this would require the ability to change the default order status based on product. And with multiple product types ordered, would require the ability to set multiple statuses to an order.</p>
  
  <p>So, at this stage the ideal solution would be a custom email that is sent conditionally based on products ordered.</p>
  
  <p>I have so far been unable to find a solution for this. Any chance this is possible?</p>
  
Tags: <plugin-craftcommerce>
Title: Custom Email Based on Product Ordered
ThreadText: # Custom Email Based on Product Ordered
  
  (Asked by: username_0 on 2016-12-09)
  
  I am looking for a way to send an email based on the products ordered.
  
  I was thinking this needed to be done using custom order statuses. Although, this would require the ability to change the default order status based on product. And with multiple product types ordered, would require the ability to set multiple statuses to an order.
  
  So, at this stage the ideal solution would be a custom email that is sent conditionally based on products ordered.
  
  I have so far been unable to find a solution for this. Any chance this is possible?
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-12-09. Score: 1
  
  This is not hard to implement in Commerce if you're ok with all the content going in one email (which I think is a better approach from the customer's point of view - we used to do this as separate emails years ago but customers got irritated that one order results in a whole bunch of emails). If you do want to trigger multiple separate emails, that's a tiny bit harder - but would not be difficult to implement in a plugin.
  
  (Sketch of that approach - listen to `onOrderComplete` and loop through cart items. For any that match your desired triggers, parse a template and then email it off with Craft's email functions - simple stuff as far as plugins go - let me know if you want me to expand on this).
  
  But back to the one email approach - your order emails (receipts or any triggered by any other status) - are full twig templates - that means you can use conditionals, includes etc, to achieve what you want.
  
  Basically, in your email template, you can loop through the products in the order and set content into some 'storage' variables (with the content being directly written in that template, or stored in fields on your products, or stored in entries in a section, or using include of other templates - anything really!).... and then output that content later in the email.
  
  We do this to produce a 'Super Receipt' - basically a receipt that includes a whole lot of additional 'How To Get The Best From Your XXX' type content. This content comes, in our case, from 3 levels - the product category for stuff that applies to e.g. all monitors, the brand, for brand specific stuff (how to get support from NEC, for example), and finally from product specific content. This allows for very quickly building up libraries of this content, and a nice hierarchy of concerns. Categories, Brands and Products each have a field `autoResponder` that holds this content.
  
  Clients love it.
  
  Here's the code.
  
  ```
      {# Store items we've seen already so we don't get duplicate AR content #}
      {% set seenItems = [] %}
      {% set seenProductCategories = [] %}
      {% set seenProductBrands = [] %}
  
      {# And store the actual content we need to print out at the end in groups #}
      {% set arProductContent = {} %}
      {% set arBrandContent = {} %}
      {% set arCatgeoryContent = {} %}
  
      {% for lineItem in order.lineItems %}
  
          {# No need to look at the same products twice... #}
          {% if lineItem.purchasable.product.id not in seenItems %}
  
              {% set seenItems = seenItems|merge([lineItem.purchasable.product.id]) %}
  
                  {# Category AR content #}
                  {% if lineItem.purchasable.product.id not in suppressedIds %}
                      {% for category in lineItem.purchasable.product.categories %}
                          {% if category.autoResponder|length %}
                              {% set arCatgeoryContent = arCatgeoryContent|merge({(category.title):category.autoResponder})  %}                             
                          {% endif %}
                      {% endfor %}  
                  {% endif %} 
  
                  {# Brand AR Content #}
                  {% set brand = lineItem.purchasable.product.brand.first() %}
                  {% if brand|length %}
                      {% if brand.id not in seenProductBrands %}
                          {% if brand.autoResponder|length %}
                              {% set seenProductBrands = seenProductBrands|merge([brand.id]) %}
                              {% set arBrandContent = arBrandContent|merge({(brand.title):brand.autoResponder})  %}
                          {% endif %}
                      {% endif %}
                  {% endif %}
  
                  {# Product AR Content  #}
                  {% set product = lineItem.purchasable.product %}
                  {% if product.autoResponder|length and product.type != "service" %}                        
                      {% set preamble = '<small>See also: <a href="' ~ product.url ~ '#wiki">' ~ product.title ~ ' wiki</a></small>' %}
                      {% set arProductContent = arProductContent|merge({(product.title): preamble|raw ~ product.autoResponder})  %}
                  {% endif %}
  
           {% endif %}
  
      {% endfor %}
  
      {# Now, print it all out, most general & needed to most specific #}
  
      {% for key, content in arCatgeoryContent %}
          {{ emailLib.paraWithHeader ( key, content|raw )}}
      {% endfor %}
  
      {% for key, content in arBrandContent %}
          {{ emailLib.paraWithHeader ( key, content|raw )}}
      {% endfor %}
  
      {% for key, content in arProductContent %}
          {{ emailLib.paraWithHeader ( key, content|raw )}}
      {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_1! Huge help. We started working through the one email approach, which was working out well. However, is there a way to conditionally send to different emails? The product that we need the new email for needs to go to a third party for logistics. We of course don't want them receiving all orders.  
  > **username_1**: Well, you could use a plugin and ‘commerce_email.onBeforeSendEmail’ or ‘onSendEmail’ events...this gives you the chance to 'intercept' the email sending and change/add/removes addresses, and/or go nuts and use Craft's email function to do whatever you like really.  
  
  ---
  
  