'Get all products from Woocommerce using REST API
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.
Solution 1:[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);
Solution 2:[2]
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 3:[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:[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');
Solution 5:[5]
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
Solution 6:[6]
/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
Solution 7:[7]
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
allProducts = allProducts.concat(products)
page = page + 1
} return allProducts }
Solution 8:[8]
You should put your resulting dataframes in a list and use a for loop to iterate over them one by one. Putting the relevant code in a function also helps you to not repeat yourself.
def plot_counts(dataframe):
dataframe['Sex'].value_counts()
sns.countplot(data=dataframe, x='Sex')
df = pd.read_csv("heart.csv")
plot_counts(df)
Clv = df.loc[0:302, :]
Hng = df.loc[303:(303+293), :]
Swtz = df.loc[(303+294):(303+294+122), :]
Lb = df.loc[(303+294+123):(303+294+123+199), :]
Stl = df.loc[(303+294+123+200):, :]
df_list = [Clv, Hng, Swtz, Lb, Stl]
for dataframe in df_list:
plot_counts(dataframe)
If you only need to make the 'Sex' column once, you can eliminate the function definition and just call sns.countplot() directly:
df = pd.read_csv("heart.csv")
df['Sex'].value_counts()
sns.countplot(data=df, x='Sex')
Clv = df.loc[0:302, :]
Hng = df.loc[303:(303+293), :]
Swtz = df.loc[(303+294):(303+294+122), :]
Lb = df.loc[(303+294+123):(303+294+123+199), :]
Stl = df.loc[(303+294+123+200):, :]
df_list = [Clv, Hng, Swtz, Lb, Stl]
for dataframe in df_list:
sns.countplot(data=dataframe, x='Sex')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
