Skip to content

How to save extension_attributes from checkout for guesst user?

I have created a custom checkout field to choose the store to pickup the orders. This extension attribute is saved only for Logged In Customers, Not for Guest checkout.

I created the attribute using below code.

db_schema.xml

<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="quote_address" resource="default" engine="innodb">
    <column name="pickup_store_name" nullable="true" xsi:type="varchar" comment="Pickup Store Name" length="500"/>
</table>
<table name="quote" resource="default" engine="innodb">
    <column name="pickup_store_name" nullable="true" xsi:type="varchar" comment="Pickup Store Name" length="500"/>
</table>
<table name="sales_order" resource="default" engine="innodb">
    <column name="pickup_store_name" nullable="true" xsi:type="varchar" comment="Pickup Store Name" length="500"/>
</table>

extensions_attribute.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoCheckoutApiDataShippingInformationInterface">
    <attribute code="pickup_store_name" type="string" />
</extension_attributes>

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="MagentoCheckoutModelShippingInformationManagement">
    <plugin name="bl_save_delivery_date_in_quote" type="AyakilCustomShippingMethodPluginMagentoQuoteModelShippingAddressManagement" sortOrder="1"/>
</type>

ShippingAddressManagement.php

...
public function beforeSaveAddressInformation(
    MagentoCheckoutModelShippingInformationManagement $subject,
    $cartId,
    ShippingInformationInterface $addressInformation
) {
    $this->logger->critical("In Side pluging");
    //$extAttributes = $address->getExtensionAttributes();
    $extAttributes = $addressInformation->getExtensionAttributes();

   //print_r($extAttributes);
    if (!empty($extAttributes)) {

        try {
            //$addressInformation->setPickupStoreName($extAttributes->getPickupStoreName());
            $this->logger->critical("In Side pluging".$extAttributes->getPickupStoreName());
            $quote = $this->quoteRepository->getActive($cartId);
            $quote->setPickupStoreName($extAttributes->getPickupStoreName());
            $quote->save();
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());
        }
    }
}

I am reaching up to $extAttributes->getPickupStoreName(), from the checkout its assigned from payload-extender-mixin.js file.

define([
'jquery',
'mage/utils/wrapper',
'underscore',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/action/create-shipping-address',
'Magento_Checkout/js/action/select-shipping-address',
'Magento_Checkout/js/checkout-data',
'uiRegistry',
'Ayakil_CustomShippingMethod/js/view/shipping-option-select',
], function (
$,
wrapper,
_,
quote,
createShippingAddress,
selectShippingAddress,
checkoutData,
registry,
storeData,
) {'use strict';

return function (payloadExtender) {
    return wrapper.wrap(payloadExtender, function (originalFunction, payload) {
        payload = originalFunction(payload);
        
        var method = quote.shippingMethod();
        var pickupStore = '';
        var newShippingAddress,
            addressData;
        if(method['carrier_code'] === 'storepickup' && $('[name="pickup_store_name"]').val() ){
            addressData = quote.shippingAddress();
            
          
            pickupStore = $('[name="pickup_store_name"]').val();
        }
        console.log(pickupStore);
        _.extend(payload.addressInformation, {
            extension_attributes: {
                'pickup_store_name': pickupStore
            }
        });
        return payload;
    });
};
});

This code segments works nicely for logged in users, But it does not work for guest users.

How can i save custom extension attributes value for guest users during guest checkout?