Developer forum

Forum » Development » How to create a related product during import of products

How to create a related product during import of products

Lanzo van Slooten
Reply

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

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
Reply

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

 


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
Reply
 
I have changed the code that first all the procucts are imported. Then I add the related products. The code is running fine but no results.
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