0

How to to add the Purchase Order Number next to the normal Magento order id on all sales email templates as well.

I try this but not working any idea for that invoice mail templaes

Please check screen sort.

enter image description here

app/code/vendor/module/etc/di.xml

<preference type="Magento\Sales\Model\Order\Email\Sender\InvoiceSender" for="vendor\module\Model\Order\Email\Sender\InvoiceSender" /> 

app/code/vendor/module/Model/Order/Email/Sender/InvoiceSender.php

'purchase_order_number' => $order->getPayment()->getPoNumber(),

app/code/vendor/module/view/frontend/email/invoice_new.html

<h1{{trans 'Po Number'}}&nbsp;{{var purchase_order_number|raw}}</h1>

3 Answers 3

0

Note: Answer is Related to magento 2.3.x

Step 1. Create di.xml

/app/code/Vendor/Module/etc/di.xml

add below code in di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="Vendor\Module\Rewrite\Sales\Model\Order\Email\SenderBuilder" />
</config>

Step 2. Create SenderBuilder.php

/app/code/Vendor/Module/Rewrite/Sales/Model/Order/Email/SenderBuilder.php


<?php
namespace Vendor\Module\Rewrite\Sales\Model\Order\Email;
class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    
    

    protected function configureEmailTemplate()
    {   
        
        $getTemplateVars = $this->templateContainer->getTemplateVars();
        $getTemplateVars['custom_variable_add'] = 'Custom Variable Text';    
        $this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());             
        $this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
        $this->transportBuilder->setTemplateVars($getTemplateVars);
        $this->transportBuilder->setFromByScope(
            $this->identityContainer->getEmailIdentity(),
            $this->identityContainer->getStore()->getId()
        );      
    }
}

in email template add variable

"var custom_variable_add":"Custom Variable"

and use like

{{trans 'Order total: %custom_variable_add' custom_variable_add=$custom_variable_add|raw}}

accept solution if works for you.

0
  1. You mixed "type" and "for" in your preference. It should be:
<preference for="Magento\Sales\Model\Order\Email\Sender\InvoiceSender" 
            type="vendor\module\Model\Order\Email\Sender\InvoiceSender" />
  1. Also, to override an e-mail template you need to copy the file to the theme (not module) - see: https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/templates/template-email.html

  2. If you must override it in a module, you have to create etc/email_templates.xml file in your module with the following content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="sales_email_invoice_template" file="your_template_name.html" module="Vendor_Module"/>
</config>
0

app/design/frontend/Vendorname/ThemeName/Magento_sales/email/invoice_new.html

{{depend order.getPayment().getPoNumber()}}

  <h1>{{trans 'Po Number #'}} &nbsp;{{var order.getPayment().getPoNumber()|raw}}</h1>

{{/depend}}

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.