Developer forum

Forum » Integration » Import related products

Import related products

Martin Christensen
Reply

Hi,

I'm looking for a way to import references to related products. We already have a data integration job, doing batch integration at a scheduled interval. My main issue is that products in the client's ERP are referenced by internal product numbers.

In EcomProducts, the column RelatedProducts holds a list of ProductIDs, which are different from the product numbers.

1) How can I import a list of related products that is based on their product number?

2) Do I need to update ProductRelatedCounter? If so, how would I do that?

Any help is appreciated

BR Martin


Replies

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Martin,

1)

you can use the TableScript with finding the existing product by number using the api: Dynamicweb.Ecommerce.Services.Products.GetProductByNumber() and once found take its Id
so the code can look like that:
using System;
using System.Linq;
using System.Collections.Generic;
using Dynamicweb.DataIntegration.Integration;

namespace CustomTableScripts
{
    public class EcomProductsRelatedSetProductIdByProductNumber : TableScript
    {
        private const string FilteredTableName = "EcomProductsRelated";        

        public override void ProcessInputRow(Mapping mapping, Dictionary<string, object> row)
        {
            if (mapping.DestinationTable == null || mapping.DestinationTable.Name != FilteredTableName)
            {
                return;
            }

            var productNumber = row[GetSourceColumnNameFromDestinationName(mapping, "ProductRelatedProductId")].ToString();

            //don't map if the value is empty
            if (string.IsNullOrEmpty(productNumber))
            {
                return;
            }            

            var product = new Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(productNumber)
            if (product == null)
            {
                //product wasn't found so leave the row as it is
                return;
            }

            row[GetSourceColumnNameFromDestinationName(mapping, "ProductRelatedProductId")] = product.Id; //this will overwrite the one in the mapping

            productNumber = row[GetSourceColumnNameFromDestinationName(mapping, "ProductRelatedProductRelId")].ToString();

            //don't map if the value is empty
            if (string.IsNullOrEmpty(productNumber))
            {
                return;
            }

            var product = new Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(productNumber)
            if (product == null)
            {
                //product wasn't found so leave the row as it is
                return;
            }

            row[GetSourceColumnNameFromDestinationName(mapping, "ProductRelatedProductRelId")] = product.Id;            
        }

        private string GetSourceColumnNameFromDestinationName(Mapping mapping, string destinationColumnName, bool destinationColumnExistenceIsOptional = false)
        {
            var activeColumn = mapping.GetColumnMappings().FirstOrDefault(c => destinationColumnName.Equals(c.DestinationColumn.Name, StringComparison.CurrentCultureIgnoreCase))?.SourceColumn?.Name;
            if (destinationColumnExistenceIsOptional)
            {
                return activeColumn ?? null;
            }
            else
            {
                return activeColumn ?? throw new InvalidOperationException($"Table Script: unexpected error has occurred since the destination table doesn't have a column called {destinationColumnName}");
            }

        }
    }
}

2)

no

BR, Dmitrij

 

You must be logged in to post in the forum