Developer forum

Forum » Development » Variantgroup not being added to product

Variantgroup not being added to product

Jonas Mersholm
Reply

I have a product

Dynamicweb.eCommerce.Products.Product product = new Dynamicweb.eCommerce.Products.Product();
product.Name = "Test product";
product.Save();

And a variantgroup

Dynamicweb.eCommerce.Variants.VariantGroup newGroup =  Dynamicweb.eCommerce.Variants.VariantGroup.Create("variantGroupFor_" + product.ID);
newGroup.LanguageID = "LANG1";
newGroup.Name = product.ID;
newGroup.Label = product.ID;
newGroup.Save();

I've added a variant to the group

Dynamicweb.eCommerce.Variants.VariantOption Opt = new Dynamicweb.eCommerce.Variants.VariantOption();
Opt.Name = "Variant option";
Opt.GroupID = newGroup.ID;
Opt.LanguageID = "LANG1";
Opt.Save();

newGroup.VariantOptions.Add(Opt);

newGroup.Save();

 

Now, all this works fine. The variantgroup is created in the Management Center, and the option is created in the variantgroup. 

The problem is, when i try to add the variantgroup to the product

product.AddVariantGroup(newGroup);
product.Save();

 

The group is not added, and therefor not visible in the "Variants" part of the product in the eCommerce backpanel?

Any idea, whats up?

 

Best regards

Jonas


Replies

 
Jonas Mersholm
Reply

bump

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Jonas,

It's most likely because a VariantGroupProductRelation has not been created.

Try this instead of the last add and save:

var relation = new VariantGroupProductRelation();
relation.ProductID = product.ID;
relation.VariantGroupID = newGroup.ID;
relation.Save(product.ID, newGroup.ID);
product.AddVariantGroup(newGroup);

This will create a relation between the variant group and the product. You should now be able to add variant combinations from the "Edit product -> Variants" screen.

You can also code the variant combinations manually if you prefer using the VariantCombination class.

- Jeppe

 

You must be logged in to post in the forum