0

I want to display product name in the subject of the Order confirmation email. I want to display like this Your Order for LooMantha Refrigerator... has been successfully placed

5
  • What if order contains more than one products? Apr 15 at 6:56
  • @Rahul Barot it has to show only first product and dots(.....) Apr 15 at 7:03
  • @Rahul Barot it has to show only first product dots(.....) and n(number of remainig products) more products has been successfully placed Apr 15 at 7:08
  • Okay, let me check if i can find something helpful to you. Apr 15 at 7:10
  • is any solution worked for you? Apr 16 at 4:35
0
  1. First, you need to create “events.xml” file inside your extension folder. app\code\Mital\EmailSubjectItems\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="order-add-custom-field-data" instance="Mital\EmailSubjectItems\Observer\Emailtemplatevars"/>
    </event>
</config>
  1. After that, you need to create “Emailtemplatevars.php” file using below code inside that file. In this file, you can add one or more parameters. app\code\Mital\EmailSubjectItems\Observer\Emailtemplatevars.php

     <?php
         namespace Mital\EmailSubjectItems\Observer;
    
         class Emailtemplatevars implements \Magento\Framework\Event\ObserverInterface
         {
             protected $helper;        
             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()->getOrder()->getId();
                 $order = $this->order->load($orderId);
                 $totalItems = count($order->getAllVisibleItems());
                 $i = 0;
                 foreach ($order->getAllVisibleItems() as $item)
                 {
                    if($i == 0){
                      $productName = $item->getName();
                      break;
                    }
                     $i++; 
                 }
    
                 if($totalItems > 1){
                     $more = $totalItems - 1;
                     $transport['itemName'] = $productName.'..and '.$more .' more products'; 
                 }else{
                     $transport['itemName'] = $productName .'product';   
                 }
    
             }
         }
    

    ?>

  1. and add below code inside that file. app\design\frontend\Themes\Yourtheme\Magento_Sales\email\order_new.html
<!--@subject Your Order For {{var itemName}} has been successfully placed @-->
<!--@vars {
"var formattedBillingAddress|raw":"Billing Address",
"var order.getEmailCustomerNote()":"Email Order Note",
"var order.increment_id":"Order Id",
"layout handle=\"sales_email_order_items\" order=$order area=\"frontend\"":"Order Items Grid",
"var payment_html|raw":"Payment Details",
"var formattedShippingAddress|raw":"Shipping Address",
"var order.getShippingDescription()":"Shipping Description"
} @-->
 
{{template config_path="design/email/header_template"}}
 
<!-- add your all custom parameter -->
 
<table>
    <tr class="email-intro">
        <td>
            <p class="greeting">{{trans "%customer_name," customer_name=$order.getCustomerName()}}</p>
            <p>
               
                {{trans "Thank you for your order from %store_name." store_name=$store.getFrontendName()}}
                {{trans "Once your package ships we will send you a tracking number."}}
                {{trans 'You can check the status of your order by <a href="%account_url">logging into your account</a>.' account_url=$this.getUrl($store,'customer/account/',[_nosid:1]) |raw}}
            </p>
            <p>
                {{trans 'If you have questions about your order, you can email us at <a href="mailto:%store_email">%store_email</a>.' store_email=$store_email |raw}}
            </p>
        </td>
    </tr>
    <tr class="email-summary">
        <td>
            <h1>{{trans 'Your Order <span class="no-link">#%increment_id</span>' increment_id=$order.increment_id |raw}}</h1>
            <p>{{trans 'Placed on <span class="no-link">%created_at</span>' created_at=$order.getCreatedAtFormatted(1) |raw}}</p>
        </td>
    </tr>
    <tr class="email-information">
        <td>
            {{depend order.getEmailCustomerNote()}}
            <table class="message-info">
                <tr>
                    <td>
                        {{var order.getEmailCustomerNote()|escape|nl2br}}
                    </td>
                </tr>
            </table>
            {{/depend}}
            <table class="order-details">
                <tr>
                    <td class="address-details">
                        <h3>{{trans "Billing Info"}}</h3>
                        <p>{{var formattedBillingAddress|raw}}</p>
                    </td>
                    {{depend order.getIsNotVirtual()}}
                    <td class="address-details">
                        <h3>{{trans "Shipping Info"}}</h3>
                        <p>{{var formattedShippingAddress|raw}}</p>
                    </td>
                    {{/depend}}
                </tr>
                <tr>
                    <td class="method-info">
                        <h3>{{trans "Payment Method"}}</h3>
                      {{var payment_html|raw}}
                    </td>
                    {{depend order.getIsNotVirtual()}}
                    <td class="method-info">
                        <h3>{{trans "Shipping Method"}}</h3>
                        <p>{{var order.getShippingDescription()}}</p>
                    </td>
                    {{/depend}}
                </tr>
            </table>
            {{layout handle="sales_email_order_items" order=$order area="frontend"}}
        </td>
    </tr>
