Automattic\WooCommerce\Blocks\Utils
ProductGalleryUtils::get_product_gallery_image_ids()
Get the product gallery image IDs.
Method of the class: ProductGalleryUtils{}
No Hooks.
Return
Array
. An array of unique image IDs for the product gallery.
Usage
$result = ProductGalleryUtils::get_product_gallery_image_ids( $product, $max_number_of_visible_images, $only_visible );
- $product(\WC_Product) (required)
- The product object to retrieve the gallery images for.
- $max_number_of_visible_images(int)
- The maximum number of visible images to return.
Default: 8 - $only_visible(true|false)
- Whether to return only the visible images.
Default: false
ProductGalleryUtils::get_product_gallery_image_ids() ProductGalleryUtils::get product gallery image ids code WC 9.4.2
public static function get_product_gallery_image_ids( $product, $max_number_of_visible_images = 8, $only_visible = false ) { // Main product featured image. $featured_image_id = $product->get_image_id(); // All other product gallery images. $product_gallery_image_ids = $product->get_gallery_image_ids(); // If the Product image is not set, we need to set it to a placeholder image. if ( '' === $featured_image_id ) { $featured_image_id = '0'; } // 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. $unique_image_ids = array_unique( array_merge( array( $featured_image_id ), $product_gallery_image_ids ) ); foreach ( $unique_image_ids as $key => $image_id ) { $unique_image_ids[ $key ] = strval( $image_id ); } if ( count( $unique_image_ids ) > $max_number_of_visible_images && $only_visible ) { $unique_image_ids = array_slice( $unique_image_ids, 0, $max_number_of_visible_images ); } // Reindex array. $unique_image_ids = array_values( $unique_image_ids ); return $unique_image_ids; }