WooCommerce.NET icon indicating copy to clipboard operation
WooCommerce.NET copied to clipboard

Product.GetAll not working with per_page=100

Open maxbale opened this issue 3 years ago • 2 comments

Hi, I'm using the following function to obtain the list of all the products in WooCommerce:

    public static async Task<List<Product>> GetAllProducts(WCObject wc, string stLogFileName)
    {
        List<Product> lista = new List<Product>();
        try
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("per_page", "100");
            int pageNumber = 1;
            dic.Add("page", pageNumber.ToString());
            bool endWhile = false;
            while (!endWhile)
            {
                var listaTemp = await wc.Product.GetAll(dic);
                if (listaTemp.Count > 0)
                {
                    lista.AddRange(listaTemp);
                    pageNumber++;
                    dic["page"] = pageNumber.ToString();
                }
                else
                {
                    endWhile = true;
                }
            }
        }
        catch (Exception ex)
        {
            Log.Write($"Impossibile ottenere da WooCommerce la lista degli articoli: {ex.Message}", stLogFileName);
        }
        return lista;
    }

The products in the site are only 6, but if I run the code with dic.Add("per_page", "100") I obtain a void list (no exception). Setting dic.Add("per_page", "99"); I obtain the correct list of 6 product. Can you verify this?

I also have different results also in Category.GetAll and Tag.GetAll depending on the value of per_page. Can you help me? Thank you

  • Wordpress version: 5.8.1
  • WooCommerce version: 5.8.0
  • WooCommerce.NET version: 0.8.3

maxbale avatar Oct 27 '21 09:10 maxbale

Have you tried with a clean WC installation? I can't replicate.

kevinamorim avatar Nov 11 '21 15:11 kevinamorim

I guess by default woocommerce API returning only 6 products, try to chage value in wordpress by adding bellow code in functions.php

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

bulkasergei avatar Nov 16 '21 15:11 bulkasergei