Get all products from Woocommerce using REST API
Asked Answered
R

10

8

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.

Rhymester answered 27/1, 2018 at 14:0 Comment(0)
R
7

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

https://squatwolf.com/wc-api/v3/products?filter[limit] =-1
Rhymester answered 30/1, 2018 at 13:36 Comment(0)
R
10

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);
Ruthi answered 27/7, 2018 at 9:38 Comment(2)
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));Bosnia
Thank you @ManuelGuzman it blows my mind that when ordering by date it can lose products along the way. Ordering by id fixed my problemKensell
R
7

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

https://squatwolf.com/wc-api/v3/products?filter[limit] =-1
Rhymester answered 30/1, 2018 at 13:36 Comment(0)
S
5

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 );
Sakmar answered 31/7, 2020 at 7:5 Comment(0)
I
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)
Indices answered 11/3, 2019 at 17:23 Comment(0)
D
4

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');
Dryfoos answered 16/12, 2019 at 18:30 Comment(0)
V
1

simple!!! you can use any number in place of 100. Its just the parameter written in https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products

https://squatwolf.com/wp-json/wc/v2/products?per_page=100

Victorinavictorine answered 30/6, 2020 at 17:9 Comment(0)
W
0
/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

Wollastonite answered 8/2, 2018 at 14:46 Comment(0)
P
0

for nodeJS:

  export const getAllProducts = async () => {
    let allProducts = []
    let breakLoop = false
    let page = 1
    while (!breakLoop) {
        const products = await api.get("products", { page })
            .then((response) => {
                return response.data
            })
            .catch((error) => {
                console.log(error.response.data);
            })
        if (products.length === 0 || !products){
            breakLoop = true
        } else {
            allProducts = allProducts.concat(products)
            page = page + 1
        }
    }

    return allProducts
 }
Photomechanical answered 19/5, 2022 at 15:49 Comment(1)
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.Lithograph
S
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']);
Sheathing answered 28/6, 2022 at 11:51 Comment(0)
R
0

It may be too late to answer this question, but almost all the answers here are using the /wc-api/v3/products endpoint, which is now deprecated and will be removed in WooCommerce v9.

As mentioned in the WooCommerce REST API documentation You can use something like this instead:
https://example.com/wp-json/wc/v3/products?per_page=100

Rexer answered 24/7 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.