How to add variants, variant combinations or/and variant attributes to the product, and what the difference between them ? (in code not in admin panel)
I need to create new variants, assign variant groups and add variant attributes to them. Is there is an example of something similar.
Thank you
Developer forum
E-mail notifications
Custom module - product import with variants
Posted on 02/05/2011 10:43:15
Replies
Posted on 04/05/2011 06:13:07
Hello,
I wrote a small example, I hope it will be useful to you.
//Dmitry
I wrote a small example, I hope it will be useful to you.
//default language
string langId = Dynamicweb.eCommerce.Common.Context.LanguageID;
//default shop
ShopCollection shops = Shop.getShops(true);
Shop defShop = shops.get_Item(0);
//create groupe
Group g = Group.Create("");
g.Name = "G1";
g.ShopID = defShop.ID;
g.LanguageID = langId;
g.Save("", langId);
//attach created group to the shop
GroupShopRelation gsr = new GroupShopRelation();
gsr.GroupID = g.ID;
gsr.ShopID = defShop.ID;
gsr.Save(g.ID, defShop.ID);
//create variant group
VariantGroup variantGrp = Dynamicweb.eCommerce.Variants.VariantGroup.Create("");
variantGrp.Name = "Variant Group 1";
variantGrp.LanguageID = langId;
variantGrp.Save();
//create first variant option (i.e. attribute)
VariantOption opt1 = VariantOption.Create("");
opt1.Name = "color";
opt1.GroupID = variantGrp.ID;
opt1.LanguageID = langId;
opt1.Save("");
//create second variant option (i.e. attribute)
VariantOption opt2 = VariantOption.Create("");
opt2.Name = "size";
opt2.GroupID = variantGrp.ID;
opt2.LanguageID = langId;
opt2.Save("");
//create product
Product p = Product.Create("");
p.Name = "Test product";
p.Number = "NUM1";
p.DefaultShopID = defShop.ID;
p.LanguageID = langId;
p.Save();
//attach product to the group
ProductGroupRelation pgr = new ProductGroupRelation();
pgr.GroupID = g.ID;
pgr.ProductID = p.ID;
pgr.Save(p.ID, g.ID);
//attach variant group to the product
VariantGroupProductRelation vgpr = new VariantGroupProductRelation(p.ID, variantGrp.ID);
vgpr.ProductID = p.ID;
vgpr.VariantGroupID = variantGrp.ID;
vgpr.LanguageID = langId;
vgpr.Save(p.ID, variantGrp.ID);
//create variant combination
string combo = VariantNumber.NumberParser(opt2.ID, p);
VariantCombination var = new VariantCombination();
var.ProductID = p.ID;
var.VariantID = combo;
var.Save(p.ID, combo);
//Dmitry
Posted on 09/05/2011 10:47:22
Thank you for an example. It helped a little.
This example shows how to create everything.
The problem now is how to get info that is already created assign it and change it.
I mean, there is variant groups, variants, and their attributes (options) already created.
What we need to do is to take product that is already created, assign already created variant groups, assign some options (that is also created in some ways, if they are not we create them (already know how, from the example of Yours)) and assign values to them, make this new assign variant combination accessible and save product with all these modifications.
Maybe You could give small example of such modifications.
Thank you
This example shows how to create everything.
The problem now is how to get info that is already created assign it and change it.
I mean, there is variant groups, variants, and their attributes (options) already created.
What we need to do is to take product that is already created, assign already created variant groups, assign some options (that is also created in some ways, if they are not we create them (already know how, from the example of Yours)) and assign values to them, make this new assign variant combination accessible and save product with all these modifications.
Maybe You could give small example of such modifications.
Thank you
Posted on 10/05/2011 03:40:18
Hello.
I made a complete example for a common vision, but if you need only variants, you could to take last blocks of the code.
I made a complete example for a common vision, but if you need only variants, you could to take last blocks of the code.
//attach variant group to the product
VariantGroupProductRelation vgpr = new VariantGroupProductRelation(p.ID, variantGrp.ID);
vgpr.ProductID = p.ID;
vgpr.VariantGroupID = variantGrp.ID;
vgpr.LanguageID = langId;
vgpr.Save(p.ID, variantGrp.ID);
//create variant combination
string combo = VariantNumber.NumberParser(opt2.ID, p);
VariantCombination var = new VariantCombination();
var.ProductID = p.ID;
var.VariantID = combo;
var.Save(p.ID, combo);
Vilius Janulis
Posted on 17/05/2011 13:33:40
Thanks it helped a lot.
But i still have some small questions:
How to make variant combination active (selected) ?
Now it imports products, adds variant groups, but they by default all are deselected (not active), how to make them active (in code of course, not in back-end).
How assign prices to different variant combinations ?
When variant combinations are active, it is possible to set a different price for a different combination, how to do that in code ? Also I know first of all we need to assign "Variants" to selected Differentiation in the prices (in English back-end it is called like that), how to do so ?
Thank you
But i still have some small questions:
How to make variant combination active (selected) ?
Now it imports products, adds variant groups, but they by default all are deselected (not active), how to make them active (in code of course, not in back-end).
How assign prices to different variant combinations ?
When variant combinations are active, it is possible to set a different price for a different combination, how to do that in code ? Also I know first of all we need to assign "Variants" to selected Differentiation in the prices (in English back-end it is called like that), how to do so ?
Thank you
Dmitry Nikolenko
Posted on 19/05/2011 05:25:19
Hi,
If you need selected variant combinations you should to use VariantCombination class. Below example, how to create active variant combination and create product variant with own name and price.
string combo = VariantNumber.NumberParser(opt2.ID, p); VariantCombination var = new VariantCombination(); var.ProductID = p.ID; var.VariantID = combo; var.Save(p.ID, combo); Product variant = Product.Create(p.ID, var.VariantID); variant.Name = variant.Name + " (variant)"; variant.DefaultPrice = 999; variant.Save(variant.ID, var.VariantID);
Bo Møller
Posted on 19/05/2011 14:46:59
Hi Dmitry and others who are interested.
Me and Vilius - who are working on this project - are at the following point now.
We have tried two approaches. None of them work, but they give different problems.
Approach 1)
We can import products and variantcombinations.
We can't activate the variantcombinations.
We can't add prices to the variant combinations.
Approach 2)
This is the approach you have suggested.
We can't create combinations.
I've attached the code we are running now - which is approach 1.
Please help guys! :-)
Me and Vilius - who are working on this project - are at the following point now.
We have tried two approaches. None of them work, but they give different problems.
Approach 1)
We can import products and variantcombinations.
We can't activate the variantcombinations.
We can't add prices to the variant combinations.
Approach 2)
This is the approach you have suggested.
We can't create combinations.
I've attached the code we are running now - which is approach 1.
Please help guys! :-)
Imar Spaanjaars
Posted on 19/05/2011 20:48:15
Can you repost the code and rename the files as .txt? The webserver doesn't allow you to download .cs files...
Imar
Dmitry Nikolenko
Posted on 20/05/2011 04:56:59
Bo, could you to check your email, please. I have wrote you some questions.
Dmitry Nikolenko
Posted on 25/05/2011 05:44:48
Hi Vilius,
I have made a module which imports products with variants from csv-file. This is example is not absolutely completed, but it gives common vision how variants work.
I have made a module which imports products with variants from csv-file. This is example is not absolutely completed, but it gives common vision how variants work.
Vilius Janulis
Posted on 25/05/2011 11:38:37
Hi Dmitry,
Imar uploaded it, but there are many many commented places, these all are other ways of trying to accomplish this task. And one option of course is in Dmitry downloads.
Acctualy did not had posability to test you code, i am getting some errors...
"System.NullReferenceException: Object reference not set to an instance of an object.
at CustomModules.CustomModules.ImportCSV.ImportCSV.AttachVariantGroups(Product p, VariantGroupCollection coll, String variantGroupName) "
Tryed to instanceate and test everything before using that i could think of still the same, so i am thinking that should be something with Dynamicweb that is not instanceated, tryed to do that too, with no success ..
Maybe you know whats the problem is ?
Imar uploaded it, but there are many many commented places, these all are other ways of trying to accomplish this task. And one option of course is in Dmitry downloads.
Dmitry Nikolenko
Posted on 26/05/2011 06:02:34
Hi Vilius,
You should to make variant groups with options in Management Center. I forgot to say about it. :)
You should to make variant groups with options in Management Center. I forgot to say about it. :)
Dmitry Nikolenko
Vilius Janulis
Posted on 26/05/2011 14:06:33
Thank you Dmitry very very much, it acctually worked perfectly.
You must be logged in to post in the forum