skip to Main Content

I’ve been trying to crack my head on this one. Creating new products now apparently doesn’t change the variant when I select the product images.

I implemented this months ago and it works fine until today.

Apparently it still works on the old products that was created but not the new ones.

Example :

Old product

New product

I appreciate if anyone can shed some light on this one. Thank you very much in advance.

2

Answers


  1. You can try this code: replace productJson.forEach in if (productJson.length > 0){ in function _selectVariant:

    productJson.forEach((product) => {
            const sectionId = product.id.replace("ProductJson-", "shopify-section-");
            const thumbnails1 = document.querySelectorAll('#'+ sectionId + ' img[src*="/products/"]');
            const thumbnails2 = document.querySelectorAll('#'+ sectionId + ' img[src*="/files/"]');
            if (thumbnails1.length > 1) {
              const productObject = JSON.parse(product.innerHTML);
              const variantImages = this._createVariantImage(productObject);
              // need to check variants > 1
              if (productObject.variants.length > 1) {
                thumbnails1.forEach((thumbnail) => {
                  thumbnail.addEventListener('click', (e) =>
                    this._updateVariant(e, sectionId, productObject, variantImages),
                  );
                });
              }
            }else if (thumbnails2.length > 1) {
              const productObject = JSON.parse(product.innerHTML);
              const variantImages = this._createVariantImage(productObject);
              // need to check variants > 1
              if (productObject.variants.length > 1) {
                thumbnails2.forEach((thumbnail) => {
                  thumbnail.addEventListener('click', (e) =>
                    this._updateVariant(e, sectionId, productObject, variantImages),
                  );
                });
              }
            }
          });
    
     
    

    Note: Make sure the number of brackets {} is still complete.

    I also found that uploading variant images directly onto the variant instead of using the main uploader and assigning it to the variant, it would work with the old code template. But using this code I did not have to go back and update all the old ‘broken’ variants and things seem to work with my old workflow.

    Login or Signup to reply.
  2. Thanks, barkles. That worked like a charm!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search