1

module.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magetrend_Custom" schema_version="2.0.0" setup_version="2.0.0">
        <sequence>
            <module name="Magetrend_Email"/>
        </sequence>
    </module>
</config>

registration.php

 <?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magetrend_Custom',
    __DIR__
);

event file C:\xampp\htdocs\myshop\app\code\Magetrend\Custom\etc events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="magetrend_email_collect_additional_vars">
        <observer name="mtemail_custom_var" instance="Magetrend\Custom\Observer\CustomVar"/>
    </event>
</config>

observer file C:\xampp\htdocs\myshop\app\code\Magetrend\Custom\Observer CustomVar.php

<?php

namespace Magetrend\Custom\Observer;

class CustomVar implements \Magento\Framework\Event\ObserverInterface { protected $order;

public function __construct(           
    \Magento\Sales\Model\Order $order  
)
{ 
    $this->order = $order;
}


public function execute(\Magento\Framework\Event\Observer $observer)
{       
    $transport = $observer->getTransport();
    $orderId = $observer->getTransport()->getOrderId();
    $order = $this->order->load($orderId);
    $allVisibleItems = $order->getAllVisibleItems();
    $totalItemsCount = count($order->getAllVisibleItems());
    $i = 0;
    foreach ($allVisibleItems as $item)
    {
        if($i == 0){
            $productName = $item->getProduct()->getName();
            break;
        }
        $i++;
    }

    if($totalItemsCount > 1){
        $remainingItems = $totalItemsCount - 1;
        $transport['itemName'] = $productName.' and '.$remainingItems .' more products'; 
    }else{
        $transport['itemName'] = $productName;   
    }
}

}

enter image description here gmail picture


what is the problem? email template

1 Answer 1

0

Please use the email_order_set_template_vars_before event, which allows you to send custom parameters to your order email. Check the working code below:

app/code/Magetrend/Custom/etc

events.xml

<?xml version="1.0"?>
<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="mtemail_custom_var" instance="Magetrend\Custom\Observer\CustomVar"/>
 </event>
</config>

app/code/Magetrend/Custom/Observer

CustomVar.php

<?php

namespace Magetrend\Custom\Observer;

class CustomVar implements \Magento\Framework\Event\ObserverInterface
{
    protected $order;

    public function __construct(
        \Magento\Sales\Model\Order $order
    )
    {
        $this->order = $order;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getTransport();
        $orderId = $observer->getTransport()->getOrderId();
        $order = $this->order->load($orderId);
        $allVisibleItems = $order->getAllVisibleItems();
        $totalItemsCount = count($order->getAllVisibleItems());
        $i = 0;
        foreach ($allVisibleItems as $item)
        {
            if($i == 0){
                $productName = $item->getProduct()->getName();
                break;
            }
            $i++;
        }

        if($totalItemsCount > 1){
            $remainingItems = $totalItemsCount - 1;
            $transport['itemName'] = $productName.' and '.$remainingItems .' more products';
        }else{
            $transport['itemName'] = $productName;
        }
    }
}

Add the following line to the email template:

<h3>{{var itemName|raw}}</h3>

Note: After updating and adding the above code, please revise your order email template. Ensure that you are checking the correct order email template. Add a static value and verify if the order email is displaying correctly or not.

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.