wc_render_product_image_template_for_image_ids() │ WC 10.8.0

Render the product-image template using a caller-supplied image set instead of the product's stored featured + gallery.

Used by the variation gallery swap path: the variation has its own ordered list of images, but we still want the parent product's gallery template to render them so theme overrides and woocommerce_product_thumbnails apply uniformly. We achieve this by hooking the product's get_image_id / get_gallery_image_ids filters for the duration of the render; the template sees the substituted values and is none the wiser.

No Hooks.

Returns

string. Rendered gallery HTML, or an empty string for a re-entrant render of the same product.

Usage

wc_render_product_image_template_for_image_ids( $product, $image_ids ): string;
$product(WC_Product) (required)
Product being rendered.
$image_ids(mixed) (required)
Image IDs to substitute. Will be normalized.

Changelog

Since 10.8.0 Introduced.

wc_render_product_image_template_for_image_ids() code WC 11.1.2

function wc_render_product_image_template_for_image_ids( WC_Product $product, $image_ids ): string {
	static $rendering_product_galleries = array();

	$product_id = $product->get_id();
	if ( isset( $rendering_product_galleries[ $product_id ] ) ) {
		return '';
	}

	$normalized  = array_values( array_unique( array_map( 'intval', array_filter( (array) $image_ids ) ) ) );
	$featured_id = $normalized[0] ?? 0;
	$gallery_ids = array_slice( $normalized, 1 );

	$remove_overrides = wc_apply_product_image_overrides( $product, $featured_id, $gallery_ids );

	$rendering_product_galleries[ $product_id ] = true;

	try {
		return wc_render_product_image_template_for( $product );
	} finally {
		$remove_overrides();
		unset( $rendering_product_galleries[ $product_id ] );
	}
}