Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Re: Detect shipping country change event on checkout

Detect shipping country change event on checkout

kristinam
Shopify Partner
5 0 0

Hi, I have created a checkout extension to add a new custom field on checkout. Now I need to show or hid that field based on shipping country in checkout. How can I detect shipping country change event within new checkout extensibility functions?

Replies 8 (8)

jamalali81
Shopify Partner
26 0 4

Try this:

export default extension('your.extension.target', (root, api) => {
api.shippingAddress.subscribe(() =>
yourMethodToHandleChange()
);
});

 

May I ask how you added the custom field and where exactly you added it?

 

Do you know how to dynamically update address fields. I have tried this but noting seems to be happening:

await api.applyShippingAddressChange({
type: 'updateShippingAddress',
address: {
zip: myNewZip
}
});

Liam
Community Manager
3108 344 910

If this is a cart/checkout validation function, you should be able to read the countryCode.

 

If you're working with extensions, there’s auseShippingAddress()method that returns the shipping address, with country included.

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Zobekdev
Shopify Partner
5 0 0

Hi Liam, i'm a little confused, how i can detect when the user write something in the input field, it's possible in the checkout extensibility api? i been reviewed all the hooks and i can't found something useful for this case.

LuckyLuc
Shopify Partner
10 0 1

Hi, I am stuck with the same issue. I would like to know if there is an event that triggers when the address changes.
Can you provide a simple example please?

Thanks

Schmidtc63
Shopify Partner
105 14 37

 

export default extension(
    `purchase.checkout.shipping-option-list.render-before`,
    async (root, api) => {
        const {shippingAddress} = api;
        let currentShippingCountry=shippingAddress.current.countryCode;
        shippingAddress.subscribe(async (newShippingAddress) => {
            if (currentShippingCountry !== newShippingAddress.countryCode) {
               currentShippingCountry = newShippingAddress.countryCode;
               // do what you need to do
            }
        });
});

 

LuckyLuc
Shopify Partner
10 0 1

Hi, do you have an example for detecting when the "Billing" address changes ? 
I tried changing shippingAddress to billingAddress in your code but it's not working for some reason

thanks for your help

LuckyLuc
Shopify Partner
10 0 1

Nevermind! It Works!!! 

guess I needed to refresh something or the browser had to update. 

 

Anyway, thanks Schmidtc63 !! You Rock!! 🙂

Schmidtc63
Shopify Partner
105 14 37

Did you ever get this figured out?