Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding product variations with multiple attributes

I am using the WoocommerceNET Library (Nuget Link) to develop a desktop app that will sync the products from the ERP database to the Woocommerce eshop database.

I added attributes size and color with values eg red,green,blue and s,m,l,xl. Now I need to create the variations.

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

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

It looks like the variation attribute of Product is a List that contain the variations ids.

How can I add a new product with variations for color and size?

like image 407
athskar Avatar asked Nov 08 '22 21:11

athskar


1 Answers

Create the product. Create the variation with the product 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);
like image 53
Wd Valena Avatar answered Nov 14 '22 22:11

Wd Valena