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
25 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 340 872

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
7 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
100 14 27

 

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
7 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
7 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
100 14 27

Did you ever get this figured out?