When I try to fetch products using the official REST API, returned product contains the a property named images which is an array and contains the URL to image of product. But when I fetch using this method, it fetches other details but not the image.

    $productsQuery= wc_get_products(array(
        'limit' => 10,
        'status' => 'publish',
    $products = array();
    foreach ($productsQuery as $product) {
        $products[] = $product->get_data();
    return new WP_REST_Response($products, 200);

how can I make this to return the images property as well.

1 Answer 1


The WC_Product object has an image_id (string) and an gallery_image_ids (array) attribute for product image and product's gallery images. Both of them are attachment ids, so you should get the image src from it's attachment id with the wp_get_attachment_image_url() method and add it to your $products array.

  • I already tried a similar solution, but getting this error Call to undefined method WC_Product_Simple::wp_get_attachment_image_url()
    – Lint
    Jul 25, 2020 at 9:46
  • 3
    There is no such method WC_Product_Simple::wp_get_attachment_image_url(). You should call the method by itself. $image_src = wp_get_attachment_image_url( $attachment_id ). Read the method docs.
    – Had1z
    Jul 25, 2020 at 9:52
  • alright, I did, but the problem is that It gives the html tag not although it includes the src, but can I somehow get just URL of image ?
    – Lint
    Jul 25, 2020 at 13:15
  • @Lint I think you called wp_get_attachment_image() function instead. It returns the html tag. The function that I mentioned is wp_get_attachment_image_url() which gives you the source of image.
    – Had1z
    Jul 25, 2020 at 13:29
  • Alright, thanks. Just a little question. When I use wc_get_product(PRODUCT_ID) function, it returns some properties not all. But when I retrieve the same product using WooCommerce REST API URL. It returns all the data that is returned by above mentioned method, also it includes images and other attributes automatically. Can I get the same properties using a local function and not the rest api url ? Thanks in advance
    – Lint
    Jul 25, 2020 at 13:41

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.