4

I have an extension attribute on quote address called "subdistrict". The data is successfuly saved to quote_address. But when completing the order, these data are not saved to sales_order_address.

below is my fieldset.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
    <fieldset id="sales_convert_quote_address">
        <field name="subdistrict">
            <aspect name="to_order_address" />
        </field>
    </fieldset>
</scope>

and my extensions_attributes.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="subdistrict" type="string" />
</extension_attributes>
<extension_attributes for="Magento\Sales\Api\Data\OrderAddressInterface">
    <attribute code="subdistrict" type="string" />
</extension_attributes>

right now i am getting an error:

"Error occurred during "shipping_address" processing. Error occurred during "extension_attributes" processing. Error occurred during "subdistrict" processing. Invalid type for value: "array". Expected Type: "string"."

and here is some part of the quote content:

  ["city"]=>
  string(14) "Kab. Tangerang"
  ["firstname"]=>
  string(7) "Yohanes"
  ["lastname"]=>
  string(7) "Pradono"
  ["customAttributes"]=>
  array(1) {
    ["subdistrict"]=>
    array(2) {
      ["attribute_code"]=>
      string(11) "subdistrict"
      ["value"]=>
      string(8) "Balaraja"
    }
  }
  ["extension_attributes"]=>
  array(1) {
    ["subdistrict"]=>
    array(2) {
      ["attribute_code"]=>
      string(11) "subdistrict"
      ["value"]=>
      string(8) "Balaraja"
    }
  }
  ["same_as_billing"]=>

and my mixin.js

define([
    'jquery',
    'mage/utils/wrapper',
    'Magento_Checkout/js/model/quote'
], function ($, wrapper, quote) {
    'use strict';

    return function (setShippingInformationAction) {

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

            // you can extract value of extension attribute from any place (in this example I use custom_attributes approach)
            // Don: the actual data to be sent to Magento backend is inside 'extension_attributes'
            console.log(shippingAddress);
            shippingAddress['extension_attributes']['subdistrict'] = shippingAddress.customAttributes['subdistrict'];
            // pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
            return originalAction();
        });
    };
});

1 Answer 1

4

I finally be able to convert the subdistrict to order address. Aparantly, some tutorials out there put wrong information on the mixin js content. it should be likethis

    if (shippingAddress.customAttributes != undefined) {
        $.each(shippingAddress.customAttributes , function( key, value ) {

            if($.isPlainObject(value)){
                value = value['value'];
            }

            shippingAddress['customAttributes'][key] = value;
            shippingAddress['extension_attributes'][key] = value;

        });
    }

And I also have to manually convert quote address subdistrict to order address using Observer on event sales_model_service_quote_submit_before

    public function execute(\Magento\Framework\Event\Observer $observer)
{
    // copy shipping address subdistrict
    $quoteShippingAdress = $observer->getQuote()->getShippingAddress();
    $subdistrict = $quoteShippingAdress->getSubdistrict();
    $orderShippingAdress = $observer->getOrder()->getShippingAddress();
    $orderShippingAdress->setSubdistrict($subdistrict);

    // copy billing address subdistrict
    $quoteBillingAddress = $observer->getQuote()->getBillingAddress();
    $subdistrict = $quoteBillingAddress->getSubdistrict();
    $orderBillingAddress = $observer->getOrder()->getBillingAddress();
    $orderBillingAddress->setSubdistrict($subdistrict);

}

as additional info, Magento 2.1 fieldset.xml doesn't support extension_attributes fields so they have to be converted manually.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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