cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 checkout page how to get postcode from selected shipping addresses?

SOLVED

Magento 2 checkout page how to get postcode from selected shipping addresses?

Hello All,

 

How to get postcode/zipcode from selected shipping address in the checkout page in magento 2.

7wSR3.png

Certified Magento 2 Developer
Vivek Singh
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 checkout page how to get postcode from selected shipping addresses?

Hello @Vivek Singh

For this first you have to override core functionality in your custom module Go to Namespace/ModuleName/view/frontend/requirejs-config.js

var config = {    map: {
        '*': {

            'Magento_Checkout/js/model/shipping-service' : 
            'NameSpace_ModuleName/js/model/shipping-service'      
        }
  }
};

and Go to Namespace/ModuleName/view/frontend/web/js/model/shipping-service.js

/*global define, url*/define(
        [
            'jquery', // For jQuery Added
            'Magento_Checkout/js/model/quote', // For Quote Added
            'ko',
            'Magento_Checkout/js/model/checkout-data-resolver'
        ],
        function ($, quote, ko, checkoutDataResolver) {
            "use strict";
            var shippingRates = ko.observableArray([]);
            return {                isLoading: ko.observable(false),
                /**
                 * Set shipping rates
                 *
                 * @param ratesData
                 */                setShippingRates: function (ratesData) {
                    if(loggedinCustomer == 1){
                        var address = quote.shippingAddress();

                     // you can get zipcode of current shipping address here
                    var zipcode = (address.postcode);

                    // STOP TO REMOVE ERROR MESSAGE FOR DHL IF SG                    shippingRates(ratesData);                    shippingRates.valueHasMutated();                    checkoutDataResolver.resolveShippingRates(ratesData);
                },

                /**
                 * Get shipping rates
                 *
                 * @returns {*}
                 */                getShippingRates: function () {
                    return shippingRates;
                }
            };
        }
);

And run setup:upgrade, setup:static-content:deploy commands

 

 

If found my answer useful? Please give Kudos and Accept it as solution! 

Magento Developer
Ankita Biswas

View solution in original post

2 REPLIES 2

Re: Magento 2 checkout page how to get postcode from selected shipping addresses?

Hello @Vivek Singh

For this first you have to override core functionality in your custom module Go to Namespace/ModuleName/view/frontend/requirejs-config.js

var config = {    map: {
        '*': {

            'Magento_Checkout/js/model/shipping-service' : 
            'NameSpace_ModuleName/js/model/shipping-service'      
        }
  }
};

and Go to Namespace/ModuleName/view/frontend/web/js/model/shipping-service.js

/*global define, url*/define(
        [
            'jquery', // For jQuery Added
            'Magento_Checkout/js/model/quote', // For Quote Added
            'ko',
            'Magento_Checkout/js/model/checkout-data-resolver'
        ],
        function ($, quote, ko, checkoutDataResolver) {
            "use strict";
            var shippingRates = ko.observableArray([]);
            return {                isLoading: ko.observable(false),
                /**
                 * Set shipping rates
                 *
                 * @param ratesData
                 */                setShippingRates: function (ratesData) {
                    if(loggedinCustomer == 1){
                        var address = quote.shippingAddress();

                     // you can get zipcode of current shipping address here
                    var zipcode = (address.postcode);

                    // STOP TO REMOVE ERROR MESSAGE FOR DHL IF SG                    shippingRates(ratesData);                    shippingRates.valueHasMutated();                    checkoutDataResolver.resolveShippingRates(ratesData);
                },

                /**
                 * Get shipping rates
                 *
                 * @returns {*}
                 */                getShippingRates: function () {
                    return shippingRates;
                }
            };
        }
);

And run setup:upgrade, setup:static-content:deploy commands

 

 

If found my answer useful? Please give Kudos and Accept it as solution! 

Magento Developer
Ankita Biswas

Re: Magento 2 checkout page how to get postcode from selected shipping addresses?

hi, i very noob in this, but how i call the postcode with you code after implement