Skip to content

Instantly share code, notes, and snippets.

@jcook3195
Last active November 5, 2018 13:18
Show Gist options
  • Save jcook3195/75b915feccb653cfcf6f929d2f92f48b to your computer and use it in GitHub Desktop.
Save jcook3195/75b915feccb653cfcf6f929d2f92f48b to your computer and use it in GitHub Desktop.
Fixing WooCommerce Featured Product Image Displaying Multiple Times in Gallery
<?php
defined( 'ABSPATH' ) || exit;
// Note: `wc_get_gallery_image_html` was added in WC 3.3.2 and did not exist prior. This check protects against theme overrides being used on older versions of WC.
if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
return;
}
global $product;
$post_thumbnail_id = $product->get_image_id();
$attachment_ids = $product->get_gallery_image_ids();
if ( $attachment_ids && $product->get_image_id() ) {
foreach ( $attachment_ids as $attachment_id ) {
if ( $attachment_id != $post_thumbnail_id) {
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $attachment_id ), $attachment_id ); // phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
}
}
}
@jcook3195
Copy link
Author

This is for when WooCommerce is displaying the featured image in the gallery multiple times. Not sure what causes the issue in the first place, but this fixes it. Copy the WooCommerce template file from 'woocommerce/templates/single-product/product-thumbnails.php' and replace code after the developer notes. All that I added to the template code was '$post_thumbnail_id = $product->get_image_id();' and the if statement in the for loop to make sure it does not show the featured image again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment