Shopify themes, liquid, logos, and UX
Hi all,
I want to display the feature image as the 1st image on the product page instead of the variant image, 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 by @made4Uo on the forum and but it is not working as expected on the dawn theme 15.0, as it is not working for the product that has no variants.
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.
Can you all please help how can I solve this issue?
@BSSCommerce-HDL @Made4uo-Ribe @Moeed @Dan-From-Ryviu @websensepro @sahilsharma9515
Solved! Go to the solution
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.
<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:
Hopefully it will help you. If yes then Please don't forget hit Like and Mark it as solution!
Best Regards
Sahil
- Your
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.
<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:
Hopefully it will help you. If yes then Please don't forget hit Like and Mark it as solution!
Best Regards
Sahil
- Your
Hi @sahilsharma9515 thanks for the code and for the help that you have offered. It worked for me.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024