0

I'm trying to get the postcode that has been entered in the Estimate Shipping block on the basket page. I am trying to grab it from local storage. I have the following code on a template file that is called on the homepage:

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

This one returns an error in the console: quote.shippingAddress(...) is null

I have also tried to retrieve it via the cart session:

...
use Magento\Checkout\Model\Cart;
...

public function __construct(
    Cart $cart,) {
    $this->cart = $cart;
}

public function getCustomerDeliveryPostcode() {
    $quote = $this->cart->getQuote();
    $shippingAddress = $quote->getShippingAddress();
    $postcode = $shippingAddress->getPostcode();
}

This one returns nothing. Can anyone see what is wrong? Magento 2.3.0

0

Try below code

protected $cartSess;

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

public function getPostcode()
{
    $quote =$this->cartSess->getQuote();
    foreach ($quote->getAllAddresses() as $address) {
       $postcode = $address->getPostCode();
    }

    return $postcode;

}

I hope it will help...!!!

  • Thank you but it did not work. The full address information has not been entered in the Estimate Shipping block which is probably why this method doesn't work. – sulman Aug 13 at 9:08

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.