Tag: How to Remove/Hide Featured Image From WooCommerce Gallery

How to Remove/Hide Featured Image From WooCommerce Gallery

Use this code in functions.php file.

/**
 * Exclude the featured image from appearing in the product gallery, if there's a product gallery.
 *
 * @param array $html Array of html to output for the product gallery.
 * @param array $attachment_id ID of each image variables.
 */
function rm_woocommerce_remove_featured_image( $html, $attachment_id ) {

	global $post, $product;

	// Get the IDs.
	$attachment_ids = $product->get_gallery_image_ids();

	// If there are none, go ahead and return early - with the featured image included in the gallery.
	if ( ! $attachment_ids ) {
		return $html;
	}

	// Look for the featured image.
	$featured_image = get_post_thumbnail_id( $post->ID );

	// If there is one, exclude it from the gallery.
	if ( is_product() && $attachment_id === $featured_image ) {
		$html = '';
	}

	return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'rm_woocommerce_remove_featured_image', 10, 2 );