3

How to get country id/country name from selected shipping address in the checkout page.

lets assume if the customer has multiple shipping addresses, based on the shipping address choose, if country name is not United States we want to display some Text message on the top. (International Custom Duties........) enter image description here

for this i have followed @Sarfaraz bheda answer for creating hook.js

Magento2 - How to trigger onchange event on Country/region in Shipping Address

thanks

  • I have updated my code to resolve 1 issue when customer is logged in. – Sarfaraz bheda Jul 11 '18 at 12:42
  • Regarding saved address, you can add dependency of 'Magento_Customer/js/model/customer', and get customer current details. – Sarfaraz bheda Jul 11 '18 at 12:43
  • To get shipping address information you can add dependency of Magento_Checkout/js/model/quote magento.stackexchange.com/questions/148788/… – Sarfaraz bheda Jul 11 '18 at 12:47
  • did you resolve the issue. can you post the answer if you did? Thanks – Kris Wen Sep 20 '19 at 0:39
3

Include quote js like

    define([
        'Magento_Checkout/js/model/quote'
    ])

in custom js. And try

    var countryId = quote.shippingAddress().countryId;
    var country = countryData()[countryId].name;

it will give you country ID

| improve this answer | |
0

Thanks @anjali-patil for giving a huge hint, I'll try to post here what he meant in more detail.

In your your component js file there is most likely a define(['A', 'B', 'C'], (A, B, C) => ...) call which is an AMD way to require files your script depends on. Here you can add a dependency to 'Magento_Checkout/js/model/quote' (if not already). quote.js file provides you the info about the current quote, including shipping address (via shippingAddress() method).

Example:

define([
    'some/module.js', 'some/otherModule.js', 'Magento_Checkout/js/model/quote'
], ( someModule     ,  someOtherModule     ,  quote) => {
    console.log(quote.shippingAddress());
})

It will output something like this:

{
    "customerAddressId": "1",
    "countryId": "LV",
    "regionId": "368",
    "regionCode": "Aizputes novads",
    "region": "Aizputes novads",
    "customerId": "1",
    "street": ["Maskavas 379, 33"],
    "telephone": "456456",
    "postcode": "LV-1063",
    "city": "Riga",
    "firstname": "Artur",
    "lastname": "Klesun"
}
| improve this answer | |

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.