Developer forum

Forum » Development » Product import

Product import


Reply
 Hi everyone,

I'm writing a product import for eCommerce, and I'm having problems figuring out exactly how to create the products.

I would like to do something like this:

Dynamicweb.eCommerce.Products.Product product = new Product();
product.ID = "123";
product.Name = "Produktnavn";
product.Save();

What else is needed to make this work? And what is the recommended way of adding a product to a group?

Replies

 
Nicolai Høeg Pedersen
Reply
Yes - that should do it...
http://engage.dynamicweb-cms.com/api/ecommerce/?Dynamicweb~Dynamicweb.eCommerce.Products.Product~Save.html

But that only adds a product to the database. You also need to add it to a group before it shows up.

http://engage.dynamicweb-cms.com/default.aspx?ID=5&action=ShowThread&ThreadID=2317


 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Let me just add that as of DW 7.2 it's no longer necessary to create ProductGroupRelations manually.
Simply do like this, assuming group1 and group2 are existing Group objects:

product.Groups.Add(group1);
product.Groups.Add(group2);
product.Save();

The Save method will automatically create/update relations.

The same goes for GroupShopRelations, assume "SHOP1" is the ID of an existing Shop:

var group = new Group();
group.ID = "GROUP1";
group.ShopID = "SHOP1";
group.Save("GROUP1");

The Save method will again create/update the GroupShopRelation. However there was a bug in the way this was done, which has been fixed in 19.2.2.1.

- Jeppe

 
Reply
 Hi again.

Thanks for the input.
When importing/creating products I would like to add custom-fields to the product as well as the normal fields.

I need to do something like this:

p.Name = "name";
p.ID = "123";
p.CustomField1 = "blabla";
p.CustomField2 = "blabla2";

or perhaps just:

p.AddCustomFieldValue("NameOfCustomField","ValueOfCostumField");

I know that this is not the way to do it (But it woud be really nice with something like this though).

I've tried using the p.ProductFieldValues, but I can't really get it to work. Have you got a simple way to add custom fields on products?

Also - if the product allready exists, I just want to update the existing custom fields.

Thanks!
 
Reply
Hi there,

You can add the following extension method to your project:

public static class ProductHelpers
{
  public void SetCustomField(this Product product, string customFieldName, object customFieldValue)
  {
    if (product.ProductFieldValues == null)
    {
      product.ProductFieldValues = new ProductFieldValueCollection();
    }
    ProductFieldValue myFieldValue = product.ProductFieldValues.GetProductFieldValue(customFieldName);
    if (myFieldValue == null)
    {
      myFieldValue = new ProductFieldValue();
      ProductField myField = null;
      foreach (ProductField field in ProductField.FindProductFieldsBySystemName(customFieldName))
      {
      myField = field;
      break;
      }
      myFieldValue.ProductField = myField;
      product.ProductFieldValues.Add(myFieldValue);
    }
    myFieldValue.Value = customFieldValue;
    }
  }
}

You can set a value like this:

myProduct.SetCustomField("FieldName", someValue);

There might be more efficient ways to do this, but at least it works... ;-)

Cheers,

Imar
 
Reply
 Hey Imar!

Thanks - it works :-)

 

You must be logged in to post in the forum