7

How we can get the value of postcode from selected shipping addresses on checkout page if a customer has multiple addresses?

enter image description here

1
  • $this->getQuote()->getShippingAddress()->getPostcode(); Oct 25, 2018 at 4:52

4 Answers 4

7

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

1
  • got an error like Uncaught SyntaxError: In strict mode code, functions can only be declared at top level or inside a block. console-output-handler.js:34 [2019-02-07 02:59:26] [ERROR] Failed to load the "Magento_Checkout/js/view/shipping" component. Feb 7, 2019 at 9:34
5

You can try this way.

protected $_checkoutSession;

public function __construct (
    \Magento\Checkout\Model\Session $_checkoutSession
    ) {
    $this->_checkoutSession = $_checkoutSession;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
  $postCode = $this->_checkoutSession->getQuote()-getShippingAddress()->getPostcode();

}

Let me know if it doesn't work.

2
  • 1
    Thank you for your reply. But i have tried this but it gives only postcode of default shipping address. If we select another shipping address then also it gives the default one. Oct 25, 2018 at 4:42
  • Ok let me check Oct 25, 2018 at 7:43
2

Define cart model in helper class & put this below code in it.
Call it in your observer or plugin method it will give you latest pincode.

public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Checkout\Model\Cart $cart,
    ) {
    $this->cart = $cart;
    parent::__construct($context);
}

public function getPostcode(){
   $this->cart->getQuote()->getShippingAddress()->getPostcode();
}
0

You can get address object of selected shipping address from default.js located in this location

vendor/magento/module-checkout/view/frontend/web/js/view/shipping-address/address-renderer/default.js

there is one function initObservable in this function you will get shipping address object of selected shipping address.

1
  • In this "initObservable" function the " shipping address object" gets null , just after adding new billing address. Apr 6, 2020 at 7:50

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.