Developer forum
E-mail notifications
How to create a related product during import of products
Lanzo van Slooten
Posted on 12/07/2011 11:16:52
I have created a custom module to import products from a xml file. The import is working fine, but I also want to add related products.
I execute the following code, where dwBaseProduct is the product that it's imported:
//RelatedProducts
foreach (var relatedproduct in product.RelatedProducts)
{
ProductRelated pr = new ProductRelated();
pr.ProdID = dwBaseProduct.ID;
pr.ProdRelID = relatedproduct.ProductNumber;
pr.RelGroupID = "RELGRP1";
dwBaseProduct.RelatedProducts.Add(pr);
}
Am I missing some code or do I have to do this another way?
Best regards,
Lanzo van Slooten
Replies
Imar Spaanjaars
Posted on 12/07/2011 21:03:12
Hi Lanzo,
Shouldn't you be using the related product's ID (which DW uses internally) and not the ProductNumber (which is "your" ID)?
Imar
Lanzo van Slooten
Posted on 13/07/2011 09:15:38
Imar
Hi Lanzo,
Shouldn't you be using the related product's ID (which DW uses internally) and not the ProductNumber (which is "your" ID)?
Imar
I think you are right. I had the same the problem with adding products to groups and I solved that with using the right ID.
thanks,
Lanzo
Lanzo van Slooten
Posted on 13/07/2011 11:17:44
Trying the same in DynamicWeb gives also no result, so I think the problem is located somewhere else.
string curLanguageId = _settings.DefaultLanguageId;
ProductRelatedGroupCollection myRelatedGroupCollection = ProductRelatedGroup.getRelatedGroupsByLanguage();
ProductRelatedGroup prGroup = myRelatedGroupCollection.get_Item(0);
foreach (var product in _source.Products)
{
Product dwBaseProduct = GetProductByNameAndLanguage(product.ProductNumber, curLanguageId);
foreach (var relatedproduct in product.RelatedProducts)
{
Product dwRelatedProduct = GetProductByNameAndLanguage(relatedproduct.ProductNumber, curLanguageId);
if (!dwBaseProduct.RelatedProducts.Contains(dwRelatedProduct))
{
ProductRelated pr = new ProductRelated();
pr.ProdID = dwBaseProduct.ID;
pr.ProdRelID = dwRelatedProduct.ID;
pr.RelGroupID = prGroup.GroupID;
dwBaseProduct.RelatedProducts.Add(pr);
}
}
dwBaseProduct.Save();
}
Who has any suggestions to solve the problem?
greetings,
Lanzo
You must be logged in to post in the forum