Woocommerce.NET + C#: 从 SKU 获取产品 ID
Woocommerce.NET + C#: get the ID of product from SKU
我正在使用带有 Woocommerce.NET 包装器的 C# 制作一个小应用程序。
我想编辑产品,但我没有产品 ID - 只有 SKU。
从 SKU 编号获取产品 ID 的最简单方法是什么?
我试过这个:
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;
}
编辑:我更改了代码,将其转换为 int,现在可以使用了!
foreach (var p in products)
{
if (p.sku == SKU)
{
IDfromSKU = Convert.ToInt32(p.id);
};
};
但问题是,我只得到 10 种产品的列表 - 不是全部。
有没有这样的设置?
我的问题仍然存在 - 是否有更直接的方法?
编辑 2:
我已经实现了你的答案,代码有效,但速度非常慢。
约 5000 种产品需要 2-3 分钟。
我可以做些什么来加快速度?
编辑 3:
抱歉,没有做足够的测试 - 第二个答案很棒!
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;
}
认为我的问题已解决!
谢谢大家!
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;
};
}
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;
}
我正在使用带有 Woocommerce.NET 包装器的 C# 制作一个小应用程序。 我想编辑产品,但我没有产品 ID - 只有 SKU。
从 SKU 编号获取产品 ID 的最简单方法是什么?
我试过这个:
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;
}
编辑:我更改了代码,将其转换为 int,现在可以使用了!
foreach (var p in products)
{
if (p.sku == SKU)
{
IDfromSKU = Convert.ToInt32(p.id);
};
};
但问题是,我只得到 10 种产品的列表 - 不是全部。 有没有这样的设置? 我的问题仍然存在 - 是否有更直接的方法?
编辑 2:
我已经实现了你的答案,代码有效,但速度非常慢。 约 5000 种产品需要 2-3 分钟。
我可以做些什么来加快速度?
编辑 3:
抱歉,没有做足够的测试 - 第二个答案很棒!
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;
}
认为我的问题已解决!
谢谢大家!
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;
};
}
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;
}