</table>
{{template config_path="design/email/footer_template"}}
0

You need to set your custom data in transport data object, for that you can use below observer

app\code\Vendor\Module\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="change_new_order_email_subject" instance="Vendor\Moule\Observer\AddCustomData"/>
    </event>
</config>

In observer write code to add your custom variable to template

app/code/Vendor/Module/Observer/AddCustomData.php

<?php
namespace Vendor\Module\Observer;

class AddCustomData 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;   
        }
    }
}

Now in order to use this parameter in email subject you need to override default order email template in your custom theme

app\design\frontend\YourVendor\your_theme\Magento_Sales\email\order_new.html

<!--@subject {{trans "Your Order For "}} {{var itemName}} {{trans "has been successfully placed"}} @-->
<!--@vars {
"var formattedBillingAddress|raw":"Billing Address",
"var order_data.email_customer_note|escape|nl2br":"Email Order Note",
"var order.increment_id":"Order Id",
"layout handle=\"sales_email_order_items\" order=$order area=\"frontend\"":"Order Items Grid",
"var payment_html|raw":"Payment Details",
"var formattedShippingAddress|raw":"Shipping Address",
"var order.shipping_description":"Shipping Description",
"var shipping_msg":"Shipping message",
"var created_at_formatted":"Order Created At (datetime)",
"var store.frontend_name":"Store Frontend Name",
"var store_phone":"Store Phone",
"var store_email":"Store Email",
"var store_hours":"Store Hours",
"var this.getUrl($store,'customer/account/',[_nosid:1])":"Customer Account URL",
"var order_data.is_not_virtual":"Order Type",
"var order":"Order",
"var order_id": "Order DB Id",
"var order_data.customer_name":"Customer Name"
} @-->

{{template config_path="design/email/header_template"}}

<table>
    <tr class="email-intro">
        <td>
            <p class="greeting">{{trans "%customer_name," customer_name=$order_data.customer_name}}</p>
            <p>
                {{trans "Thank you for your order from %store_name." store_name=$store.frontend_name}}
                {{trans "Once your package ships we will send you a tracking number."}}
                {{trans 'You can check the status of your order by <a href="%account_url">logging into your account</a>.' account_url=$this.getUrl($store,'customer/account/',[_nosid:1]) |raw}}
            </p>
            <p>
                {{trans 'If you have questions about your order, you can email us at <a href="mailto:%store_email">%store_email</a>' store_email=$store_email |raw}}{{depend store_phone}} {{trans 'or call us at <a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}{{/depend}}.
                {{depend store_hours}}
                    {{trans 'Our hours are <span class="no-link">%store_hours</span>.' store_hours=$store_hours |raw}}
                {{/depend}}
            </p>
        </td>
    </tr>
    <tr class="email-summary">
        <td>
            <h1>{{trans 'Your Order <span class="no-link">#%increment_id</span>' increment_id=$order.increment_id |raw}}</h1>
            <p>{{trans 'Placed on <span class="no-link">%created_at</span>' created_at=$created_at_formatted |raw}}</p>
        </td>
    </tr>
    <tr class="email-information">
        <td>
            {{depend order_data.email_customer_note}}
            <table class="message-info">
                <tr>
                    <td>
                        {{var order_data.email_customer_note|escape|nl2br}}
                    </td>
                </tr>
            </table>
            {{/depend}}
            <table class="order-details">
                <tr>
                    <td class="address-details">
                        <h3>{{trans "Billing Info"}}</h3>
                        <p>{{var formattedBillingAddress|raw}}</p>
                    </td>
                    {{depend order_data.is_not_virtual}}
                    <td class="address-details">
                        <h3>{{trans "Shipping Info"}}</h3>
                        <p>{{var formattedShippingAddress|raw}}</p>
                    </td>
                    {{/depend}}
                </tr>
                <tr>
                    <td class="method-info">
                        <h3>{{trans "Payment Method"}}</h3>
                        {{var payment_html|raw}}
                    </td>
                    {{depend order_data.is_not_virtual}}
                    <td class="method-info">
                        <h3>{{trans "Shipping Method"}}</h3>
                        <p>{{var order.shipping_description}}</p>
                        {{if shipping_msg}}
                        <p>{{var shipping_msg}}</p>
                        {{/if}}
                    </td>
                    {{/depend}}
                </tr>
            </table>
            {{layout handle="sales_email_order_items" order_id=$order_id area="frontend"}}
        </td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}}

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.