Magento – cannot get extension attributes value

checkoutcustom-fieldmagento-2.1magento2PHP

The value can post

enter image description here

However,I cannot get Extension attributes on my plugin which intercept Checkout success controller code. getExtensionAttributes always return null

    <?php

namespace Genning\CheckoutCreateCustomer\Plugin\Onepage;

class Success 
{
    /**
     * Order success action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */

    protected $_storeManager;

    protected $_customerFactory;

    protected $_addressInformation;

    protected $_addressValue;

    protected $_customerSession;

    protected $_checkoutSession;

    protected $_logger;


    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        // \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation,
        \Magento\Quote\Api\Data\AddressInterface $addressValue,
        \Magento\Customer\Model\Session $session,
        \Magento\Checkout\Model\Session $checksession,
         \Psr\Log\LoggerInterface $logger

    ) {
        $this->_storeManager = $storeManager;
        $this->_customerFactory = $customerFactory;
        // $this->_addressInformation = $addressInformation;
        $this->_addressValue = $addressValue;
        $this->_customerSession = $session;
        $this->_checkoutSession = $checksession;
        $this->_logger = $logger;
    }




    public function aroundExecute(
        \Magento\Checkout\Controller\Onepage\Success $subject, 
        callable $proceed        
        )
    {

        if (!$this->_customerSession->isLoggedIn()) { 
        // Get Website ID
        $websiteId  = $this->_storeManager->getWebsite()->getWebsiteId();
        // $this->_logger->debug(var_export($websiteId, true));
        //get data
        // $extAttributes = $this->_addressInformation->getExtensionAttributes();
        $extAttributes2 = $this->_addressValue->getExtensionAttributes();
        // $this->_logger->debug(var_export($extAttributes2, true));
        // $password = $extAttributes->getPassword();
        // $sponsor_number = $extAttributes->getSponsorNumber();
        // $address = $this->_checkoutSession->getLastRealOrder()->getShippingAddress();
        // $customerEmail = $this->_checkoutSession->getLastRealOrder()->getCustomerEmail();
        // $firstName = $this->_checkoutSession->getLastRealOrder()->getShippingAddress()->getFirstname();
        // $lastName = $this->_checkoutSession->getLastRealOrder()->getShippingAddress()->getLastname();


        // // Instantiate object (this is the most important part)
        // $customer   = $this->_customerFactory->create();
        // $customer->setWebsiteId($websiteId);

        // // Preparing data for new customer
        // $customer->setEmail($customerEmail ); 
        // $customer->setFirstname($firstName);
        // $customer->setLastname($lastName);
        // $customer->setPassword($password);
        // $customerData = $customer->getDataModel();
        // $customerData->setCustomAttribute('sponsorvgnumber', $sponsor_number);
        // $customer->updateData($customerData);
        // $customer->setAddresses($address);
        // $storeId = $this->_storeManager->getWebsite($websiteId)->getDefaultStore()->getId();
        // $customer->setStoreId($storeId);
        // $storeName = $this->_storeManager->getStore($customer->getStoreId())->getName();
        // $customer->setCreatedIn($storeName);        

        // // Save data
        // $customer->save();
        }
        $returnValue = $proceed();
        return $returnValue;
    }
}

This is my attribute extension.xml

<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="password" type="string" />
        <attribute code="sponsor_number" type="string" />        
    </extension_attributes>
    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="password" type="string" />
        <attribute code="sponsor_number" type="string" />        
    </extension_attributes>    
</config>

my LayoutProcessor.php defining the custom fields in checkout step

            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['sposor_number'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'options' => [],
                'id' => 'sponsor_number'
            ],
            'dataScope' => 'shippingAddress.custom_attributes.sponsor_number',
            'label' => 'Sponsor Number',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => ['required-entry' => true,'validate-number' => true],
            'sortOrder' => 250,
            'id' => 'sponsor_number'
        ];
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['password'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'options' => [],
                'id' => 'password'
            ],
            'dataScope' => 'shippingAddress.custom_attributes.password',
            'label' => 'Password',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => ['required-entry' => true,'validate-admin-password' => true],
            'sortOrder' => 260,
            'id' => 'password'
        ];
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['cpassword'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'options' => [],
                'id' => 'cpassword'
            ],
            'dataScope' => 'shippingAddress.custom_attributes.cpassword',
            'label' => 'Confirm Password',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => ['required-entry' => true,'validate-cpassword' => true],
            'sortOrder' => 270,
            'id' => 'cpassword'
        ];   

my set-shipping-information-setting-mixin.js

    define([
    'jquery',
    'mage/utils/wrapper',
    'Magento_Checkout/js/model/quote',
    'Magento_Customer/js/model/customer'    

], function ($, wrapper, quote, customer) {
    'use strict';

    return function (setShippingInformationAction) {

        return wrapper.wrap(setShippingInformationAction, function (originalAction) {
            var shippingAddress = quote.shippingAddress();

            if (shippingAddress['extension_attributes'] === undefined) {
                shippingAddress['extension_attributes'] = {};
            }
            if(!customer.isLoggedIn()){
                shippingAddress['extension_attributes']['sponsor_number'] = shippingAddress.customAttributes['sponsor_number'];
                shippingAddress['extension_attributes']['password'] = shippingAddress.customAttributes['password'];
            }           
            // pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
            return originalAction();
        });
    };
});

Best Answer

You have typo in your code:

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

Should it be 'sponsor_number' ?

Related Topic