@@ -38,7 +38,7 @@ export class HTMLTemplate {
38
38
* @param {number|undefined } max
39
39
* @returns {string }
40
40
*/
41
- static priceRange = ( min , max ) => ( min !== max ? ` ( ${ min } - ${ max } ) ` : '' ) ;
41
+ static priceRange = ( min , max ) => ( min !== max ? `${ min } - ${ max } ` : ` ${ min } ` ) ;
42
42
43
43
/**
44
44
* @param {string } str
@@ -108,7 +108,7 @@ ${HTMLTemplate.metaName('twitter:card', 'summary_large_image')}
108
108
${ HTMLTemplate . metaName ( 'twitter:title' , product . name ) }
109
109
${ HTMLTemplate . metaName ( 'twitter:description' , product . metaDescription ) }
110
110
${ HTMLTemplate . metaName ( 'twitter:label1' , 'Price' ) }
111
- ${ HTMLTemplate . metaName ( 'twitter:data1' , product . prices ?. final ?. amount ) }
111
+ ${ HTMLTemplate . metaName ( 'twitter:data1' , product . price ?. final ?. amount ?. value ?? product . priceRange ?. minimum ?. final ?. amount ?. value ) }
112
112
${ HTMLTemplate . metaName ( 'twitter:label2' , 'Availability' ) }
113
113
${ HTMLTemplate . metaName ( 'twitter:data2' , product . inStock ? 'In stock' : 'Out of stock' ) } `;
114
114
}
@@ -126,8 +126,9 @@ ${HTMLTemplate.metaName('externalId', product.externalId)}
126
126
${ HTMLTemplate . metaName ( 'addToCartAllowed' , product . addToCartAllowed ) }
127
127
${ HTMLTemplate . metaName ( 'inStock' , product . inStock ? 'true' : 'false' ) }
128
128
${ HTMLTemplate . metaProperty ( 'product:availability' , product . inStock ? 'In stock' : 'Out of stock' ) }
129
- ${ HTMLTemplate . metaProperty ( 'product:price.amount' , product . prices ?. final ?. amount ) }
130
- ${ HTMLTemplate . metaProperty ( 'product:price.currency' , product . prices ?. final ?. currency ) } `;
129
+ ${ HTMLTemplate . metaProperty ( 'product:price.amount' , product . price ?. final ?. amount ?. value ?? product . priceRange ?. minimum ?. final ?. amount ?. value ) }
130
+ ${ HTMLTemplate . metaProperty ( 'product:price.currency' , product . price ?. final ?. amount ?. currency ?? product . priceRange ?. minimum ?. final ?. amount ?. currency ) }
131
+ ${ HTMLTemplate . metaProperty ( 'product:type' , product . type ) } `;
131
132
}
132
133
133
134
/**
@@ -169,6 +170,38 @@ ${HTMLTemplate.indent(this.renderJSONLD(), 2)}
169
170
</head>` ;
170
171
}
171
172
173
+ /**
174
+ * Render product price block
175
+ * @param {Product } product
176
+ * @returns {string }
177
+ */
178
+ renderProductPrices ( product ) {
179
+ const { price, priceRange } = product ;
180
+ const hasFinal = price
181
+ ? price . final ?. amount ?. value < price . regular ?. amount ?. value
182
+ : priceRange ?. minimum ?. final . amount . value < priceRange ?. minimum ?. regular . amount . value ;
183
+ const isRange = ! ! priceRange
184
+ && priceRange . minimum ?. final ?. amount ?. value !== priceRange . maximum ?. final ?. amount ?. value ;
185
+
186
+ const regularPrice = price ? price . regular ?. amount : priceRange . minimum ?. regular ?. amount ;
187
+ const finalPrice = price ? price . final ?. amount : priceRange . minimum ?. final ?. amount ;
188
+
189
+ return /* html */ `\
190
+ <div class="product-prices">
191
+ <div>
192
+ <div>Regular</div>
193
+ <div>${ regularPrice . value } ${ regularPrice . currency } </div>
194
+ ${ isRange ? `<div>${ priceRange . maximum ?. regular ?. amount ?. value } ${ priceRange . maximum ?. regular ?. amount ?. currency } </div>` : '' }
195
+ </div>
196
+ ${ hasFinal ? /* html */ `\
197
+ <div>
198
+ <div>${ finalPrice . value } ${ finalPrice . currency } </div>
199
+ <div>${ price ? price . final ?. amount ?. value : priceRange . minimum ?. final ?. amount ?. value } </div>
200
+ ${ isRange ? `<div>${ priceRange . maximum ?. final ?. amount ?. value } ${ priceRange . maximum ?. final ?. amount ?. currency } </div>` : '' }
201
+ </div>` : '' }
202
+ </div>` ;
203
+ }
204
+
172
205
/**
173
206
* Create the product images
174
207
* @param {Image[] } images
@@ -235,7 +268,7 @@ ${attributes.map((attr) => /* html */`\
235
268
* @returns {string }
236
269
*/
237
270
renderProductOptions ( options ) {
238
- return options . length > 0 ? /* html */ `\
271
+ return options ? .length > 0 ? /* html */ `\
239
272
<div class="product-options">
240
273
${ options . map ( ( opt ) => /* html */ `\
241
274
<div>
@@ -302,13 +335,23 @@ ${HTMLTemplate.indent(this.renderProductItems(opt.items), 2)}`).join('\n')}
302
335
303
336
/**
304
337
* Create the variant prices
305
- * @param {Pick<Prices, 'regular' | 'final'> } prices
338
+ * @param {Variant } variant
306
339
* @returns {string }
307
340
*/
308
- renderVariantPrices ( prices ) {
309
- return /* html */ `\
310
- <div>Regular: ${ prices . regular ?. amount } ${ prices . regular ?. currency } ${ HTMLTemplate . priceRange ( prices . regular ?. minimumAmount , prices . regular ?. maximumAmount ) } </div>
311
- <div>Final: ${ prices . final ?. amount } ${ prices . final ?. currency } ${ HTMLTemplate . priceRange ( prices . final ?. minimumAmount , prices . final ?. maximumAmount ) } </div>` ;
341
+ renderVariantPrices ( variant ) {
342
+ if ( variant . price ) {
343
+ const { price } = variant ;
344
+ return /* html */ `\
345
+ <div>Regular: ${ price . regular ?. amount ?. value } ${ price . regular ?. amount ?. currency } </div>
346
+ <div>Final: ${ price . final ?. amount ?. value } ${ price . final ?. amount ?. currency } </div>` ;
347
+ }
348
+ if ( variant . priceRange ) {
349
+ const { minimum, maximum } = variant . priceRange ;
350
+ return /* html */ `\
351
+ <div>Regular: ${ HTMLTemplate . priceRange ( minimum ?. regular ?. amount ?. value , maximum ?. regular ?. amount ?. value ) } ${ minimum ?. regular ?. amount ?. currency } </div>
352
+ <div>Final: ${ HTMLTemplate . priceRange ( minimum ?. final ?. amount ?. value , maximum ?. final ?. amount ?. value ) } ${ minimum ?. final ?. amount ?. currency } </div>` ;
353
+ }
354
+ return '' ;
312
355
}
313
356
314
357
/**
@@ -328,7 +371,7 @@ ${this.variants.map((v) => /* html */`\
328
371
<div>${ v . name } </div>
329
372
<div>${ v . description } </div>
330
373
<div>${ v . inStock ? 'inStock' : '' } </div>
331
- ${ v . prices ? HTMLTemplate . indent ( this . renderVariantPrices ( v . prices ) , 4 ) : '' }
374
+ ${ v . price || v . priceRange ? HTMLTemplate . indent ( this . renderVariantPrices ( v ) , 4 ) : '' }
332
375
<div>
333
376
${ HTMLTemplate . indent ( this . renderVariantImages ( v . images ) , 6 ) }
334
377
</div>
@@ -389,6 +432,7 @@ ${HTMLTemplate.indent(this.renderHead(), 2)}
389
432
<h1>${ name } </h1>
390
433
${ description ? `<p>${ description } </p>` : '' }
391
434
${ HTMLTemplate . indent ( this . renderProductImages ( images ) , 8 ) }
435
+ ${ HTMLTemplate . indent ( this . renderProductPrices ( this . product ) , 8 ) }
392
436
${ HTMLTemplate . indent ( this . renderProductAttributes ( attributes ) , 8 ) }
393
437
${ HTMLTemplate . indent ( this . renderProductOptions ( options ) , 8 ) }
394
438
${ HTMLTemplate . indent ( this . renderProductVariants ( ) , 8 ) }
0 commit comments