SalesforceCommerceCloud / pwa-kit

React-based JavaScript frontend framework to create a progressive web app (PWA) storefront for Salesforce B2C Commerce.
https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/pwa-kit-overview.html
BSD 3-Clause "New" or "Revised" License
278 stars 130 forks source link

[BUG] Baskets not localized for guest shoppers #871

Open johnboxall opened 1 year ago

johnboxall commented 1 year ago

Currently, OCAPI calls to baskets do not include the locale parameter.

In multi-locale sites, guests browsing a non-default locale will see incorrectly localized product descriptions when looking at their basket.

To reproduce:

  1. Open https://pwa-kit.mobify-storefront.com/global/en-GB/product/25695327M?size=9SM&pid=701644153083M&color=JJI15XX
  2. Add the product to your cart
  3. Open the german cart: https://pwa-kit.mobify-storefront.com/global/de-DE/cart

Expect product details in German, but observe they are in English:

Screenshot 2022-12-22 at 9 14 24 AM

This issue also likely impacts OcapiShopperOrders, which may result in order confirmation emails being sent using the wrong locale.


To resolve, we'll likely need to include locale information in all OCAPI basket calls: https://github.com/SalesforceCommerceCloud/pwa-kit/blob/a0c629acf3b64e680c04956cc145eafbea81b211/packages/template-retail-react-app/app/commerce-api/ocapi-shopper-baskets.js

vcua-mobify commented 1 year ago

Thanks for submitting this @johnboxall . I've added a ticket in the backlog for fixing this

ValentinGurkov commented 1 year ago

Based on the discussion here https://sfcc-unofficial.slack.com/archives/CBB7YAAHW/p1676628824576879 and the response I got from SFCC Support, the OCAPI baskets endpoints do not respect the locale parameter.

The relevant case number is 44187657.

What we did (maybe there's a better approach) is modify useBasket's _productItemsDetail into a localized object.

What we changed::

// import useMultiSite hook
// import {merge} from 'lodash-es'
......
export default function useBasket(opts = {}) {
    ...
    const {locale} = useMultiSite()
    .....
    get _productItemsDetail() {
        return basket?._productItemsDetail?.[locale]
    },
    .....
    async getProductsInBasket(ids, options) {
        .....
        const newData = {
            [locale]: itemDetail
        }

        const updatedBasket = {
            ...basket,
            _productItemsDetail: merge(basket._productItemsDetail, newData)
        }
    .....
.....

Then getProductsInBasket should run whenever the locale is changed (this is done for us by the OOTB useShooper hook but our country selection logic may differ since our locale change is all done single page).

shopperProducts in commerce-api/index should also be configured to send the locale in API calls.