admin管理员组

文章数量:1331230

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.

Share Improve this question asked Jul 25, 2020 at 8:21 LintLint 1131 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: woocommerce offtopicwcgetproducts() not return the images details