1

I have created a custom field on checkout before shipping method like this: enter image description here

etc/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">        
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="CCOrderEmail_LayoutProcessorPlugin" type="Tekglide\CCOrderEmail\Plugin\LayoutProcessorPlugin"/>
    </type>      
</config>

LayoutProcessorPlugin.php :

class LayoutProcessorPlugin {

    public function afterProcess(
    \Magento\Checkout\Block\Checkout\LayoutProcessor $subject, $jsLayout
    ) {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['before-form']['children']['cc_order_email'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'options' => [],
                'id' => 'cc-order-emailaddress'
            ],
            'dataScope' => 'shippingAddress.custom_attributes.cc_order_email',
            'label' => 'CC Email Address',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => [],
            'sortOrder' => 250,
            'id' => 'cc-order-emailaddress'
        ];
    
    
        return $jsLayout;
    }

}


extension_attributes.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="cc_order_email" type="string"/>
    </extension_attributes>
</config>

Setup/UpgradeSchema.php :

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * Upgrades DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $quoteAddressTable = 'quote';
        $orderTable = 'sales_order';

        //Quote address table
        $setup->getConnection()
            ->addColumn(
                $setup->getTable($quoteAddressTable),
                'cc_order_email',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'CC Order Email'
                ]
            );
        //Order address table
        $setup->getConnection()
            ->addColumn(
                $setup->getTable($orderTable),
                'cc_order_email',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'CC Order Email'

                ]
            );

        $setup->endSetup();
    }
}

Observer/OrderPlaceAfter.php :

<?php
namespace Tekglide\CCOrderEmail\Observer;

use Psr\Log\LoggerInterface;
use Magento\Framework\App\Area;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Translate\Inline\StateInterface;
use Tekglide\RequestAQuote\Mail\Template\TransportBuilder;
 
class OrderPlaceAfter implements \Magento\Framework\Event\ObserverInterface {

    /**
     *
     * @var \Magento\Checkout\Model\Session
     */
    private $checkoutSession;

    /**
     *
     * @var \Extroniks\CheckoutFields\Model\Order\FieldFactory
     */
    private $orderFieldFactory;

    /**
     *
     * @var \Extroniks\CheckoutFields\Helper\Data
     */
    private $helper;

    public function __construct(
        StoreManagerInterface $storeManager,
        TransportBuilder $transportBuilder,
        StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        LoggerInterface $logger,
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->storeManager = $storeManager;
        $this->transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->logger = $logger;
        $this->checkoutSession   = $checkoutSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $order          = $observer->getEvent()->getOrder();
        $ccOrderEmail = $this->checkoutSession->getData('cc_order_email'); // get cc email value

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $emailReceiver = $this->scopeConfig->getValue($ccOrderEmail, $storeScope);

        $vars = array(
            'store' => 1,
            'orderNumber' => $order->getIncrementId(),
            'customer_name' => $order->getCustomerName(),
            'items'=> $order->getAllItems(),
        );
 
        // set from email
        $sender = $this->scopeConfig->getValue('trans_email/ident_sales/email', $storeScope);
 
        $this->inlineTranslation->suspend();
        $transport = $this->transportBuilder->setTemplateIdentifier(10, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)->setTemplateOptions(
            [
                'area' => Area::AREA_FRONTEND,
                'store' => 1
            ]
        )->setTemplateVars(
            $vars
        )->setFrom(['name' => $sender,'email' => $sender]
        )->addTo(
            $emailReceiver
        )->getTransport();
 
        try {
            $transport->sendMessage();
        } catch (\Exception $exception) {
            $this->logger->critical($exception->getMessage());
        }
        $this->inlineTranslation->resume();
 
    }

}


I am not able to get the value of cc email address field in my observer. Any help would be appreciated.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.