Advertisement
ikai2

SKU pf

Sep 12th, 2022 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {%- assign current_variant = product.selected_or_first_available_variant -%}
  2. <div class="vendor_name_box">
  3. <b>Sku: </b><span class="variant-sku">{{ current_variant.sku }}</span>
  4. </div>
  5.  
  6. // Update SKU when change the varaint, add this code in the custom code editor in the JS
  7. const products = Array.from(document.querySelectorAll('[data-pf-type="ProductBox"]'));  
  8.     products.map(product => {
  9.         const selects = product.querySelectorAll(".pf-variant-select");
  10.         const swatches = product.querySelectorAll(".pf-option-swatches div[data-option-name]");
  11.           const changeSku = () => {
  12.             setTimeout(() => {
  13.                 const variantId = product.querySelector(
  14.                     '[data-pf-type="ProductVariantSwatches"] input'
  15.                 ).value;
  16.                 const productId = product
  17.                     .querySelector('[data-pf-type="ProductVariantSwatches"] input')
  18.                     .getAttribute("data-product-id");
  19.                 const variant = __pageflyProducts[productId].variants.filter(
  20.                     (item) => item.id === parseInt(variantId)
  21.                 );
  22.                 const sku = variant[0].sku;
  23.                 const variantSku = product.querySelector(".variant-sku")
  24.                 variantSku.innerText = sku;
  25.             }, 0);
  26.           }
  27.           changeSku();
  28.           swatches.forEach((swatch) => {
  29.               swatch.addEventListener("click", changeSku);
  30.           });
  31.          selects.forEach((sel) => {
  32.               sel.addEventListener("change", changeSku);
  33.           });
  34.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement