Buyer Identity CountryCode isn't updating from Country Selection

Buyer Identity CountryCode isn't updating from Country Selection

awef194586
Tourist
6 0 2

I implemented a countryselector for US and SG, which seems to be working on all fronts except for updating the buyeridentity in the cart. When I select US, the buyeridentity in the Cart appears to be in SG still. As a result, when I add a US item to cart, it properly sends an optimistic update to cart with the correct cartlineitem, but the server seems to reject it, and the item disappears after a second or two in the cart aside. When I log the cart in CartMain, with 0 items in the cart, the countrycode is set to SG, but when the optimistic update is posted to the server, it briefly changes to US with totalquantity set to 1; upon rejection, it changes back to SG with totalquantity set to 0. 

This is confusing, as I believe I followed the steps to update the cart buyer identity as shown here (https://shopify.dev/docs/storefronts/headless/hydrogen/markets/country-selector) and I also tried to unset the previous cart after updating the cart buyer identity (await session.unset('cartId');)

 
const cartId = await session.get('cartId');
 
This is how I implemented the update to the cart buyerIdentity in ($locale).jsx (as well as root.jsx):
// If a cart exists, fetch its current buyer identity and update if needed
if (cartId) {
const currentCart = await cart.get();
const currentCountry = currentCart?.buyerIdentity?.countryCode;
// console.log(
// 'Current buyer identity:',
// currentCart?.buyerIdentity?.countryCode,
// );

// Compare with the country from the URL locale
if (currentCountry !== locale.country) {
// Update the cart's buyer identity with the new country code
await updateCartBuyerIdentity(context, {
cartId,
buyerIdentity: {countryCode: locale.country},
});
// Optionally, you can clear the cartId from the session so a new cart is created:
// await session.unset('cartId');
}
}
 
Replies 0 (0)