I have an archive display that groups products by product_tag, I'll upload a pic so you can view it.
To use that pic as an example, that one product card is displaying information about 7 products, price, sku, etc.
Of those 7 products, let's say 3 have product images / gallery and 4 don't.
What I'm trying to achieve is a view that will take the currently displayed image/gallery and loop through the grouped products to grab whichever product has an image/gallery and display that, instead of displaying a placeholder image.
To that effect I created a View that will return results based on product_tag and a template that has simply the image gallery.
This currently works, but the issue lies whenever there are multiple results (ie, several of the grouped products have image/galleries), then it displays the results but all results, resulting in multiple galleries.
I've tried limiting the result of the view to only 1 result but if I do that whever the diplay is of multiple products if the first result doesn't have an image then the result is empty obviously.
To circumvent this 'issue', I have some JS that cleansup the duplicate galleries:
function cleanupProductImageGalleries() {
// Find all elements with the ID BlockMainSingle
const blockMainSingles = document.querySelectorAll('#BlockMainSingle');
blockMainSingles.forEach(block => {
// Find all product image divs within this block
const productImageDivs = block.querySelectorAll('.wp-block-woocommerce-views-product-image.wooviews-product-image');
// If there's more than one, remove all but the first
if (productImageDivs.length > 1) {
for (let i = 1; i < productImageDivs.length; i++) {
productImageDivs[i].remove();
}
}
});
}
// Run the function when the DOM is fully loaded
document.addEventListener('DOMContentLoaded', cleanupProductImageGalleries);
It also works, but I'm wondering if there is some logic to be used within the View to achieve the same without the extra JS.