How to get all products from woo-commerce?

2019-07-19 03:37发布

I have setup Woo-Commerce which have more than 1000 products. Using Woocommerce rest api php library I am trying to get all products.

But it gives me 10 products. If I use filter[limit] it gives me around 400 products not more than this.

$res = $wc_api->get_products(array( 'filter[limit]' => 400 ) );

Can anyone say me how can I get all products from woocommerce?

2条回答
可以哭但决不认输i
2楼-- · 2019-07-19 04:24

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);

Source

查看更多
趁早两清
3楼-- · 2019-07-19 04:31

For the latest version of the WC API, use

$products = $client->products->get( '', ['filter[limit]' => -1] );

If it's not working try replacing the limit with posts_per_page as follows:

'filter[posts_per_page]'=>-1

Depending on your server specs and the total number of products this type of query might need a lot of memory so if you get any error or the query doesn't finish be sure that the memory limit in php.ini is high enough.

查看更多
登录 后发表回答