0

I am getting a zip code from the user when customer visit the website first time and saved the zip code using cookie.

I want to automatically set the zip code value saved in the cookie to checkout page shipping address postcode field.

How can i achieve this task.Please help me.strong text

0

To archive this, you can fallback from your theme to Magento_Checkout/js/checkout-data.js and change the function initData to add your default postcode value, as you can see here:

    /**
     * @return {*}
     */
    initData = function () {
        return {
            'selectedShippingAddress': null, //Selected shipping address pulled from persistence storage
            'shippingAddressFromData': {
                postcode: window.defaultCustomerZipCode
            }, //Shipping address pulled from persistence storage
            'newCustomerShippingAddress': null, //Shipping address pulled from persistence storage for customer
            'selectedShippingRate': null, //Shipping rate pulled from persistence storage
            'selectedPaymentMethod': null, //Payment method pulled from persistence storage
            'selectedBillingAddress': null, //Selected billing address pulled from persistence storage
            'billingAddressFromData': null, //Billing address pulled from persistence storage
            'newCustomerBillingAddress': null //Billing address pulled from persistence storage for new customer
        };
    },

In this case, I'm taking the value from a custom block and putting there as a default value this way:

Create a layout file checkout_index_index.xml in your module

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block as="zipcode" class="Ziru\CustomerZipCode\Block\ZipCode" name="zipcode" template="Ziru_CustomerZipCode::zipcode.phtml"/>
        </referenceContainer>
    </body>
</page>

An then in the .phtml file you can call your block and add the value to the window object.

<?php
/**
 * @var $block \Ziru\CustomerZipCode\Block\ZipCode
 */
?>
<div id="zip-code-details">
    <script>
        window.defaultCustomerZipCode = "<?= $block->getDefaultCustomerZipCode() ?>"
    </script>
</div>

To take a way Magento will apply any value in

'shippingAddressFromData': {
   postcode: window.defaultCustomerZipCode
 },

So feel free to have your value there following any logic that you want. And also, Magento will save postcode in the session as soon as the user changes this value. It means when the page gets refreshed the last value will be applied.

Lemme know if you could archive your challenger o/

  • thank you for your valuable time, I will try your solution and comeback. – Mujahidh Jan 24 at 5:03

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.