bongzilla

microdata: breadcrumbs, images, price

May 2nd, 2021
713
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. $( document ).ready(function() {
  3.  
  4.     const breadcrumbs = $(".br-link");
  5.  
  6.     breadcrumbs.each(function(idx, b) {
  7.         const breadcrumbText = b.innerText;
  8.         b.innerHTML = `<span itemprop=\"name\">${breadcrumbText}</span>`;
  9.     })
  10.  
  11.     const lastBreadcrumb = $(".br-text")[0]
  12.     if(lastBreadcrumb) {
  13.         const breadcrumbText = lastBreadcrumb.innerText;
  14.         lastBreadcrumb.innerHTML = `<span itemprop=\"name\">${breadcrumbText}</span>`;
  15.     }
  16.  
  17.     const postImages = $(".post-div-blog img");
  18.     postImages.each(function(idx, img) {
  19.         img.setAttribute("itemprop", "image");
  20.     })
  21.     const isVariable = $(".variations").length > 0;
  22.  
  23.     function replacer(match, p1, p2, offset, string) {
  24.         let price = p1.replace(" ", "");
  25.         return `<bdi><span itemprop=\"price\" content=\"${price}\">${p1}</span>&nbsp;<span itemprop=\"priceCurrency\" class=\"woocommerce-Price-currencySymbol\">${p2}</span></bdi>`
  26.     }
  27.  
  28.     if(isVariable) {
  29.         setTimeout(() => {
  30.             const priceDiv = $(".prod-div .woocommerce-Price-amount").eq(0);
  31.             const oldMarkup = $(".prod-div .woocommerce-Price-amount")[0].innerHTML;
  32.             // const newMarkup = oldMarkup.replace(/^<bdi>(.*)&nbsp;<span class="woocommerce-Price-currencySymbol">(.*)<\/span><\/bdi>/g, "<bdi><span itemprop=\"price\">$1</span>&nbsp;<span itemprop=\"priceCurrency\" class=\"woocommerce-Price-currencySymbol\">$2</span></bdi>");
  33.             const newMarkup = oldMarkup.replace(/^<bdi>(.*)&nbsp;<span class="woocommerce-Price-currencySymbol">(.*)<\/span><\/bdi>/g, replacer);
  34.             priceDiv.html(newMarkup);
  35.  
  36.         }, 2000)       
  37.     } else {
  38.         const priceDiv = $(".prod-div .woocommerce-Price-amount").eq(0);
  39.         const oldMarkup = $(".prod-div .woocommerce-Price-amount")[0].innerHTML;
  40.         // const newMarkup = oldMarkup.replace(/^<bdi>(.*)&nbsp;<span class="woocommerce-Price-currencySymbol">(.*)<\/span><\/bdi>/g, "<bdi><span itemprop=\"price\">$1</span>&nbsp;<span itemprop=\"priceCurrency\" class=\"woocommerce-Price-currencySymbol\">$2</span></bdi>");
  41.         const newMarkup = oldMarkup.replace(/^<bdi>(.*)&nbsp;<span class="woocommerce-Price-currencySymbol">(.*)<\/span><\/bdi>/g, replacer);
  42.         priceDiv.html(newMarkup);
  43.     }
  44. })
  45. </script>
RAW Paste Data