cancel
Showing results for 
Search instead for 
Did you mean: 

Can't get extension attributes in observer - Magento 2

Can't get extension attributes in observer - Magento 2

As the title says

Background: I have followed the guide in the Magento 2.1 docs about adding a custom input to Shipping Address in checkout:http://devdocs.magento.com/guides/v2.1/howdoi/checkout/checkout_new_field.html

I am confident I followed the instructions to a T. The input was renamed to 'birthday' from 'custom_field'.

I have an observer attached to sales_order_save_before. I am trying to access 'birthday' as a custom attribute, but when I call

 

$birthday = $address->getExtensionAttributes()->getBirthday();

I get an error saying 'Uncaught Error: Call to a member function getBirthday() on null'. I know that the interface is being generated properly. Is there another way I can grab this information?

Related code:

Block/Checkout/LayoutProcessor:Smiley Tonguerocess()

 

public function process($jsLayout) {

    $customAttributeCode = 'birthday';
    $customField = [
        'component' => 'Magento_Ui/js/form/element/abstract',
        'config' => [
            'customScope' => 'shippingAddress.custom_attributes',
            'customEntry' => null,
            'template' => 'ui/form/field',
            'elementTmpl' => 'ui/form/element/date'
        ],
        'dataScope' => 'shippingAddress.custom_attributes' . '.' . $customAttributeCode,
        'label' => 'Birthday (MM/DD/YYYY)',
        'provider' => 'checkoutProvider',
        'sortOrder' => 1000,
        '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;
}

 

etc\frontend\di.xml

 

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="birthdayCheckout" xsi:type="object">Vendor\Module\Block\Checkout\LayoutProcessor</item>
            </argument>
        </arguments>
    </type>
</config>

 

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

Mixin code

 

/*jshint browser:true jquery:true*/
/*global alert*/
define([
    'jquery',
    'mage/utils/wrapper',
    'Magento_Checkout/js/model/quote'
], function ($, wrapper, quote) {
    'use strict';


    /*
     add file upload logic here
     */
    return function (setShippingInformationAction) {

        return wrapper.wrap(setShippingInformationAction, function (originalAction) {
            var shippingAddress = quote.shippingAddress();
            if (shippingAddress['extension_attributes'] == undefined) {
                shippingAddress['extension_attributes'] = {};
            }

            shippingAddress['extension_attributes']['birthday'] = shippingAddress.customAttributes['birthday'];
            // pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
            return originalAction();
        });
    };

});