Skip to content

Commit 6c7305c

Browse files
committed
fix: handle null prices on complex product
1 parent 5061e74 commit 6c7305c

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/content/queries/cs-product.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ import { gql } from '../../utils/product.js';
1717
* @returns {Product}
1818
*/
1919
export const adapter = (productData) => {
20-
const minPrice = productData.priceRange?.minimum ?? productData.price;
21-
const maxPrice = productData.priceRange?.maximum ?? productData.price;
20+
let minPrice = productData.priceRange?.minimum ?? productData.price;
21+
let maxPrice = productData.priceRange?.maximum ?? productData.price;
2222

23-
console.debug('[cs-product] minPrice, maxPrice: ', minPrice, maxPrice);
23+
if (minPrice == null) {
24+
minPrice = maxPrice;
25+
} else if (maxPrice == null) {
26+
maxPrice = minPrice;
27+
}
2428

2529
/** @type {Product} */
2630
const product = {
@@ -65,7 +69,7 @@ export const adapter = (productData) => {
6569
isDefault: value.isDefault,
6670
})),
6771
})),
68-
prices: {
72+
prices: (minPrice && maxPrice) ? {
6973
regular: {
7074
// TODO: determine whether to use min or max
7175
amount: minPrice.regular.amount.value,
@@ -83,7 +87,7 @@ export const adapter = (productData) => {
8387
// TODO: add variant?
8488
},
8589
visible: minPrice.roles?.includes('visible'),
86-
},
90+
} : null,
8791
};
8892

8993
return product;

src/content/queries/cs-variants.js

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export const adapter = (variants) => variants.map(({ selections, product }) => {
2020
const minPrice = product.priceRange?.minimum ?? product.price;
2121
const maxPrice = product.priceRange?.maximum ?? product.price;
2222

23-
console.debug('[cs-variant] minPrice, maxPrice: ', minPrice, maxPrice);
24-
2523
/** @type {Variant} */
2624
const variant = {
2725
name: product.name,

src/templates/json-ld.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ export default (product, variants) => {
4444
image,
4545
productID: sku,
4646
offers: [
47-
{
47+
prices ? ({
4848
'@type': 'Offer',
4949
sku,
5050
url,
5151
image,
5252
availability: inStock ? 'InStock' : 'OutOfStock',
5353
price: prices?.final?.amount,
5454
priceCurrency: prices?.final?.currency,
55-
},
55+
}) : undefined,
5656
...variants.map((v) => ({
5757
'@type': 'Offer',
5858
sku: v.sku,
@@ -62,7 +62,7 @@ export default (product, variants) => {
6262
price: v.prices?.final?.amount,
6363
priceCurrency: v.prices?.final?.currency,
6464

65-
})),
65+
})).filter(Boolean),
6666
],
6767
...(brandName
6868
? {

0 commit comments

Comments
 (0)