Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

woocommerce_single_product_image_thumbnail_html filter not fired anymore #9618

Closed
webaxones opened this issue May 27, 2023 · 3 comments · Fixed by #9630
Closed

woocommerce_single_product_image_thumbnail_html filter not fired anymore #9618

webaxones opened this issue May 27, 2023 · 3 comments · Fixed by #9630
Assignees
Labels
block: product image gallery Issues related to the Product Image Gallery block. type: bug The issue/PR concerns a confirmed bug. type: has use case For any issues or PRs that include a use case description for a feature.

Comments

@webaxones
Copy link

webaxones commented May 27, 2023

The woocommerce_single_product_image_thumbnail_html filter is no longer fired on the product page using the product image gallery block since version 10.3.0.
It was possible to use this filter in the previous version 10.2.0, for example, to display in the gallery only the images of the variations if the product is variable.
WP 6.2.2
WC 7.7.0
WC Blocks 10.3.0

To reproduce:

function only_displays_tom_and_jerry_in_the_product_photo_gallery( string $html, int $attachment_id ): string {
    return '<p>Tom & Jerry</p>';
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', '\only_displays_tom_and_jerry_in_the_product_photo_gallery', 99, 2 );

I think maybe that it's related to this commit but I'm not sure:
81a2912

@webaxones
Copy link
Author

My use case:

My client uses the gallery to display different photos of various quality. Those corresponding to the variations come from suppliers, the others are made in-house.
I am currently working on the redesign of the site. I'm not going to make him modify thousands of products.
I have decided to separate the photos for aesthetic reasons: I keep the professional photos in the main gallery, and I display the other photos in a secondary gallery further down on the product page.
It's not just for aesthetic reasons: the photos made in-house exactly correspond to the product being sold.

So I just filter images like this for the main gallery:

/**
 * Only displays product variation images in the product photo gallery
 *
 * @param  string $html HTML of the image
 * @param  int $attachment_id ID of the image
 *
 * @return string HTML of the image
 */
function only_displays_product_variation_images_in_the_product_photo_gallery( string $html, int $attachment_id ): string {
    global $product;

	if ( ! $product->is_type( 'variable' ) ) {
		return $html;
	}

	$variations_images_ids = [];
	$variations = $product->get_available_variations();
	foreach ( $variations as $variation ) {
		$variations_images_ids[] = $variation['image_id'];
	}

    return ( ! empty( $variations_images_ids ) && in_array( $attachment_id, $variations_images_ids ) ) ? $html : '';
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', __NAMESPACE__ . '\only_displays_product_variation_images_in_the_product_photo_gallery', 99, 2 );

And in a custom block, I display photos not linked to variations.

Result:
gallery

But broken in WC Blocks 10.3.0 :D

@nerrad
Copy link
Contributor

nerrad commented May 29, 2023

Thanks for sharing the use-case @webaxones - I definitely can see how the filters are helpful while the components of the image gallery block are not exposed in the editor. We have some plans for the Product Image Gallery which should address that use-case. Until then, the filters are being restored in the PR @nefeline did. Thanks for reporting!

@nerrad nerrad added the type: has use case For any issues or PRs that include a use case description for a feature. label May 29, 2023
@webaxones
Copy link
Author

@nerrad Yes, I saw the PR from @nefeline, and I just tested it now, and my hook is working perfectly again. Thank you both for the promptness. ❤️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: product image gallery Issues related to the Product Image Gallery block. type: bug The issue/PR concerns a confirmed bug. type: has use case For any issues or PRs that include a use case description for a feature.
Projects
None yet
3 participants