[英]WooCommerce.NET add product with category/subcategory
I am looking for some "inspiration", on how to add a product to my WC store.我正在寻找一些关于如何将产品添加到我的 WC 商店的“灵感”。
I can add a simple product:我可以添加一个简单的产品:
Product prod = new Product()
{
sku = SKU,
name = Name,
description = Description,
regular_price = Price,
manage_stock = true,
stock_quantity = quantity,
};
wc.Product.Add(prod).GetAwaiter().GetResult();
But how do I add this product to category, when I only have a category name?但是,当我只有一个类别名称时,如何将此产品添加到类别中?
Additional problem is that I have a category structure, where many sub categories have the same name.另一个问题是我有一个类别结构,其中许多子类别具有相同的名称。
Example:例子:
main category: MALE sub categories: shoes, t-shirts, underwear, accessories
主类:男 子类:鞋、T恤、内衣、配饰
main category: FEMALE sub categories: shoes, t-shirts, underwear, accessories
主类:FEMALE 子类:鞋子、T恤、内衣、配饰
main category: CHILDREN sub categories: shoes, t-shirts, underwear
主类:儿童 子类:鞋、T恤、内衣
I was importing the products database via CSV, where I named the category:我通过 CSV 导入产品数据库,我在其中命名了类别:
CATEGORY>SUBCATEGORY
类别>子类别
So?所以? Any Ideas?
有任何想法吗?
Thanks!谢谢!
EDIT: From the answer of Matthias S. I have got some good insight.编辑:从 Matthias S. 的回答中,我得到了一些很好的见解。 But I would need a more universal solution.
但我需要一个更通用的解决方案。 I need to get the proper category id, from only the names of categories.
我需要仅从类别名称中获取正确的类别 ID。 I am thinking in a direction of loops, where I would: 1. loop: find top category by name -2.
我正在考虑循环的方向,我会:1.循环:按名称-2查找顶级类别。 loop: find firs subcategory by name --3.
循环:按名称查找第一个子类别--3。 loop: find next subcategory by name ----... Would that be even possible?
循环:按名称查找下一个子类别----...这可能吗?
Thanks!谢谢!
A category is more than just a name for WC.类别不仅仅是 WC 的名称。 You need to create the same category tree inside your shop as you have on your input side.
您需要在商店内创建与输入端相同的类别树。 A WC category therefor has an unique ID, the display name and a parentID.
因此,WC 类别具有唯一 ID、显示名称和 parentID。
For your given example you could create the MALE category via api对于您给定的示例,您可以通过 api 创建 MALE 类别
ProductCategory newCategoryMale = new ProductCategory
{
name = "MALE"
};
ProductCategory maleCategory = await wooCommerce.Category.Add(newCategoryMale)
and the returned category will have an id you can then use to create the sub categories.并且返回的类别将有一个 id,然后您可以使用它来创建子类别。
ProductCategory newCategoryShoes = new ProductCategory
{
name = "shoes",
parent = maleCategory.id
};
ProductCategory shoes = await wooCommerce.Category.Add(newCategoryShoes)
Now that you have the category tree structure as well as the related ids, you can create your products straight with the categories assigned.现在您已经拥有了类别树结构以及相关的 ID,您可以使用分配的类别直接创建您的产品。
Product prod = new Product()
{
sku = SKU,
name = Name,
description = Description,
regular_price = Price,
manage_stock = true,
stock_quantity = quantity,
categories = new List<ProductCategoryLine>
{
new ProductCategoryLine
{
id = shoes.id
}
}
};
Edit:编辑:
To get the id of an already existing category you may use要获取现有类别的 id,您可以使用
List<ProductCategory> existingCategories = await wooCommerce.Category.GetAll();
to get all categories already setup in your shop.获取您商店中已设置的所有类别。 When looking for Male->Shoes you need to search that list for an root level category named "MALE" and find a category named "shoes" where the parentId equals your male-categorys id.
在查找 Male->Shoes 时,您需要在该列表中搜索名为“MALE”的根级别类别,并找到一个名为“shoes”的类别,其中 parentId 等于您的男性类别 id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.