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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With