XiaoFaye / WooCommerce.NET

A .NET Wrapper for WooCommerce/WordPress REST API
MIT License
391 stars 217 forks source link

I can't retrieve the stock code of the products. #713

Open ghost opened 1 year ago

ghost commented 1 year ago

Hello everyone;

How can i listing all SKU?

ghost commented 1 year ago

solved

mcupryk commented 1 year ago

@palmeItGit please paste your solution in case some one needs it. Thanks

ghost commented 1 year ago

Hi! you guessed it, I don't remember :) but I found a code like this: Firstly you must (get all product(s) / filtered product(s) after than

private async Task<bool> CheckExistOnWoo(string ArtikelNr)
        {
            //First step get source products data
            bool isExist = false;
            RestAPI rest = new RestAPI(webConnecs.StoreAddress, webConnecs.Key, webConnecs.Secret, jsonDeserializeFilter: trimstr);
            WCObject wc = new WCObject(rest);

            List<Product> products = new List<Product>();

            #region GetProductWithIdFromWeb
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("per_page", "100");
            dictionary.Add("sku", ArtikelNr);
            int pageNumber = 1;
            dictionary.Add("page", pageNumber.ToString());
            bool endWhile = false;
            while (!endWhile)
            {
                var productsTemp = await wc.Product.GetAll(dictionary);

                if (productsTemp.Count > 0)
                {
                    products.AddRange(productsTemp);
                    pageNumber++;
                    dictionary["page"] = pageNumber.ToString();
                }
                else
                {
                    endWhile = true;
                }
            }
            #endregion

            //Now you can check sku data...
            foreach (Product p in products)
            {
                if (p.sku == ArtikelNr)
                {
                    isExist = true;
                    break;
                }
            }

            return isExist;
        }

i hope you can found a sloution...

macupryk commented 1 year ago

Nice work. Thanks again for the update.