Modify address in order email

In order confirmation email, you see billing address and shipping address, this address format based on html template of customer address configuration. But you want to add data which not a customer attribute or an address attribute, how can you do that? To do that you need to change Renderer class.

Step 1: Add a plugin by using di.xml

<type name="Magento\Sales\Model\Order\Address\Renderer">
        <plugin name="add-custom-attribute-before-render"
                type="Yosto\CustomerAddress\Plugin\Order\Address\RendererPlugin"/>
    </type>

Step 2: Use beforeFormat function in class “RendererPlugin”

public function beforeFormat(
        \Magento\Sales\Model\Order\Address\Renderer $subject,
        \Magento\Sales\Model\Order\Address $address,
        $type
    ) {

  $address->setData("custom", "Custom Data");
}

Step 3: Edit address html template

Go to Stores -> Configurations -> Customers ->Customer Configuration -> Address Templates, add a new line to HTML format:

{{depend custom}}<br />Custom Data: {{var custom}}{{/depend}}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *