2

I am writing a custom shipping rates module, and I can’t seem to figure out how to get the estimate shipping Country ID.

I know how to get the Country id once the order has been created, but not for the estimate shipping portion of the checkout process

Any input is greatly appreciated!

For Magento 1.9 I used this

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id')

Or

Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getData('country_id'); 
  • Hi did you got the solution for this question – Deeban Babu Oct 6 '16 at 9:34
2

An interest fact about Magento2 That magento2 does not save shipping address address and billing address to quote until click on Place Order button at frontend.

Save Process

If you will check at database table quote_address during checkout,then you can understand.. Basically at browser local storage,Magento save shipping and billing address and save during click on place Order .

If you Digg on shipping estimation then you can understand that magento only need address attribute field country_id,region,region_id,city etc need for calculate the shipping rates .

At frontend checkout,Magento send address fields value from existing address or newly created address.

| improve this answer | |
1

Magento 2.3.5

 private $cart;

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

public function ()
{
   
    $estimated_country = $this->cart->getQuote()->getShippingAddress()->getCountryId();
}
| improve this answer | |
0

Inject \Magento\Checkout\Model\Session in your constructor.

/**
 * @var \Magento\Checkout\Model\Session
 */
protected $_checkoutSession;

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

}

And then, we can get:

 $this->_checkoutSession->getQuote()->getShippingAddress()->getCity();
 $this->_checkoutSession->getQuote()->getShippingAddress()->getCountryId();
 $this->_checkoutSession->getQuote()->getBillingAddress()->getCountryId();

UPDATE:

For the estimation shipping country, we can try:

/** @var \Magento\Quote\Api\Data\EstimateAddressInterface $estimatedAddress */
$estimatedAddress->getCountryId();
| improve this answer | |
  • Thanks for replay but this not working in custom module model, Call \Magento\Checkout\Model\Session using objectManager $countryId = $checkoutSession->getQuote()->getShippingAddress()->getCountryId(); it's return NULL – Ankur Bhadania Sep 12 '16 at 13:34
  • Currently, you login into your account? – Khoa TruongDinh Sep 12 '16 at 14:07
  • No i am not login into my account – Ankur Bhadania Sep 13 '16 at 5:02
  • I have just updated my answer, we should try: Magento\Quote\Api\Data\EstimateAddressInterface. – Khoa TruongDinh Sep 13 '16 at 13:03
  • it also return null data with login or logout – Ankur Bhadania Sep 14 '16 at 8:44
0

Instead of checkout session, I think you have to use checkout Cart

    protected $_checkoutCart;

    public function __construct(
        \Magento\Checkout\Model\Cart $checkoutCart
    )
    {
        $this->_checkoutCart = $checkoutCart;

    }

And Then :

    $this->_checkoutCart->getQuote()->getShippingAddress()->getData();
| improve this answer | |
  • With out login it return null , i used $this->_checkoutCart->getQuote()->getShippingAddress()->getCountryId(); – Ankur Bhadania Sep 13 '16 at 5:40

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.