Automattic\WooCommerce\Blocks\Utils
ProductGalleryUtils::get_product_gallery_image_ids
Get the product gallery image IDs.
Method of the class: ProductGalleryUtils{}
No Hooks.
Returns
Array. An array of unique image IDs for the product gallery.
Usage
$result = ProductGalleryUtils::get_product_gallery_image_ids( $product );
- $product(WC_Product) (required)
- The product object to retrieve the gallery images for.
ProductGalleryUtils::get_product_gallery_image_ids() ProductGalleryUtils::get product gallery image ids code WC 10.5.0
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;
}