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::process()

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>
    <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
        <attribute code="file" 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();
        });
    };

});
4
  • 1
    is $address taken from quote or order? If you see the value send in the request in address extension_attributes then it will be place in quote address entity and most likely not automatically copied to order address.
    – Zefiryn
    Mar 12, 2017 at 8:58
  • Address is taken from quote, but I've tried getting it from the order as well Mar 12, 2017 at 11:29
  • How the value looks in the request being send to the server? Is it really a string? You should also make sure that cache has been cleared and var/di and var/generation were removed, for safe do it manually once again.
    – Zefiryn
    Mar 12, 2017 at 13:23
  • Input is Magentos date element. It's really just a fancy text input. So I'm positive it is being sent as a string Mar 12, 2017 at 18:51

1 Answer 1

1

You need a requirejs-config.js to run your mixin.

view/frontend/requirejs-config.js

var config = {
"map": {
    "*": {
        "Magento_Checkout/js/model/shipping-save-processor/default" : "Vendor_Module/js/shipping-save-processor-default-override"
    }
}

};

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.