0

Triggered "checkout_onepage_controller_success_action" event but custom checkout field value not getting in observer.

How can I save checkout page custom field data with other checkout details to a custom table using observer.

Created a "sms" custom field on checkout page

namespace EdmondsCommerce\Sms\Plugin;

use Magento\Checkout\Block\Checkout\LayoutProcessor;

class SmsPlugin
{
    public function afterProcess(LayoutProcessor $subject, $jsLayout) {
        $customAttributeCode = 'sms';
        $customField = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                // customScope is used to group elements within a single form (e.g. they can be validated separately)
                'customScope' => 'shippingAddress.custom_attributes',
                'customEntry' => null,
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input'
            ],
            'dataScope' => 'shippingAddress.custom_attributes' . '.' . $customAttributeCode,
            'label' => 'Custom SMS Attribute',
            'provider' => 'checkoutProvider',
            'sortOrder' => 0,
            'validation' => [
                'required-entry' => true
            ],
            'options' => [],
            'filterBy' => null,
            'customEntry' => null,
            'visible' => true,
        ];

        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'][$customAttributeCode] = $customField;

        return $jsLayout;
    }
}

Extension attribute code

<?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\Quote\Api\Data\AddressInterface">
        <attribute code="sms" type="string" />
    </extension_attributes>
</config>

event.xml

<?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="checkout_onepage_controller_success_action">
        <observer name="checkout_onepage_observer" instance="EdmondsCommerce\Sms\Observer\TestObserver" />
    </event>

Observer Code

namespace EdmondsCommerce\Sms\Observer;
use Magento\Framework\Event\ObserverInterface;
 use Magento\Framework\App\RequestInterface;

class TestObserver implements \Magento\Framework\Event\ObserverInterface
{
    protected $_checkoutSession;


    public function __construct(\Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) {


        $this->_checkoutSession = $checkoutSession;
        $this->_addressInformation = $addressInformation;

    }

    public function execute(\Magento\Framework\Event\Observer $observer){

        $order = $this->_checkoutSession->getLastRealOrder();


        $shippingAddress = $this->_addressInformation->getShippingAddress();
        $shippingAddressExtensionAttributes = $shippingAddress->getExtensionAttributes();
        if ($shippingAddressExtensionAttributes) {
            $sms = $shippingAddressExtensionAttributes->getSms();
            echo $sms;
            exit();

        }
    }
}
0

It is not Proper way for add custom field,also you missed js file to override Magento_Checkout/js/action/set-shipping-information.js

Check it here

Edit
1. It look like you are adding order attribute,so you have to save that value in quote and order table. 2. get that value from address extension attribute by plugin method.

<type name="Magento\Checkout\Model\ShippingInformationManagement">
      <plugin name="save_to_quote_table" type="EdmondsCommerce\Sms\Plugin\Quote\SaveToQuote" sortOrder="10"/>
   </type>
...
class SaveToQuote
{...
...
public function beforeSaveAddressInformation(
       \Magento\Checkout\Model\ShippingInformationManagement $subject,
       $cartId,
       \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
   ) {

       if(!$extAttributes = $addressInformation->getExtensionAttributes())
           return;
       $quote = $this->quoteRepository->getActive($cartId);

       $quote->setSms($extAttributes->getSms());
   }
}

in that function of class you have to set it in qoute then you can assign value to order from quote in your observer. May be it helps.(don't forget to compile)

  • 1
    js files also included, not mentioned above /app/code/EdmondsCommerce/Sms/view/frontend/web/js/action/set-shipping-information-mixin.js /app/code/EdmondsCommerce/Sms/view/frontend/requirejs-config.js – Ragesh VP Oct 18 '18 at 12:25
  • 1
    Please help me to solve the issue... I want to add a custom field on checkout page and save all the details to a custom table – Ragesh VP Oct 18 '18 at 12:29
  • i have update answer – Ketan Borada Oct 19 '18 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.