Buyer identity set for Hydrogen cart overwritten in checkout

I’m running into a quirk setting up the B2B features in our Hydrogen store. I’ve followed the B2B cookbook and everything is working great from the Hydrogen side, however after sending to checkout the buyerIdentity on the cart is being overwritten with whatever the state of the checkout UI is.

For full context, we have:

This is the flow that gets into the situation I’m describing:

  1. When selecting location A in Hydrogen, adding products to cart, and then navigating to checkout, I’m initially redirected to a location selector under account.example.com
  2. Select location A in this selector, and it redirects back to checkout (all good)
  3. Using the link to change locations in the checkout, I switch to location B. Once redirected back to checkout I get presented with a message about stock problems with the items in the cart (all good, expected as B does not have access to the same product catalog)
  4. I then go back out to Hydrogen and select location A, rebuild the cart
  5. Navigate to checkout and am immediately given the stock problems message again, because checkout is still set to location B

My initial thought was that I wasn’t properly setting the buyerIdentity on the cart before navigating to checkout, but I confirmed with the GraphQL API after step 4 that the buyerIdentity on the cart is correctly set to location A. After loading into checkout in step 5 and checking the cart with GraphQL again, it now shows buyerIdentity changed to location B.

I’m sure I’m probably just missing something, but any help getting me in the right direction would be much appreciated!

Hey @JF_jarbo, that does sound odd indeed!

Would you be able to provide some additional information:

  • SFAPI Version
  • What SFAPI call you are using to set the company location
  • If you can share a store URL where this is happening it can help us debug better

Hi @andrew_nguyen1,

  • The SFAPI version is 2026-04
  • If you mean to set the company location on the cart, that’s just being done through the default cart.updateBuyerIdentity()
  • Unfortunately we haven’t pushed these updates live yet as we wanted to make sure we got this figured out before rolling out our B2B upgrades. I’d be happy to share code or anything else that would help, though!

Hi! Even if it’s not a live store, could you create a private deployment and give us a link or similar? Or your shop id + the deployment name so we can see the configuration. Thanks!

Sure thing, here’s the link to the preview deployment:
https://01ks57mjkbrxxzcvbx1kzz66th-787c3fb9014a044a6e94.myshopify.dev

This was picked up by support and we managed to get it working properly, posting here for anyone in the future!

The trick is to also route the buyer context update through Shopify’s company location update endpoint. With the B2B cookbook as an example, the flow would be:

// /routes/b2blocations.tsx
// ...
await customerAccount.setBuyer({ companyLocationId });

const buyer = await customerAccount.getBuyer();

const result = await cart.updateBuyerIdentity({
  companyLocationId,
  customerAccessToken: buyer?.customerAccessToken,
});

const headers = result?.cart?.id
  ? cart.setCartId(result.cart.id)
  : new Headers();

// This is the Hydrogen URL to redirect to after the location update
const returnTo = new URL('/cart', request.url).toString();

const updateUrl = new URL(
  '/company_location/update',
  'https://example.com', // This should be the domain pointing to Online Store
);

updateUrl.searchParams.set('location_id', companyLocationId);
updateUrl.searchParams.set('return_to', returnTo);

return redirect(updateUrl.toString(), { headers }); 

By redirecting to the Shopify side to update the location before redirecting back to Hydrogen, the buyer identity in checkout is kept in sync with Hydrogen.