Automattic\WooCommerce\Blocks\Utils

ProductGalleryUtils::get_product_gallery_images()public staticWC 1.0

When requesting a full-size image, this function may return an array with a single image. However, when requesting a non-full-size image, it will always return an array with multiple images. This distinction is based on the image size needed for rendering purposes:

  • "Full" size is used for the main product featured image.
  • Non-full sizes are used for rendering thumbnails.

Method of the class: ProductGalleryUtils{}

No Hooks.

Return

Array.

Usage

$result = ProductGalleryUtils::get_product_gallery_images( $post_id, $size, $attributes, $wrapper_class, $crop_images );
$post_id(int) (required)
Post ID.
$size(string)
Image size.
Default: 'full'
$attributes(array)
Attributes.
Default: array()
$wrapper_class(string)
Wrapper class.
Default: ''
$crop_images(true|false)
Whether to crop images.
Default: false

ProductGalleryUtils::get_product_gallery_images() code WC 9.4.2

public static function get_product_gallery_images( $post_id, $size = 'full', $attributes = array(), $wrapper_class = '', $crop_images = false ) {
	$product_gallery_images = array();
	$product                = wc_get_product( $post_id );

	if ( $product instanceof \WC_Product ) {
		$all_product_gallery_image_ids = self::get_product_gallery_image_ids( $product );

		if ( 'full' === $size || 'full' !== $size && count( $all_product_gallery_image_ids ) > 1 ) {
			$size = $crop_images ? self::CROP_IMAGE_SIZE_NAME : $size;

			foreach ( $all_product_gallery_image_ids as $product_gallery_image_id ) {
				if ( '0' !== $product_gallery_image_id ) {
					if ( $crop_images ) {
						self::maybe_generate_intermediate_image( $product_gallery_image_id, self::CROP_IMAGE_SIZE_NAME );
					}

					$product_image_html = wp_get_attachment_image(
						$product_gallery_image_id,
						$size,
						false,
						$attributes
					);
				} else {
					$product_image_html = self::get_product_image_placeholder_html( $size, $attributes, $crop_images );
				}

				if ( $wrapper_class ) {
					$product_image_html = '<div class="' . $wrapper_class . '">' . $product_image_html . '</div>';
				}

				$product_image_html_processor = new \WP_HTML_Tag_Processor( $product_image_html );
				$product_image_html_processor->next_tag( 'img' );
				$product_image_html_processor->set_attribute(
					'data-wc-context',
					wp_json_encode(
						array(
							'imageId' => $product_gallery_image_id,
						),
						JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
					)
				);

				$product_gallery_images[] = $product_image_html_processor->get_updated_html();
			}
		}
	}

	return $product_gallery_images;
}