-
Notifications
You must be signed in to change notification settings - Fork 3
/
seo-rich-snippets-min-quantity-fix.html
110 lines (83 loc) · 3.62 KB
/
seo-rich-snippets-min-quantity-fix.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<script data-cfasync="false">
/*
Web Site Advantage: Min Quantity Fix [v1.1]
Multiplies prices by min quantity to comply with Google Merchant Centre requirements
https://bigcommerce.websiteadvantage.com.au/
Copyright (C) 2023 Web Site Advantage
*/
{{#if page_type '===' 'product' }}
// Fix is now in the Tag Rocket app, so this is no longer required. However you could use it to customise referenceQuantity and referenceQuantity.valueReference values.
// place this in the footer
!function(w,d){
// https://support.google.com/merchants/answer/6098334
// https://support.google.com/merchants/answer/6386198
// https://support.google.com/merchants/answer/6324371
// https://developers.google.com/search/docs/appearance/structured-data/product#unit-pricing-example
// https://support.google.com/merchants/answer/6324455
var unit_pricing_measure_include = false;
var unit_pricing_measure_type = "ct"; // Per unit
// https://support.google.com/merchants/answer/6324490
var unit_pricing_base_measure_include = false; // requires unit_pricing_measure_include to be true
var unit_pricing_base_measure = 1;
var unit_pricing_base_measure_type = "ct"; // Per unit
// END of settings
var element = d.getElementById('wsa-rich-snippets-jsonld-product'); // change for development server
var json = JSON.parse(element.text);
var unit_pricing_measure = null;
if (!json.offers)
return;
for(var i=0; i < json.offers.length; i++) {
var offer = json.offers[i];
if (offer["@type"] == "Offer") {
var priceSpecification = offer.priceSpecification;
if (!priceSpecification || !priceSpecification.eligibleQuantity)
continue;
unit_pricing_measure = priceSpecification.eligibleQuantity.minValue;
if (!unit_pricing_measure)
continue;
// fix the price
if (offer.price) {
// Docs say only put price in the priceSpecification when adding a referenceQuantity
delete offer.price;
delete offer.priceCurrency;
}
if (priceSpecification.price) {
priceSpecification.price *= unit_pricing_measure;
}
if (priceSpecification.maxPrice) {
priceSpecification.maxPrice *= unit_pricing_measure;
}
if (priceSpecification.minPrice) {
priceSpecification.minPrice *= unit_pricing_measure;
}
delete priceSpecification.eligibleQuantity;
if (!unit_pricing_measure_include)
continue;
priceSpecification.referenceQuantity = {
"@type": "QuantitativeValue",
"value": unit_pricing_measure,
"unitCode": unit_pricing_measure_type
};
if (!unit_pricing_base_measure_include)
continue;
priceSpecification.referenceQuantity.valueReference = {
"@type": "QuantitativeValue",
"value": unit_pricing_base_measure,
"unitCode": unit_pricing_base_measure_type
};
}
else if (offer["@type"] == "AggregateOffer") {
if (unit_pricing_measure == null)
continue;
if (offer.lowPrice) {
offer.lowPrice *= unit_pricing_measure;
}
if (offer.highPrice) {
offer.highPrice *= unit_pricing_measure;
}
}
}
element.text = JSON.stringify(json);
}(window, document)
{{/if}}
</script>