首页 文章

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

提问于
浏览
4

我正在使用WoocommerceNET库(Nuget Link)开发一个桌面应用程序,它将ERP数据库中的产品同步到Woocommerce eshop数据库 .

我添加了属性大小和颜色的值,例如红色,绿色,蓝色和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);

但我得到一个错误

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

看起来Product的 variation 属性是一个包含变体id的List .

如何添加具有颜色和大小变化的新产品?

1 回答

  • 0

    创建产品 . 使用产品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);
    

相关问题