Skip to content

Commit a0fba5f

Browse files
committed
fix: include mpn in product and offer schemas
1 parent 2d20d91 commit a0fba5f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/templates/json/JSONTemplate.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ export class JSONTemplate {
6868
return productUrl;
6969
}
7070

71+
/**
72+
* @param {Variant} [variant]
73+
*/
74+
constructMPN(variant) {
75+
return variant
76+
? variant.attributes.find((attr) => attr.name.toLowerCase() === 'mpn')?.value ?? this.constructMPN()
77+
: this.product.attributes.find((attr) => attr.name.toLowerCase() === 'mpn')?.value ?? this.product.sku;
78+
}
79+
7180
renderBrand() {
7281
const { attributes } = this.product;
7382
const brandName = attributes?.find((attr) => attr.name === 'brand')?.value;
@@ -90,13 +99,16 @@ export class JSONTemplate {
9099
return {
91100
offers: [
92101
...offers.map((v) => {
93-
const offerUrl = this.constructProductURL(configurableProduct ? v : undefined);
94102
const { prices: variantPrices } = v;
103+
const offerUrl = this.constructProductURL(configurableProduct ? v : undefined);
104+
const mpn = this.constructMPN(configurableProduct ? v : undefined);
95105
const finalPrice = variantPrices?.final?.amount;
96106
const regularPrice = variantPrices?.regular?.amount;
107+
97108
const offer = {
98109
'@type': 'Offer',
99110
sku: v.sku,
111+
mpn,
100112
url: offerUrl,
101113
image: v.images?.[0]?.url ?? image,
102114
availability: v.inStock ? 'InStock' : 'OutOfStock',
@@ -116,7 +128,7 @@ export class JSONTemplate {
116128
offer.priceValidUntil = v.specialToDate;
117129
}
118130

119-
return offer;
131+
return pruneUndefined(offer);
120132
}).filter(Boolean),
121133
],
122134
};
@@ -145,13 +157,15 @@ export class JSONTemplate {
145157
} = this.product;
146158

147159
const productUrl = this.constructProductURL();
160+
const mpn = this.constructMPN();
148161
const image = images?.[0]?.url ?? findProductImage(this.product, this.variants)?.url;
149162
return JSON.stringify(pruneUndefined({
150163
'@context': 'http://schema.org',
151164
'@type': 'Product',
152165
'@id': productUrl,
153166
name,
154167
sku,
168+
mpn,
155169
description: metaDescription,
156170
image,
157171
productID: sku,

src/types.d.ts

-7
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,6 @@ declare global {
164164
label: string;
165165
value: string;
166166
}
167-
168-
// === util types ===
169-
170-
export type PickStartsWith<T extends object, S extends string> = {
171-
[K in keyof T as K extends `${S}${infer R}` ? K : never]: T[K]
172-
}
173-
174167
}
175168

176169
export { };

0 commit comments

Comments
 (0)