K
K
Konstantin2021-09-23 23:17:16
API
Konstantin, 2021-09-23 23:17:16

How to get all products by API?

According to the documentation, I want to get all the products, but no matter how I change the per_page parameter, it doesn’t display all the products and I can’t find out how many pages with products.

Question how to get all products by api?

const getProductsFromSite = async() => {
    // Parse paginated page
    let paginate = [];
    let page = 1;

    let response = await WooCommerce.get(`products?page=${page}`, {
        per_page: 100
    })

    while (response.data.length >= 1) {
        paginate = [...paginate, ...response.data]
        console.log({id: response.data[0].id, total: paginate.length, page})

        page++;
        response = await WooCommerce.get(`products?page=${page}`, {per_page: 100})
    }


    let products = await Promise.all(response.data.map( item => {
       const {id, sku, regular_price, manage_stock, stock_quantity} = item;
       if(sku) return {id, sku, regular_price, manage_stock, stock_quantity}
    }).filter(Boolean))

    return products 
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question