Main image in product page switches to first variant image upon entrance

Main image in product page switches to first variant image upon entrance

najiamir
Tourist
3 0 1

Hi everyone

I'm struggling with this problem, seems like only I have it.

I chose a main Picture for my product, but instead of showing it in the product page , it defaults back to the first variant image upon entrance for some reason.

 

I realized it has something to do with this line of code:

{%- assign featured_media = product.selected_or_first_available_variant.featured_media -%}

 

But as I lack coding knowledge, I have no idea what to do with it. Everything I try gives back an error or deletes the whole thumbnail section.

 

Any ideas for how to solve this? The variant picture doesn't match there at all 😞

 

 

 

צילום מסך 2024-07-31 115835.png

 

 

Replies 3 (3)

Small_Task_Help
Shopify Partner
830 28 75

Hi,

 

Need to update the Liquid code to use the main product image as the default

Use javascript if required to handle dynamic image changes as per variant

Javascript code example

 

document.addEventListener('DOMContentLoaded', function () {
  var variantSelect = document.querySelector('select[name="id"]'); // Adjust selector as needed
  var imageContainer = document.querySelector('.product-image'); // Adjust selector as needed

  variantSelect.addEventListener('change', function (event) {
    var selectedVariant = event.target.value;
    var selectedImage = document.querySelector('.variant-image[data-variant-id="' + selectedVariant + '"]');

    if (selectedImage) {
      imageContainer.src=selectedImage.src;
    }
  });
});

 

 

Please share your store URL as well

To Get Shopify Experts Help, E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert Agency
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
najiamir
Tourist
3 0 1

Thanks. This is my store URL: 

 

https://electroship.store/

najiamir
Tourist
3 0 1