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.
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
Reset to default 2The 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.