微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用多个属性添加产品变体

我正在使用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);

但我得到一个错误

Cannot implicitly convert type
‘System.Collections.Generic.List < WooCommerceNET.WooCommerce.v2.Variation >’
to ‘System.Collections.Generic.List < int >’

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

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

解决方法

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

Product p = new Product()
{
    //options ....
    type = "variable",//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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