DontKnow
DontKnow

Reputation: 64

Woocommerce.NET + C#: get the ID of product from SKU

I am making a small app using C# with Woocommerce.NET wrapper. I would like to edit a product, but I do not have the ID of a product - only SKU.

What would be the easiest way to get product ID from SKU number?

I have tried this:

RestAPI rest = new RestAPI(url, key, secret); 
WCObject wc = new WCObject(rest);  

var products = wc.Product.GetAll().GetAwaiter().GetResult();

products.ForEach(p => if p.sku = productSKU)
    {
        var productIDfromSKU = p.id;
    }

EDIT: I have changed the code, to convert to int and it now works!

foreach (var p in products)
     {
      if (p.sku == SKU)
           {
            IDfromSKU = Convert.ToInt32(p.id);

             };
       };

But the problem is, that I only get a list of 10 products - not all. Is there a setting for that? My question remain - Is there a more straight forward way?

EDIT 2:

I have implemented your answers, the code works, but it is terribly slow. 2-3 minutes on ~5000 products.

What can I do to speed things up?

EDIT 3:

Sorry, have not done enough testing - the second answer great!

string SKU = "***wanted SKU***";
List<Product> products  = new List<Product>();
Dictionary<string, string> pDic = new Dictionary<string, string>();
pDic.Add("sku", SKU);

int productIDfromSKU = 0;
string productNamefromSKU  = "";
products  = await wc.Product.GetAll(pDic);
if (products.Count > 0)
{
    productIDfromSKU = Convert.ToInt32(products[0].id);
    productNfromSKU = products[0].name;
}

Consider my problem solved!

Thank you all!

Upvotes: 2

Views: 3484

Answers (2)

Ssshark
Ssshark

Reputation: 46

        RestAPI rest = new RestAPI(url, key, secret); 
        WCObject wc = new WCObject(rest);

        string SKU = "***wanted SKU***";
        List<Product> products  = new List<Product>();
        Dictionary<string, string> pDic = new Dictionary<string, string>();
        pDic.Add("sku", SKU);

        int productIDfromSKU = 0;
        string productNamefromSKU  = "";
        products  = await wc.Product.GetAll(pDic);
        if (products.Count > 0)
        {
            productIDfromSKU = Convert.ToInt32(products[0].id);
            productNfromSKU = products[0].name;
        }

Upvotes: 2

Ssshark
Ssshark

Reputation: 46

        RestAPI rest = new RestAPI(url, key, secret); 
        WCObject wc = new WCObject(rest);              

        List<Product> products = new List<Product>();
        Dictionary<string, string> dic1 = new Dictionary<string, string>();
        dic1.Add("per_page", "100");
        int pageNumber1 = 1;
        dic1.Add("page", pageNumber1.ToString());

        bool endWhile1 = false;
        while (!endWhile1)
        {
            var productsTemp = await wc.Product.GetAll(dic1);
            if (productsTemp.Count > 0)
            {
                products.AddRange(productsTemp);
                pageNumber1++;
                dic1["page"] = pageNumber1.ToString();
            }
            else
            {
                endWhile1 = true;
            }
        }

        foreach (Product p in products)
        {
           if (p.sku == SKU)
           {
               IDfromSKU = Convert.ToInt32(p.id);
               break;
           };
        }

Upvotes: 1

Related Questions