cancel
Showing results for 
Search instead for 
Did you mean: 

Extention Attributes in payload return as null in backend

SOLVED

Extention Attributes in payload return as null in backend

I am trying to implement Extention Attributes. I want to send the e-mail and a subscribe boolean to the backend in the checkout.

In ShippingInformationManagementPlugin the var_dump(getExtensionAttributes()) returns null. When I look in $addressInformation I also see the extention attributes as null.

model/shipping-save-processor/default.js

 define([
'ko',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/resource-url-manager',
'mage/storage',
'Magento_Checkout/js/model/payment-service',
'Magento_Checkout/js/model/payment/method-converter',
'Magento_Checkout/js/model/error-processor',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/action/select-billing-address',
'Magento_Checkout/js/model/shipping-save-processor/payload-extender',
'jquery'
], function (
ko,
quote,
resourceUrlManager,
storage,
paymentService,
methodConverter,
errorProcessor,
fullScreenLoader,
selectBillingAddressAction,
payloadExtender,
$
 ) {
'use strict';

return {
    /**
     * @return {jQuery.Deferred}
     */
    saveShippingInformation: function () {
        var payload;

        if (!quote.billingAddress()) {
            selectBillingAddressAction(quote.shippingAddress());
        }

        console.log('E-mail:', $('[name="username"]').val());
        console.log('Subscribe:', Boolean($('[name="newsletter-subscribe"]').attr('checked')));

        payload = {
            addressInformation: {
                shipping_address: quote.shippingAddress(),
                billing_address: quote.billingAddress(),
                shipping_method_code: quote.shippingMethod().method_code,
                shipping_carrier_code: quote.shippingMethod().carrier_code,
                extension_attributes: {
                    email: $('[name="username"]').val(),
                    subscribe: Boolean($('[name="newsletter-subscribe"]').attr('checked'))
                }
            }
        };

        payloadExtender(payload);

        fullScreenLoader.startLoader();

        return storage.post(
            resourceUrlManager.getUrlForSetShippingInformation(quote),
            JSON.stringify(payload)
        ).done(
            function (response) {
                quote.setTotals(response.totals);
                paymentService.setPaymentMethods(methodConverter(response['payment_methods']));
                fullScreenLoader.stopLoader();
            }
        ).fail(
            function (response) {
                errorProcessor.process(response);
                fullScreenLoader.stopLoader();
            }
        );
    }
};
 });

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\Checkout\Api\Data\ShippingInformationInterface">
    <attribute code="email" type="string" />
    <attribute code="subscribe" type="boolean" />
</extension_attributes>

Model/Checkout/ShippingInformationManagementPlugin.php

 

<?php

namespace BB\Checkout\Model\Checkout;

class ShippingInformationManagementPlugin
{
protected $quoteRepository;

/**
 * @var \Magento\Newsletter\Model\SubscriberFactory
 */
protected $subscriberFactory;

public function __construct(
    \Magento\Quote\Model\QuoteRepository $quoteRepository,
    \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
    \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory,
    \Magento\Framework\Event\Observer $observer
) {
    $this->quoteRepository = $quoteRepository;
    $this->subscriberFactory = $subscriberFactory;
    $this->extensionAttributesFactory = $extensionAttributesFactory;
    $this->observer = $observer;
}

/**
 * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
 * @param $cartId
 * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
 */
public function beforeSaveAddressInformation(
    \Magento\Checkout\Model\ShippingInformationManagement $subject,
    $cartId,
    \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
    $extAttributes = $addressInformation->getExtensionAttributes();

    $email = $extAttributes->getEmail();

    var_dump(getExtensionAttributes()); die();
}
}

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\Model\ShippingInformationManagement">
    <plugin name="save-in-quote" type="BB\Checkout\Model\Checkout\ShippingInformationManagementPlugin" sortOrder="10"/>
</type>
</config>

Am I missing something?

 

3 REPLIES

Re: Extention Attributes in payload return as null in backend

Hello,

 

Have you override js using require-config.js

 

If you added into theme, then ok.

 

When you click on next button check console and make sure extension attribute pass, if works then you will get that attributes.

 

 

Re: Extention Attributes in payload return as null in backend

public function beforeSaveAddressInformation(
    \Magento\Checkout\Model\ShippingInformationManagement $subject,
    $cartId,
    \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
    $extAttributes = $addressInformation->getShippingAddress()->getExtensionAttributes();

    $email = $extAttributes->getEmail();

    var_dump($extAttributes); die();
}
}
If Issue Solved, Click Kudos/Accept As solutions.

Re: Extention Attributes in payload return as null in backend

Apparently the only use of 

payloadExtender(payload);

in default.js is to empty out the extension_attributes.... Why Magento?!

 

/js/model/shipping-save-processor/payload-extender.js

define(function () {
    'use strict';

    return function (payload) {
        payload.addressInformation['extension_attributes'] = {};

        return payload;
    };
});


By deleting that it works.