| @bigcommerce/catalyst-core | minor |
|---|
Add out-of-stock / backorder message to product cards on PLPs based on store settings:
- Add out of stock message if the product is out of stock and stock is set to display it.
- Add the backorder message if the product has no on-hand stock and is available for backorder and the store/product is set to display the backorder message
For existing Catalyst stores, the simplest way to get the newly added feature is to rebase the existing code with the new release code. The files that will be updated are listed below.
If you prefer not to rebase or have made customizations that prevent rebasing, follow these manual steps:
Add the inventory fields to your product card fragment in core/components/product-card/fragment.ts under Product:
inventory {
hasVariantInventory
isInStock
aggregated {
availableForBackorder
unlimitedBackorder
availableOnHand
}
}
variants(first: 1) {
edges {
node {
entityId
sku
inventory {
byLocation {
edges {
node {
locationEntityId
backorderMessage
}
}
}
}
}
}
}Update the Product interface in core/vibes/soul/primitives/product-card/index.tsx adding the following field to it:
inventoryMessage?: string;
Modify core/data-transformers/product-card-transformer.ts to include inventory message in the transformed data. You can simply copy the whole file from this release as it does not have UI breaking changes.
Update core/vibes/soul/primitives/product-card/index.tsx layout to display the new inventoryMessage product field.
Add inventory settings queries to the pages data. Add the following query to the main GQL query under site.settings:
inventory {
defaultOutOfStockMessage
showOutOfStockMessage
showBackorderMessage
}
to the following page data files:
core/app/[locale]/(default)/(faceted)/brand/[slug]/page-data.tscore/app/[locale]/(default)/(faceted)/category/[slug]/page-data.tscore/app/[locale]/(default)/(faceted)/search/page-data.tscore/app/[locale]/(default)/page-data.ts
Update the corresponding page components to use the productCardTransformer method (if not already using it) to get the product card, and pass inventory data to those product cards based on the store inventory settings. Use the following code while retrieving the product lists:
const { defaultOutOfStockMessage, showOutOfStockMessage, showBackorderMessage } =
data.site.settings?.inventory ?? {};
return productCardTransformer(
featuredProducts,
format,
showOutOfStockMessage ? defaultOutOfStockMessage : undefined,
showBackorderMessage,
);
in the following files:
core/app/[locale]/(default)/(faceted)/brand/[slug]/page.tsxcore/app/[locale]/(default)/(faceted)/category/[slug]/page.tsxcore/app/[locale]/(default)/(faceted)/search/page.tsxcore/app/[locale]/(default)/page.tsx
core/app/[locale]/(default)/(faceted)/brand/[slug]/page-data.tscore/app/[locale]/(default)/(faceted)/brand/[slug]/page.tsxcore/app/[locale]/(default)/(faceted)/category/[slug]/page-data.tscore/app/[locale]/(default)/(faceted)/category/[slug]/page.tsxcore/app/[locale]/(default)/(faceted)/search/page-data.tscore/app/[locale]/(default)/(faceted)/search/page.tsxcore/app/[locale]/(default)/page-data.tscore/app/[locale]/(default)/page.tsxcore/components/product-card/fragment.tscore/data-transformers/product-card-transformer.tscore/vibes/soul/primitives/product-card/index.tsx