Product Hydration
Last updated
Was this helpful?
Was this helpful?
// Helper method to convert a remote product to a Firework product object.
function buildFWProduct(remoteProduct) {
const { id, name, variations } = remoteProduct;
// Use `shopping.productFactory` to build Firework product object
return window._fwn.shopping.productFactory((product) => {
// 1. Update the product title.
product
.extId(id)
.name(name);
// 2. Update product variant price and availability.
for (const remoteVariant of variations) {
const {
variation_id,
is_in_stock,
display_price,
} = remoteVariant;
product.variant((variant) => {
variant
.extId(variation_id)
.isAvailable(is_in_stock)
.price(display_price)
);
}
});
}
// Configure callback to hydrate the video product when the products are loaded.
// Fired for each video individually. Suitable for widgets rendering single video (fw-storyblock)
window._fwn.shopping.onProductsLoaded(async ({ products }) => {
const productIds = products.map((product) => product.produce_ext_id);
// Make a server request to get the latest product data.
const remoteProducts = await fetchProductsFromServer(productIds);
return remoteProducts.map((remoteProduct) => buildFWProduct(remoteProduct));
});// Alternativelly, widget level callback triggered as soon as all videos are loaded (or paginated).
// Suitable for widgets displaying multiple videos (fw-carousel, fw-player-deck)
window._fwn.shopping.onWidgetProductsLoaded(async ({ products }) => {
const productIds = products.map((product) => product.produce_ext_id);
// Make a server request to get the latest product data.
const remoteProducts = await fetchProductsFromServer(productIds);
return remoteProducts.map((remoteProduct) => buildFWProduct(remoteProduct));
});