添加具有多个属性的产品变体

ath*_*kar 5 c# wordpress woocommerce woocommerce-rest-api

我正在使用WoocommerceNET库(Nuget Link)来开发一个桌面应用程序,该应用程序会将产品从ERP数据库同步到Woocommerce eshop数据库。

我添加了属性size和color以及值,例如red,green,blue和s,m,l,xl。现在,我需要创建变体。

试过这个:

  List<VariationAttribute> vatrib = new List<VariationAttribute>()
            { new VariationAttribute() { name="Color",option="GREEN" },
              new VariationAttribute() { name="size",option="L" } };

       Variation var = new Variation() {
                             regular_price=1.0M,
                             visible=true,
                             attributes=vatrib,
                             stock_quantity=5,
                             manage_stock=true
                        };
       //... repeat for each variation ....

       List<Variation> varis = new List<Variation>();
       varis.Add(var);
       varis.Add(var1);
       varis.Add(var2);  ... and so on for all variations

       Product p = new Product()
                {
                    //options ....
                    type = "variable",
                    manage_stock = true,
                    in_stock = true,
                    attributes=attribs,
                    variations=varis,
                };
                await wc.Product.Add(p);
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误

无法将类型'System.Collections.Generic.List <WooCommerceNET.WooCommerce.v2.Variation>'隐式转换为'System.Collections.Generic.List <int>'

看起来Product 的Variation属性是一个包含变体ID的列表。

如何添加颜色和尺寸不同的新产品?

小智 2

创建产品。使用产品 ID 创建变体。

Product p = new Product()
{
    //options ....
    type = "variable",
    manage_stock = true,
   in_stock = true,
    attributes=attribs,
    //variations=varis,
};

//Returns the new product with the id.
p = await wc.Product.Add(p);

//Returns the new variation with the id
var1 = wc.Product.Variations.Add(var1, p.id.Value);

// Add the variation id to the product
p.Variations.Add(var1.id.Value);

//Update the product
wc.Product.Update(p.id.Value, p);
Run Code Online (Sandbox Code Playgroud)