Store and buyer context in custom components
Key Takeaway
Get the buyer's account from the effective account ID data expression, not from the current user's ID.
A custom storefront component often needs to know who the buyer is, which account they are buying for, and which site it runs in, so it can show the right data. In a B2B store built with LWR templates, this context comes from Storefront APIs (commerce/contextApi and commerce/effectiveAccountApi), from @salesforce modules for the user and site, and from data expressions such as {!User.Commerce.EffectiveAccount.id}. These built-in sources avoid extra server calls.
The buyer's account and buyer groups
A custom component in a B2B store built with LWR templates should get the buyer's account from the effective account ID data expression, {!User.Commerce.EffectiveAccount.id}. Data expressions work on any LWR store page. When a guest shopper views the page, this expression returns the ID of the guest buyer profile.
Don't use the current user's ID in place of the account. The @salesforce/user/Id module imports that ID, which identifies the Salesforce user. A user ID is not an account ID, so account-based lookups with it return nothing.
Lena buys for Harbor Foods and opens a store page with a custom banner that shows her account's delivery region. The banner looks up the account with the effective account ID, so the lookup receives the Harbor Foods account ID and finds the record. If the banner used Lena's user ID instead, the lookup would search accounts for an ID that belongs to a user and return nothing. When a guest shopper opens the same page, the effective account ID is the ID of the guest buyer profile.
In JavaScript, the commerce/effectiveAccountApi bundle exposes an effectiveAccount object. effectiveAccount.update(accountId, accountName) sets the effective account ID and name in session storage. effectiveAccount.accountId and effectiveAccount.accountName read them back.
Another data expression, {!User.Commerce.BuyerGroups}, returns the list of buyer groups associated with the user.