Skip to content

Commit 9fb1b8c

Browse files
committed
Update fix for gtin value
1 parent 5e19e73 commit 9fb1b8c

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

react/Product.js

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,52 +62,25 @@ const getSKUAvailabilityString = (seller) =>
6262

6363
const normalizeGTIN = (raw) => {
6464
if (raw === null || raw === undefined) return null
65-
6665
const digits = String(raw).replace(/\D+/g, '')
6766

6867
if (!digits || /^0+$/.test(digits)) return null
6968

7069
const VALID = [8, 12, 13, 14]
7170

72-
if (VALID.includes(digits.length)) {
73-
return { value: digits, length: digits.length }
74-
}
71+
if (VALID.includes(digits.length)) return digits
7572

7673
const target = VALID.find((len) => digits.length < len)
7774

78-
if (target) {
79-
return { value: digits.padStart(target, '0'), length: target }
80-
}
75+
if (target) return digits.padStart(target, '0')
8176

8277
return null
8378
}
8479

85-
const mapGtinToSpecificField = (gtinObj) => {
86-
if (!gtinObj) return {}
87-
const { value, length } = gtinObj
88-
89-
switch (length) {
90-
case 8:
91-
return { gtin8: value }
92-
93-
case 12:
94-
return { gtin12: value }
95-
96-
case 13:
97-
return { gtin13: value }
98-
99-
case 14:
100-
return { gtin14: value }
101-
102-
default:
103-
return {}
104-
}
105-
}
106-
10780
const parseSKUToOffer = (
10881
item,
10982
currency,
110-
{ decimals, pricesWithTax, useSellerDefault }
83+
{ decimals, pricesWithTax, useSellerDefault, gtinValue }
11184
) => {
11285
const seller = useSellerDefault
11386
? getSellerDefault(item.sellers)
@@ -124,12 +97,18 @@ const parseSKUToOffer = (
12497
return null
12598
}
12699

100+
const rawSkuValue = item?.[gtinValue]
101+
const normalizedGtin = normalizeGTIN(rawSkuValue)
102+
const offerSku = normalizedGtin || item.itemId
103+
104+
if (availability === OUT_OF_STOCK && price === 0) return null
105+
127106
const offer = {
128107
'@type': 'Offer',
129108
price,
130109
priceCurrency: currency,
131110
availability,
132-
sku: item.itemId,
111+
sku: offerSku,
133112
itemCondition: 'http://schema.org/NewCondition',
134113
priceValidUntil: path(['commertialOffer', 'PriceValidUntil'], seller),
135114
seller: {
@@ -157,7 +136,13 @@ const getSellerDefault = (sellers) => {
157136
const composeAggregateOffer = (
158137
product,
159138
currency,
160-
{ decimals, pricesWithTax, useSellerDefault, disableAggregateOffer }
139+
{
140+
decimals,
141+
pricesWithTax,
142+
useSellerDefault,
143+
disableAggregateOffer,
144+
gtinValue,
145+
}
161146
) => {
162147
const items = product.items || []
163148
const allSellers = getAllSellers(items)
@@ -169,6 +154,7 @@ const composeAggregateOffer = (
169154
decimals,
170155
pricesWithTax,
171156
useSellerDefault,
157+
gtinValue,
172158
})
173159
)
174160
.filter(Boolean)
@@ -231,6 +217,7 @@ export const parseToJsonLD = ({
231217
pricesWithTax,
232218
useSellerDefault,
233219
disableAggregateOffer,
220+
gtinValue,
234221
})
235222

236223
if (offers === null) {
@@ -241,15 +228,9 @@ export const parseToJsonLD = ({
241228
const category = getCategoryName(product)
242229

243230
const rawGTIN = selectedItem?.[gtinValue]
244-
const gtinObj =
245-
gtinValue === 'itemId'
246-
? normalizeGTIN(selectedItem?.itemId)
247-
: normalizeGTIN(rawGTIN)
248-
249-
const gtinFields = mapGtinToSpecificField(gtinObj)
250-
const gtin = gtinObj?.value ?? null
231+
const gtin = normalizeGTIN(rawGTIN)
251232

252-
const merchantSKU = selectedItem?.itemId || null
233+
const merchantSKU = gtin || selectedItem?.itemId || null
253234

254235
const productLD = {
255236
'@context': 'https://schema.org/',
@@ -265,7 +246,6 @@ export const parseToJsonLD = ({
265246
sku: merchantSKU,
266247
category,
267248
offers: disableOffers ? null : offers,
268-
...gtinFields,
269249
gtin,
270250
}
271251

0 commit comments

Comments
 (0)