2

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Ashish Patil
  • 313
  • 1
  • 6
  • 15

5 Answers5

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.

Bogdan Stoica
  • 4,349
  • 2
  • 23
  • 38
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);

Source

Leviand
  • 2,745
  • 4
  • 29
  • 43
spartanz51
  • 311
  • 3
  • 9
0

http://localhost:8888/wp-json/wc/store/products

Try this endpoint for list products. You can get Categories and Prices as well.

0

If you are using a Laravel package like codexshaper/laravel-woocommerce or corcel/woocommerce or some other package, be careful with the product type in your WooCommerce website. By default when you call $products = Product::all(); it just gives you the products with simple product-type.

This is the list of default product-type in woocommece:

'simple', 'grouped', 'external', 'variable', 'external', 'subscription', 'variable-subscription'

So You have to specify the product-type of you want to get other products.

$product = WooProduct::all(['per_page'=> '100','type'=>'simple']);
Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40
0

The only way I found is iterating over all pages (1st, 2nd, etc...) of products using the below wp-cli command until the returned list is empty.

wp wc product list --page={page} --format=json --fields=id,description

It mimics the way we work in wp admin.

rok
  • 9,403
  • 17
  • 70
  • 126