0

I want print a custom file in order transactional emails. Here is my Setup file:

namespace xxx\xxxx\Setup;

    $quote = $setup->getTable('quote');
    $salesOrder = $setup->getTable('sales_order');


    $setup->getConnection()->addColumn(
        $quote,
        'gst',
        [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'nullable' => true,
            'comment' =>'Gst'
        ]
    );

    $setup->getConnection()->addColumn(
        $salesOrder,
        'gst',
        [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'nullable' => true,
            'comment' =>'Gst'
        ]
    );

    $setup->endSetup();  

The function that saves data on order

    class SaveOrderObserver implements ObserverInterface
   {
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    $quote = $observer->getEvent()->getQuote();

    $order->setData('gst', $quote->getGst());

    return $this;
}
}

The event xml:

 <event name="sales_model_service_quote_submit_before">

How to print GST field on user and admin email after order?

5
  • this event working, Issue of print on mail Nov 1, 2019 at 11:30
  • You want to display GST field in email? Nov 1, 2019 at 11:55
  • thanks @Ranganathan , Yes Nov 1, 2019 at 12:17
  • Did you saved value in quote table GST column and sales_order table GST column? Nov 1, 2019 at 12:30
  • yes both table quote and sales_order save in db. Nov 1, 2019 at 12:40

1 Answer 1

1

app/code/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\Module\Observer\ObserverforAddCustomVariable" />
    </event>
</config>

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

<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;

class ObserverforAddCustomVariable implements ObserverInterface
{
    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getEvent()->getTransport();
        $order = $transport->getOrder();
        $transport['gst'] = $order->getGst();
    }
}

Create custom order email template in Admin panel->Marketing->Email Templates and use below code to display in email template

 {{var gst}} 

Check below link to how to override email templates

Override email templates

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.