0

We are looking for a solution whereby if a customer orders a specific product or product type, they will receive a different order confirmation (new order) email.

The standard confirmation emails mentions the the item being dispatched within a certain timeframe however, with the specific product we are referring to, the user would be sending an item in to us for repair / upgrade so it needs to be custom.

Can anyone help with this?

1 Answer 1

0

Yes, you can achieve this in Magento 2 by using an observer to modify the order confirmation email based on the products ordered. Here's a basic outline of the steps you would need to follow:

  1. Create an observer for the email_order_set_template_vars_before event.
  2. In the observer, check the products in the order to see if they match the specific product or product type you are interested in.
  3. If the products match, modify the email template variables to include the custom message you want to include in the email.
  4. If the products don't match, simply let the observer continue as normal and the standard confirmation email will be sent.

Here's an example implementation of the observer:

Create a new module and add the following code to your events.xml file:

   <event name="email_order_set_template_vars_before">
        <observer name="custom_order_confirmation_email" instance="Vendor\Module\Observer\CustomOrderConfirmationEmail" />
   </event>
  1. Create a new observer class at Vendor\Module\Observer\CustomOrderConfirmationEmail.php:

     <?php
     namespace Vendor\Module\Observer;
    
     use Magento\Framework\Event\Observer;
     use Magento\Framework\Event\ObserverInterface;
    
     class CustomOrderConfirmationEmail implements ObserverInterface
     {
         /**
          * @param Observer $observer
          */
         public function execute(Observer $observer)
         {
             // Get the order and the email template variables
             $order = $observer->getData('order');
             $transport = $observer->getData('transport');
             $vars = $transport->getData('vars');
    
             // Check if the order contains the specific product or product type
             $containsSpecificProduct = false;
             foreach ($order->getAllVisibleItems() as $item) {
                 // Replace "PRODUCT TYPE" and "PRODUCT SKU" with your own values
                 if ($item->getProductType() == "PRODUCT TYPE" || $item->getSku() == "PRODUCT SKU") {
                     $containsSpecificProduct = true;
                     break;
                 }
             }
    
             // If the order contains the specific product, add the custom message
             if ($containsSpecificProduct) {
                 $vars['custom_message'] = "This is a custom message for orders that contain the specific product or product type.";
             }
    
             // Update the email template variables and return the observer
             $transport->setData('vars', $vars);
             return $this;
         }
     }
    
  2. In the email template file for the order confirmation email (sales_email_order_template.xml), you can then include the custom message by using the {{var custom_message}} variable wherever you want the message to appear.

Note: Make sure to replace "PRODUCT TYPE" and "PRODUCT SKU" in the observer code with the actual product type or SKU that you want to trigger the custom email.

Your Answer

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

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