cancel
Showing results for 
Search instead for 
Did you mean: 

Order confirmation email (additional text) based on product ?

SOLVED

Order confirmation email (additional text) based on product ?

Hi all

I'm trying to think of a way to add additional html and text to the order confirmation email template based on the products purchased.

All of the products belong to one parent category.

It's a big ask I know and probably just not possible.

The issue is that I need to instruct the customer of additional order processing steps if they order products purchased from this particular parent category.

If anyone has suggestions, I'd be most grateful.

Thank you all in advance

Andy

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Order confirmation email (additional text) based on product ?

@Andy_Acute 

 

Yes, you will need to create a new module (you always should), and yes you can set condition based on the product category, the condition where I matched it with product name, you can change the condition with product category.

Let me know if it works for you !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

View solution in original post

4 REPLIES 4

Re: Order confirmation email (additional text) based on product ?

Hello @Andy_Acute 

 

You need to create two files :

 

1. events.xml inside Vendor/Module/etc/events.xml

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="add_Custom_variable_to_Order" 
            instance="[Vendor]\[ModuleName]\Observer\ObserverforAddCustomVariable" />
    </event>
</config>

2. Create observer for the event :

<?php
namespace [Vendor]\[ModuleName]\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverforAddCustomVariable implements ObserverInterface
{

    public function __construct(
    ) {
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $transport = $observer->getTransport();
$order = $transport->getOrder();
$items = $order->getAllItems();
$value = "";
foreach($items as $item){

// ADD YOUR CONDITION BELOW
if($item->getName() == "PRODUCT NAME"){
$value .= "<h1>CUSTOM TEXT</h1>";
}
} $transport['CustomVariable1'] = $value; } }

And in email template you can use this variable by adding this in email template :

{{var customVariable1}}

Hope it helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Order confirmation email (additional text) based on product ?

Hi guarav

Thank you for your help

Do I need to create a new module for this?

When you mention Vendor/Module/etc/events.xml is 'Module' a new module?

Where does the observer go?

Thanks again

Andy

Re: Order confirmation email (additional text) based on product ?

Also, is it possible to base this condition on the category the products belong to?

Thanks again

Andy

Re: Order confirmation email (additional text) based on product ?

@Andy_Acute 

 

Yes, you will need to create a new module (you always should), and yes you can set condition based on the product category, the condition where I matched it with product name, you can change the condition with product category.

Let me know if it works for you !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy