Change 1st image to feature image on the product page rather than the variant image.

Solved

Change 1st image to feature image on the product page rather than the variant image.

sahilrev
Shopify Partner
30 0 2
Hi all,

 

I want to display the feature image as the 1st image on the product page, but unable to achieve the same as it is showing the variant image as the 1st image not the feature image(or first image which we have added  in the product admin section).

 

I have tried previous suggestion that has given on the forum but it is not working on the dawn 15.0 theme.

 

Also I need to achieve the same for the collection page, as for some of the product on the collection page its showing the feature image, but if the first variant is sold out then it is not showing the variant image rather than the feature image.

 

how can I achieve this?

 

Thanks in advance for the help.

Accepted Solution (1)

sahilsharma9515
Shopify Partner
1206 156 233

This is an accepted solution.

Hi @sahilrev Hopefully you have found the solution for this issue, if not then here is the code you can use to solve the issue of product page, As I have already added the code on your other thread, so adding the code here as well.

 

<script>
    document.addEventListener('DOMContentLoaded', function() {
    const allMediaItems = document.querySelectorAll('.product__media-item'); // All media items (big images)
    const thumbnailItems = document.querySelectorAll('.thumbnail-list__item'); // Thumbnails
    const featuredMediaId = document.querySelector('.product__media-item.is-active')?.getAttribute('data-media-id'); // Featured media ID

    // Function to display the initial featured image on load
    const showInitialMedia = () => {
        if (featuredMediaId) {
            // Check if the featured media exists and activate it
            const featuredMediaItem = document.querySelector(`[data-media-id="${featuredMediaId}"]`);
            if (featuredMediaItem) {
                resetActiveState(); // Clear previous active states
                featuredMediaItem.classList.add('is-active');
            }
        } else {
            // If no featured media, default to the first media item
            allMediaItems[0].classList.add('is-active');
        }
    };

    // Function to reset all media and thumbnails to remove active state
    const resetActiveState = () => {
        allMediaItems.forEach(item => item.classList.remove('is-active'));
        thumbnailItems.forEach(item => item.querySelector('button').setAttribute('aria-current', 'false'));
    };

    // Function to update the main media image when a thumbnail is clicked
    const activateMediaItem = (mediaId) => {
        resetActiveState(); // Remove 'is-active' from all media items
        const selectedMediaItem = document.querySelector(`[data-media-id="${mediaId}"]`);
        if (selectedMediaItem) {
            selectedMediaItem.classList.add('is-active'); // Activate the selected media item
        }
    };

    // Attach click event to thumbnails to update the big image
    thumbnailItems.forEach(thumbnail => {
        thumbnail.addEventListener('click', function() {
            const mediaId = thumbnail.getAttribute('data-target'); // Get the corresponding media ID
            activateMediaItem(mediaId);
            thumbnail.querySelector('button').setAttribute('aria-current', 'true'); // Mark thumbnail as active
        });
    });

    // Call the initial load function to set the featured image
    showInitialMedia();
});
</script>

 

Please follow the steps:

  1. Login in shopify admin.
  2. Click on the Online Store.
  3. Then click on the button next to Customize in live Theme.
  4. Click Edit Code.
  5. Search theme.liquid in the code in left hand side in your theme.
  6. Add the following code in the bottom of the file above </body> tag

Hopefully it will help you. If yes then Please don't forget hit Like and Mark it as solution!

 

Best Regards

Sahil

- Your

 Coffee Tip 

can create magic in coding ❤️❤️

- Need a Shopify Developer? CHAT ON WHATSAPP or EMAIL ME !


- Hopefully the solution will help you. If yes then Please hit

 Like 

and

 Mark it as solution! ❤️


View solution in original post

Replies 3 (3)

Arif_Shopidevs
Shopify Partner
263 37 36

Hello, It need to modify the card template for collection to achieve this.
Product page is little bit complex due to variate. Normally default varient picture show their.  If you update theme it will remove the old customization

you can check https://easydisplaydemo.myshopify.com/ which show product image also hover and normally, on order basis.

Thank you

Found it helpful? Please like and mark the solution that helped you.
Slider Revolution - Create sliders, theme sections, banners, videos, pages, advanced animation, and social feeds.
Essential Grid Gallery - Create photo galleries, video galleries, portfolio galleries, product gallery, collection gallery and more.
EasyDisplay: Product Showcase - Easily display collections, related products, discounts, recently viewed items, and best sellers

sahilsharma9515
Shopify Partner
1206 156 233

This is an accepted solution.

Hi @sahilrev Hopefully you have found the solution for this issue, if not then here is the code you can use to solve the issue of product page, As I have already added the code on your other thread, so adding the code here as well.

 

<script>
    document.addEventListener('DOMContentLoaded', function() {
    const allMediaItems = document.querySelectorAll('.product__media-item'); // All media items (big images)
    const thumbnailItems = document.querySelectorAll('.thumbnail-list__item'); // Thumbnails
    const featuredMediaId = document.querySelector('.product__media-item.is-active')?.getAttribute('data-media-id'); // Featured media ID

    // Function to display the initial featured image on load
    const showInitialMedia = () => {
        if (featuredMediaId) {
            // Check if the featured media exists and activate it
            const featuredMediaItem = document.querySelector(`[data-media-id="${featuredMediaId}"]`);
            if (featuredMediaItem) {
                resetActiveState(); // Clear previous active states
                featuredMediaItem.classList.add('is-active');
            }
        } else {
            // If no featured media, default to the first media item
            allMediaItems[0].classList.add('is-active');
        }
    };

    // Function to reset all media and thumbnails to remove active state
    const resetActiveState = () => {
        allMediaItems.forEach(item => item.classList.remove('is-active'));
        thumbnailItems.forEach(item => item.querySelector('button').setAttribute('aria-current', 'false'));
    };

    // Function to update the main media image when a thumbnail is clicked
    const activateMediaItem = (mediaId) => {
        resetActiveState(); // Remove 'is-active' from all media items
        const selectedMediaItem = document.querySelector(`[data-media-id="${mediaId}"]`);
        if (selectedMediaItem) {
            selectedMediaItem.classList.add('is-active'); // Activate the selected media item
        }
    };

    // Attach click event to thumbnails to update the big image
    thumbnailItems.forEach(thumbnail => {
        thumbnail.addEventListener('click', function() {
            const mediaId = thumbnail.getAttribute('data-target'); // Get the corresponding media ID
            activateMediaItem(mediaId);
            thumbnail.querySelector('button').setAttribute('aria-current', 'true'); // Mark thumbnail as active
        });
    });

    // Call the initial load function to set the featured image
    showInitialMedia();
});
</script>

 

Please follow the steps:

  1. Login in shopify admin.
  2. Click on the Online Store.
  3. Then click on the button next to Customize in live Theme.
  4. Click Edit Code.
  5. Search theme.liquid in the code in left hand side in your theme.
  6. Add the following code in the bottom of the file above </body> tag

Hopefully it will help you. If yes then Please don't forget hit Like and Mark it as solution!

 

Best Regards

Sahil

- Your

 Coffee Tip 

can create magic in coding ❤️❤️

- Need a Shopify Developer? CHAT ON WHATSAPP or EMAIL ME !


- Hopefully the solution will help you. If yes then Please hit

 Like 

and

 Mark it as solution! ❤️


sahilrev
Shopify Partner
30 0 2

Hi @sahilsharma9515 thanks for the code it worked like a charm, and also thanks for the help.