0

I have added a custom field in checkout form and saved that values in quote table. And also displayed in the backend in order details. Now I want to send that custom field value in email. I have used observer to do so. When static value is passed then it is printed in email but I am not getting any solution to get the custom field value from quote table and pass it in email. I am getting order id but from that order id please help/guide me to find the custom field value.

Below is my code

namespace Rishi\Emails\Observer;
use Magento\Framework\Event\ObserverInterface;

class AddMyData implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getTransport();
        $order=$transport['order'];
        $orderId=$order->getEntityId();

        $transport['myvar'] = $orderId;
        $transport['payment_html'] = 'myvar';
    }
}

I have created a custom template and added {{var myvar|raw}}.

Please help me.

2
  • What event are you usign for the observer ?
    – Taschert
    Commented Apr 13, 2021 at 11:58
  • 'sales_model_service_quote_submit_before' this event.@Taschert Commented Apr 13, 2021 at 11:59

1 Answer 1

1

You have to save that quote value to the order table and then get direct value in an email template.

  1. For save quote value to order table. Define Events in the events.xml file.
   <?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="sales_model_service_quote_submit_before">
        <observer name="save_custom_fee" instance="Vendor\Module\Observer\AddCustomField" />
    </event>
</config>
  1. Now Create an Observer file to Handle Logic once trigger an action for the event
<?php declare(strict_types=1);
/**
 * Save Custom Field in sales_order before place an order.
 */

namespace Vendor\Module\Observer;

use Exception;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AddCustomField implements ObserverInterface
{
    /**
     * @param Observer $observer
     * @return $this;
     * @throws Exception
     */
    public function execute(Observer $observer)
    {
        $event = $observer->getEvent();
        // Get Order Object
        /* @var $order \Magento\Sales\Model\Order */
        $order = $event->getOrder();
        // Get Quote Object
        /** @var $quote \Magento\Quote\Model\Quote $quote */
        $quote = $event->getQuote();

        if ($quote->getCustomField()) {
            $order->setCustomField($quote->getCustomField());
        }
        return $this;
    }
}
  1. Write in order email template {{var order.getCustomField()}}
2
  • I am already saving that value in order table. Is there a way to get value from 'quote' table? Commented Apr 13, 2021 at 13:09
  • Please check this link if it helps. link for email
    – Mital Shah
    Commented Apr 15, 2021 at 9:10

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.