Automattic\WooCommerce\Blocks\Utils

ProductGalleryUtils::get_product_gallery_image_idspublic staticWC 1.0

Get the product gallery image IDs.

Метод класса: ProductGalleryUtils{}

Хуков нет.

Возвращает

Массив. An array of unique image IDs for the product gallery.

Использование

$result = ProductGalleryUtils::get_product_gallery_image_ids( $product );
$product(WC_Product) (обязательный)
The product object to retrieve the gallery images for.

Код ProductGalleryUtils::get_product_gallery_image_ids() WC 10.5.2

public static function get_product_gallery_image_ids( $product ) {
	$product_image_ids = array();

	// Main product featured image.
	$featured_image_id = $product->get_image_id();

	if ( $featured_image_id ) {
		$product_image_ids[] = $featured_image_id;
	}

	// All other product gallery images.
	$product_gallery_image_ids = $product->get_gallery_image_ids();

	if ( ! empty( $product_gallery_image_ids ) ) {
		// We don't want to show the same image twice, so we have to remove the featured image from the gallery if it's there.
		$product_image_ids = array_unique( array_merge( $product_image_ids, $product_gallery_image_ids ) );
	}

	// If the Product image is not set and there are no gallery images, we need to set it to a placeholder image.
	if ( ! $featured_image_id && empty( $product_gallery_image_ids ) ) {
		$product_image_ids[] = '0';
	}

	foreach ( $product_image_ids as $key => $image_id ) {
		$product_image_ids[ $key ] = strval( $image_id );
	}

	// Reindex array.
	$product_image_ids = array_values( $product_image_ids );

	return $product_image_ids;
}