Skip to content

Commit 8dedfdd

Browse files
authored
Merge pull request #31 from adobe-rnd/price-undef
fix: handle null prices on complex product
2 parents 8504b65 + e3096e9 commit 8dedfdd

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
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/html.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,26 @@ ${v.images?.map((img) => `\
155155
</picture>`).join('\n')}
156156
</div>
157157
<div>${v.selections?.join(', ')}</div>
158-
</div>`).join('\n')};
158+
</div>`).join('\n')}
159159
</div>
160-
<div class="variant-attributes">
161-
${variants?.map((v) => `\
160+
161+
<div class="variant-attributes">
162+
${variants?.map((v) => `\
162163
<div>
163164
<div>sku</div>
164165
<div>${v.sku}</div>
165166
<div></div>
166167
<div></div>
167168
</div>
168-
${v.attributes?.map((attribute) => `\
169+
${v.attributes?.map((attribute) => `\
169170
<div>
170171
<div>attribute</div>
171172
<div>${attribute.name}</div>
172173
<div>${attribute.label}</div>
173174
<div>${attribute.value}</div>
174175
</div>`).join('\n')}`).join('\n')}
175176
</div>
177+
</div>
176178
</main>
177179
<footer></footer>
178180
</body>

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)