Update Product JSON

This is helpful if you're using Shopify Markets and you'd like to translate product and variant titles within Rebuy widgets. Items in cart will be translated automatically so no additional code is needed to translate titles of cart items.

It is important to run this code within the productsChange event listener, as this event listener will allow the fetching of product JSON to be done at the correct time. This event will run when products are loaded within the widget on page load, as well as when the products inside the widget change (if an item is added to cart from the widget, or the data source is reevaluated and products within the widget change due to new data source response).

<!-- Utilize StorefrontHelper to enrich product JSON with locale-aware titles -->
<script>
  // update product JSON every time products are loaded or changed
  document.addEventListener('rebuy.productsChange', (e) => {
    const widget = e.detail.widget;
    const storefrontHelper = new ShopifyStorefrontHelper();

    // loop over products in the widget and enrich product json
    widget.data.products.forEach((product) => storefrontHelper.updateProductJSON(product));
  });
</script>