Adding product options with multiple attributes

I use the WoocommerceNET library ( Nuget Link ) to develop a desktop application that will synchronize products from the ERP database with the Woocommerce eshop database.

I added size and color attributes with values ​​like red, green, blue and s, m, l, xl. Now I need to create a variation.

Tried this:

  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);

But I get an error

It is not possible to implicitly convert the type 'System.Collections.Generic.List <WooCommerceNET.WooCommerce.v2.Variation>' to 'System.Collections.Generic.List <int>'

It looks like the variant attribute for the product is a list containing the variant identifiers.

?

+4
1

. .

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);
0

Source: https://habr.com/ru/post/1677467/


All Articles