Hello All,
How to get postcode/zipcode from selected shipping address in the checkout page in magento 2.
Solved! Go to Solution.
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!
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!
hi, i very noob in this, but how i call the postcode with you code after implement