How to get all products from woo-commerce?

淺唱寂寞╮ 提交于 2019-12-12 17:07:50

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/42201427/how-to-get-all-products-from-woo-commerce

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