Woocommerce get image galleries by product id
Just to clarify, the above answers will get all but the thumbnail (main) image.
To prepend the thumbnail, use this:
$product_id = '652';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();
//This adds the thumbnail id as the first element of the array
array_unshift($attachment_ids, get_post_thumbnail_id($product_id));
print_r($attachment_ids);
I have already tested this and it works
<?php
$product_id = '14';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display the image URL
echo $Original_image_url = wp_get_attachment_url( $attachment_id );
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
?>
Use the get_gallery_image_ids()
for get the Gallery Attachment Ids because get_gallery_attachment_ids()
deprecated since WooCommerce 3.0.0