Get all products from Woocommerce using REST API

39,811

Solution 1

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

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

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

Solution 3

This worked for me. With API v3

/wc-api/v3/products?

Retrieve first 500 products by default or

/wc-api/v3/products?per_page=900

To get 900 products

function maximum_api_filter($query_params) {
   $query_params['per_page']['maximum'] = 10000;
   $query_params['per_page']['default'] = 500;
   return $query_params;
}
add_filter('rest_product_collection_params', 'maximum_api_filter', 10, 1 );

Solution 4

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)

Solution 5

add code to function.php

function maximum_api_filter($query_params) {
    $query_params['per_page']["maximum"]=100000;
    return $query_params;
}

add_filter('rest_product_collection_params', 'maximum_api_filter');
Share:
39,811
Sagar Bahadur Tamang
Author by

Sagar Bahadur Tamang

A computer engineering student who is interested in anything his brain finds interesting and hard.

Updated on May 20, 2022

Comments

  • Sagar Bahadur Tamang
    Sagar Bahadur Tamang about 2 years

    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.

  • Manuel Guzman
    Manuel Guzman about 2 years
    Since the default orderby is 'date' the records can be returned out of order as you page, leading to duplicated and missing records. Make sure to sort by id to get all records: $products = $wc->get('products',array('ordery' => 'id', 'per_page' => 100, 'page' => $page));
  • Daan
    Daan about 2 years
    Thank you @ManuelGuzman it blows my mind that when ordering by date it can lose products along the way. Ordering by id fixed my problem
  • Honsa Stunna
    Honsa Stunna about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.