10

Looking for a code which could potentially help me get the list of gallery images present in product in woocommerce.

Need them so i can use them in a custom single page design for woocommerce product page.

3 Answers 3

21
<?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');

    }?>
3
  • You should try to explain your code, it would be helpful. Dec 1, 2019 at 21:57
  • OK sure i can do it. Dec 1, 2019 at 22:06
  • 3
    This is the easiest and best answer.
    – RiotAct
    Dec 11, 2019 at 0:48
6

The get_gallery_image_ids() method on the product will return an array of image IDs.

global $product;
$gallery_images = $product->get_gallery_image_ids();

Then you can use functions like wp_get_attachment_image() for each ID to get HTML/URLs etc.

6

This is the complete function if you want all the images from the gallery, including the main image:

    public function getAllProducts( $request ){
    $products = new WP_Query([
        'post_type' => 'post'//,
            //'show_product_on_only_premium' => 'yes',
        ]);

    $args = array(
        'status' => 'publish',
    );
    $products = wc_get_products( $args );
    $tempArr = [];
    foreach($products as $product){
        $product_obj = json_decode($product-> __toString());
        $product_obj->img_src = wp_get_attachment_image_src($product_obj->image_id)[0];
        $images_ids = $product-> get_gallery_image_ids();

        $images_arr = [];
        for($i = 0, $j = count($images_ids); $i < $j;$i++ ){
            $image_query = wp_get_attachment_image_src($images_ids[$i]);
            $img = new StdClass;
            $img->src = $image_query[0];
            array_push($images_arr, $img);
        }
        $product_obj->gallery = $images_arr;
        array_push($tempArr, $product_obj);
    }
    return $tempArr;
} 

And if you want the variations: from: https://gist.github.com/Niloys7/17b88d36c1c38844a6cf2127c15dee63

<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();

foreach( $attachment_ids as $attachment_id ) 
{
  //Get URL of Gallery Images - default wordpress image sizes
  echo $Original_image_url = wp_get_attachment_url( $attachment_id );
  echo $full_url = wp_get_attachment_image_src( $attachment_id, 'full' )[0];
  echo $medium_url = wp_get_attachment_image_src( $attachment_id, 'medium' )[0];
  echo $thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' )[0];

  //Get URL of Gallery Images - WooCommerce specific image sizes
  echo $shop_thumbnail_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' )[0];
  echo $shop_catalog_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_catalog' )[0];
  echo $shop_single_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_single' )[0];

  //echo Image instead of URL
  echo wp_get_attachment_image($attachment_id, 'full');
  echo wp_get_attachment_image($attachment_id, 'medium');
  echo wp_get_attachment_image($attachment_id, 'thumbnail');
  echo wp_get_attachment_image($attachment_id, 'shop_thumbnail');
  echo wp_get_attachment_image($attachment_id, 'shop_catalog');
  echo wp_get_attachment_image($attachment_id, 'shop_single');
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.