skip to Main Content

I’m working with WooCommerce api v3, and when I call

$woocommerce->get('products/categories');

It returns only 10 results. I read the documentation and there is no specification about how to list all categories from api, but I have more than 40 categories on wordpress.

Someone already had this problem before?

Thanks!

2

Answers


  1. you can pass more parameters to the function as you can see in the documentation. This should work:

    $woocommerce->get('products/categories', array( 'per_page' => -1 ) );

    Looks like -1 doesn’t work to display all the categories, so a positive integer needs to be in place instead.

    $woocommerce->get('products/categories', array( 'per_page' => 99 ) );

    Login or Signup to reply.
  2. The below code will help you to get all categories at once

    // find category at wp-Store with name and return it
    
    async function check_wp_category(api, gisCategory) {
      let category = {
        wp_category_id: -1,
        isFound: false,
      };
      let flag = true;
    let page_no = 1;
    while (flag) {
        await api.get("products/categories?page=" + page_no)
            .then((response) => {
                if (response.data.length == 0) {
                    flag = false;
                    return category;
                }
    
                for (let i = 0; i < response.data.length; i++) {
                    if (gisCategory === response.data[i].name) {
                        console.log("category id in loop" + response.data[i].id);
                        category.wp_category_id = response.data[i].id;
                        console.log("wp_category_id value in loop" + category.wp_category_id);
                        category.isFound = true;
                        return category;
                    }
                }
            })
            .catch((error) => {
                console.log(error.response.data);
            });
            page_no++;
        }
        return category;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search