Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vivirenremoto/e77d7ccb0f70c7b8b1d10fe73507f3fb to your computer and use it in GitHub Desktop.
Save vivirenremoto/e77d7ccb0f70c7b8b1d10fe73507f3fb to your computer and use it in GitHub Desktop.
product schema dynamic tag
var product_images = [];
jQuery(function () {
for (var i = 0; i < 5; i++) {
if (jQuery('rs-sbg:eq(' + i + ')').length) {
product_images.push(jQuery('rs-sbg:eq(' + i + ')').attr('data-lazyload'));
}
}
if (product_images.length == 0) {
product_images.push(jQuery("meta[property='og:image']").attr('content'));
}
var product_price = jQuery('.descuento strong').html().replace('€', '');
var product_currency = 'EUR';
var product_json = {
"@context": "https://schema.org/",
"@type": "Product",
"name": jQuery('h1 strong').html(),
"image": product_images,
"description": jQuery('meta[name=description]').attr('content'),
"brand": {
"@type": "Brand",
"name": jQuery("meta[property='og:site_name']").attr('content')
},
"offers": {
"@type": "Offer",
"url": jQuery("meta[property='og:url']").attr('content'),
"priceCurrency": product_currency,
"price": product_price
}
};
var el = document.createElement('script');
el.type = 'application/ld+json';
el.text = JSON.stringify(product_json);
document.querySelector('head').appendChild(el);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment