Get all products from Woocommerce using REST API

元气小坏坏 提交于 2019-11-28 11:54:01

问题


I am trying to retrieve all products using rest api. I have read this question. I am using postman to make calls. Here is my query

https://squatwolf.com/wp-json/wc/v2/products?filter[posts_per_page] =-1

The query shows only 10 results.


回答1:


This isn't the latest API endpoint:

/wc-api/v3/products?filter[limit]=

You have to fetch page per page to get all the products:

$page = 1;
$products = [];
$all_products = [];
do{
  try {
    $products = $wc->get('products',array('per_page' => 100, 'page' => $page));
  }catch(HttpClientException $e){
    die("Can't get products: $e");
  }
  $all_products = array_merge($all_products,$products);
  $page++;
} while (count($products) > 0);



回答2:


I was able to find the data using the following solution,

https://squatwolf.com/wc-api/v3/products?filter[limit] =-1



回答3:


The filter parameter is no longer supported, see the Docs. So you really need to loop the pages.

Here is how to get all products in JavaScript (for a Gutenberg Block store):

let allProducts = [],
    page = 1

while (page !== false) {
    const products = yield actions.receiveProducts(`/wc-pb/v3/products?per_page=100&page=${page}`)

    if (products.length) {
        allProducts = allProducts.concat(products)
        page++
    } else {
        page = false // last page
    }
}

return actions.setProducts(allProducts)



回答4:


/wp-json/wc/v2/products

and

/wc-api/v3/products

both seems to work, but to get certain number of products I'm using

/wc-api/v3/products?filter[limit]=

put the number of products there. -1 for all products



来源:https://stackoverflow.com/questions/48476534/get-all-products-from-woocommerce-using-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!