Developer forum

Forum » Development » Custom module - product import with variants

Custom module - product import with variants


Reply
 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


Replies

 
Reply
 Hello,

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
 
Reply
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



 
Reply
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.

//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
Reply
 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
 
Dmitry Nikolenko
Reply

 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
Reply
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! :-)
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Can you repost the code and rename the files as .txt? The webserver doesn't allow you to download .cs files...

Imar

 
Dmitry Nikolenko
Reply
Bo, could you to check your email, please. I have wrote you some questions.
 
Dmitry Nikolenko
Reply
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.
 
Vilius Janulis
Reply
Hi Dmitry,
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
Reply
 Hi Vilius,

You should to make variant groups with options in Management Center. I forgot to say about it. :)
100-200.png 170g.png A4.png d.png vg.png
 
Dmitry Nikolenko
Reply
result.
result.png
 
Vilius Janulis
Reply
Thank you Dmitry very very much, it acctually worked perfectly.

 

You must be logged in to post in the forum