Category

How to get image gallery in Woocommerce?

Recently came through a problem, where i had to build a custom single page for the Products. The requirement was to use the featured image as a the main image while the gallery images needed to be placed at the bottom.

Getting the featured image for a WooCommerce product is fairly simple, it works very similar to how you get an image for a post. You can also reference: How to get featured images outside loop in WordPress?

For Woocommerce Gallery, here is the snippet of code that can help:

$product_id = ’14’; // id of the product, you can also use get_the_id() for this within the single page
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_attachment_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’);

}