Created
May 5, 2021 17:48
-
-
Save joelcardinal/a6dd5d8a1cf175754fd9eb650418fe91 to your computer and use it in GitHub Desktop.
PDP schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// JSON+LD Schema :: PDP | |
(function(){ | |
var minPrice = null; | |
var maxPrice = null; | |
var isMaster = true; | |
var pdpData = app.pdp && app.pdp.data != undefined ? app.pdp.data : window.pageData != undefined ? window.pageData : {}; | |
var productData = app.product && app.product.data && app.product.data.cache && app.product.data.cache[pdpData.productID] != undefined ? app.product.data.cache[pdpData.productID] : app.config && app.config.products && app.config.products[pdpData.productID] ? app.config.products[pdpData.productID] : null; | |
if(!productData){return;} | |
if(isMaster){ | |
var keys = []; | |
var colors = productData.masterData.colors; | |
for(var priceKey in colors){ | |
keys.push(Number(priceKey)); | |
} | |
// sort descending | |
keys.sort(function(a,b){return b-a}); | |
minPrice = keys[keys.length - 1]; | |
maxPrice = keys[0]; | |
} else { | |
minPrice = maxPrice = productData.masterData.single.price.price; | |
} | |
var schemaObj = { | |
"@context": "http://schema.org", | |
"@type": "Product", | |
"name" : productData.pidData.name, | |
"offers" : [ | |
{ | |
"@type" : "Offer", | |
"price" : maxPrice, | |
"availability": "http://schema.org/InStock", | |
"url" : pdpData.link, | |
"priceCurrency" : productData.pidData.priceCurrency | |
}, | |
// for some reason we always send both, even if single color no sale | |
{ | |
"@type" : "Offer", | |
"price" : minPrice, | |
"availability": "http://schema.org/InStock", | |
"url" : pdpData.link, | |
"priceCurrency" : productData.pidData.priceCurrency | |
} | |
], | |
// TODO | |
/* | |
add additional schema?: | |
• brand or organization | |
• *image | |
• *url (of product) | |
• *productID | |
• logo | |
• category | |
• color | |
• itemCondition | |
• manufacturer | |
• material | |
• mpn/gtin/sku/productID | |
• review // which one, this is singular? | |
*/ | |
"brand":{"name":"Crocs"}, | |
"image" : pdpData.image, | |
"url" : pdpData.link, | |
"productID" : pdpData.productID, | |
"itemCondition" : "http://schema.org/NewCondition" | |
}; | |
if(productData.pidData.rating.starRatingBase && productData.pidData.rating.starRatingCount){ | |
schemaObj.aggregateRating = { | |
"@type" : "AggregateRating", | |
"worstRating" : 1, // for some reason these are hard-coded this way | |
"ratingValue" : productData.pidData.rating.starRatingBase, | |
"bestRating" : 5, // for some reason these are hard-coded this way | |
"ratingCount" : productData.pidData.rating.starRatingCount | |
} | |
} | |
if(pdpData.categoryName){schemaObj.category = pdpData.categoryName;} | |
addInfoToDOM(schemaObj); | |
function addInfoToDOM(schemaObj){ | |
// add schema via ld+json | |
// per Google you can dynamically set this: | |
// https://developers.google.com/search/docs/guides/intro-structured-data | |
var head = document.getElementsByTagName("head")[0], | |
sTag = document.createElement("script"); | |
sTag.type = "application/ld+json"; | |
sTag.classList.add('dev3test'); | |
sTag.text = JSON.stringify(schemaObj); | |
head.appendChild(sTag); | |
} | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment