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. 
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() – Claudiu Creanga Apr 11 '17 at 18:45

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.