-
Notifications
You must be signed in to change notification settings - Fork 3
/
seo-rich-snippets-advanced-shipping-services.html
201 lines (169 loc) · 8.34 KB
/
seo-rich-snippets-advanced-shipping-services.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<script data-cfasync="false">
/*
Web Site Advantage: SEO Rich Snippets Advanced Shipping Services [v1.0]
Adds advanced shipping service rules to the product markup
https://bigcommerce.websiteadvantage.com.au/rich-snippets/articles/advanced-google-shipping-information-via-javascript/
Copyright (C) 2023 Web Site Advantage
*/
{{~#if page_type '===' 'product'}}
// this example is designed to work with BigCommerce stores using our SEO Rich Snippets app. Add the script in the script manager.
// https://bigcommerce.websiteadvantage.com.au/rich-snippets/
// It is using example shipping rules. You will have to modify it to follow your rules.
!function(){
var offerId = '#Offer'; // This is what SEO Rich Snippets uses to identify the products offer. It is used to connect this with the Offer created by SEO Rich Snippets
// These should match the settings in Store Settings->Website->Physical Dimension Settings
var bigCommerceDecimalToken = '.';
var bigCommerceThousandsToken = ','
// BigCommerce provides formatted dimensions. This converts them to numbers
function parseBigCommerceDimension(text) {
var newText = text.toLowerCase().replace("{{settings.measurements.length}}".toLowerCase(), "").replace("{{settings.measurements.weight}}".toLowerCase(), "").replace(bigCommerceDecimalToken, ".").replace(bigCommerceThousandsToken, "").trim();
if (newText) {
return parseFloat(newText);
}
else return -1;
}
// Let's get BigCommerce product data that could be of use when calculating shipping prices
var product = {
price: {{~#if product.price.with_tax}}{{product.price.with_tax.value}}{{else}}{{~#if product.price.without_tax}}{{product.price.without_tax.value}}{{else}}-1{{~/if}}{{~/if}}, // -1 = price unknown
priceCurrency: '{{~#if product.price.with_tax}}{{product.price.with_tax.currency}}{{else}}{{~#if product.price.without_tax}}{{product.price.without_tax.currency}}{{else}}{{~/if}}{{~/if}}',
weight: parseBigCommerceDimension('{{product.weight}}'),
width: parseBigCommerceDimension('{{product.width}}'),
height: parseBigCommerceDimension('{{product.height}}'),
depth: parseBigCommerceDimension('{{product.depth}}'),
fixedShippingPrice: {{~#if product.shipping.price}}{{product.shipping.price.value}}{{else}}-1{{~/if}}, // -1 = no fixed price. 0 = free
fixedShippingCurrency: '{{~#if product.shipping.price}}{{product.shipping.price.currency}}{{~/if}}'
};
// Standard handling times for the examples
var minHandlingTimeInDays = 0;
var maxHandlingTimeInDays = 1;
// Transit times for the regions the examples ship to
var newYorkMinTransitTimeInDays = 1;
var newYorkMaxTransitTimeInDays = 3;
var usMinTransitTimeInDays = 1;
var usMaxTransitTimeInDays = 5;
var caMinTransitTimeInDays = 1;
var caMaxTransitTimeInDays = 7;
shippingItems = [];
// Example shipping items
// Say we offer free shipping in the New York state
shippingItems.push({
cost: 0,
currency: product.priceCurrency,
country: ["US"],
region: ["NY"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: newYorkMinTransitTimeInDays,
transitTimeMaxDays: newYorkMaxTransitTimeInDays
});
// If a product has a fixed shipping price then offer that in the places we ship to:
if (product.fixedShippingPrice >= 0) {
shippingItems.push({
cost: product.fixedShippingPrice,
currency: product.fixedShippingCurrency,
country: ["US"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: usMinTransitTimeInDays,
transitTimeMaxDays: usMaxTransitTimeInDays
});
shippingItems.push({
cost: product.fixedShippingPrice,
currency: product.fixedShippingCurrency,
country: ["CA"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: caMinTransitTimeInDays,
transitTimeMaxDays: caMaxTransitTimeInDays
});
}
else {
// not fixed, so shipping is calculated.
// Let's offer free shipping for products over $100
if (product.price >= 100) {
shippingItems.push({
cost: 0,
currency: product.priceCurrency,
country: ["US"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: usMinTransitTimeInDays,
transitTimeMaxDays: usMaxTransitTimeInDays
});
shippingItems.push({
cost: 0,
currency: product.priceCurrency,
country: ["CA"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: caMinTransitTimeInDays,
transitTimeMaxDays: caMaxTransitTimeInDays
});
}
else {
// shipping is based on weight in the US. We can only calculate a max cost.
var usShippingMaxCost = Math.min(10 + (product.weight / 100), 100);
shippingItems.push({
maxCost: usShippingMaxCost,
currency: product.priceCurrency,
country: ["US"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: usMinTransitTimeInDays,
transitTimeMaxDays: usMaxTransitTimeInDays
});
var caShippingMaxCost = 10 + (product.price / 5);
shippingItems.push({
cost: caShippingMaxCost,
currency: product.priceCurrency,
country: ["CA"],
handlingTimeMinDays: minHandlingTimeInDays,
handlingTimeMaxDays: maxHandlingTimeInDays,
transitTimeMinDays: caMinTransitTimeInDays,
transitTimeMaxDays: caMaxTransitTimeInDays
});
}
}
// ---------------------------------------------------------------
// Code to add the structured data
var shippingDetails = [];
for (var i = 0; i < shippingItems.length; i++) {
var item = shippingItems[i];
var shippingRate = {"@type": "MonetaryAmount"};
if (item.maxCost !== 'undefined') shippingRate.maxValue = item.maxCost;
if (item.cost !== 'undefined') shippingRate.value = item.cost;
if (item.currency) shippingRate.currency = item.currency;
var shippingDestination = {"@type": "DefinedRegion"};
if (item.country) shippingDestination.addressCountry = item.country;
if (item.region) shippingDestination.addressRegion = item.region;
if (item.postalCode) shippingDestination.postalCode = item.postalCode;
var handlingTime = {"@type": "QuantitativeValue", "unitCode": "DAY"};
if (item.handlingTimeMinDays !== 'undefined') handlingTime.minValue = item.handlingTimeMinDays;
if (item.handlingTimeMaxDays !== 'undefined') handlingTime.maxValue = item.handlingTimeMaxDays;
var transitTime = {"@type": "QuantitativeValue", "unitCode": "DAY"};
if (item.transitTimeMinDays !== 'undefined') transitTime.minValue = item.transitTimeMinDays;
if (item.transitTimeMaxDays !== 'undefined') transitTime.maxValue = item.transitTimeMaxDays;
shippingDetails.push({
"@type": "OfferShippingDetails",
"shippingRate": shippingRate,
"shippingDestination": shippingDestination,
"deliveryTime": {
"@type": "ShippingDeliveryTime",
"handlingTime": handlingTime,
"transitTime": transitTime
}
});
}
var offer = {
"@context":"https://schema.org",
"@id": offerId,
"shippingDetails": shippingDetails
};
var script = document.createElement('script');
script.id = 'wsa-rich-snippets-jsonld-offer-shipping';
script.setAttribute('type', 'application/ld+json');
script.textContent = JSON.stringify(offer);
document.head.appendChild(script);
}();
{{~/if}}
</script>