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.

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 10.9.4

function wc_render_product_image_template_for_image_ids( WC_Product $product, $image_ids ): string {
	$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 );

	try {
		return wc_render_product_image_template_for( $product );
	} finally {
		$remove_overrides();
	}
}