3

I want to change the country in estimate shipping (cart page) programatically in my module (meaning user clicks on a button in footer on homepage and I change the shipping country on cart page for that session):

enter image description here

However it doesn't work if I do it with the session object.

// Magento\Checkout\Model\Session;

$quote = $this->session->getQuote()
$quote->getShippingAddress()->setCountryId('GB')->save();
$quote->getShippingAddress()->getCountryId() // returns GB which is right, but the sidebar on frontend is still showing US on refresh. 

3 Answers 3

1

This needs to be done with js. Something like:

var countryData = customerData.get('checkout-data');
            if (countryData()) {
                if (countryData().shippingAddressFromData) {
                    countryData().shippingAddressFromData.country_id = "GB";
                } else {
                    countryData().shippingAddressFromData = {};
                    countryData().shippingAddressFromData.country_id = "US;
                }
                customerData.set('checkout-data', countryData());
            }
0

This should be $quote->setShippingAddress()->getCountryId()->save();

1
  • not working, tried also $quote->setShippingAddress()->setCountryId("GB")->save() Commented Apr 11, 2017 at 18:45
0

use this: $quote->getShippingAddress()->setCountryId("GB")->save()

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.