cancel
Showing results for 
Search instead for 
Did you mean: 

Get postcode entered previously on Cart page

Get postcode entered previously on Cart page

Would it be possible to get postcode entered before and when reloaded the page?

I would like to do the similar thing to this below on Magento 2

$cart = Mage::getSingleton('checkout/cart');
$address = $cart->getQuote()->getShippingAddress()->getPostcode();

 

5 REPLIES 5

Re: Get postcode entered previously on Cart page

Hello @tvgarden

 

magento 2 store into local storage of browser that value

 

require(['jquery','Magento_Checkout/js/model/quote'], function($,quote){ 
            console.log(quote.shippingAddress().postcode)
})

so you need to above way to get that value.

 

Hope it will help you.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Get postcode entered previously on Cart page

Thank you.

 

So to use the value in PHP code I would need to make an ajax call?

Re: Get postcode entered previously on Cart page

Hello @tvgarden

 

yes right!!! In session, you will get value but sometimes not right.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Get postcode entered previously on Cart page

Do you mean we can get postcode from Customersession?

 

 

If I can I would like to get postcode before page load.

 

Also I would like to know which php function is called when a postcode is entered to estimate shipping? 

Re: Get postcode entered previously on Cart page

You can get postcode using below way,

<?php

    public function __construct(
        \Magento\Checkout\Model\Cart $_cart
    )
    {
        $this->_cart = $_cart;
    }
    
    public function execute(Observer $observer)
    {
        $quote           = $this->_cart->getQuote(); 
        $postcode = $quote->getShippingAddress()->getPostcode();
    }
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial