Developer forum

Forum » Development » Product.Clone() suddenly returning error

Product.Clone() suddenly returning error

Jonas Mersholm
Reply

Hi. We have a system where we take products from the main-cart, and clone them to dealer groups. This has worked fine so far, but until after an update to 8.6, we are now receiving the following error:

Type 'Dynamicweb.eCommerce.Products.ProductVatGroupCollection' in Assembly 'Dynamicweb, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

 

This is the code:

            try
            {
                Dynamicweb.eCommerce.Products.Product NP = E.Clone();
            } catch(Exception realExc2)
            {
                HttpContext.Current.Response.Write(realExc2.Message);
            }

 

As said, this has all worked fine before the upgrade to 8.6.

Any ideas?

 

Best regards.

Jonas


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

This is due to TFS#14588, set a vat group for each country on the products. That have added the ProductVatGroupCollection property on the product and that is not serializable.

Instead of doing a clone (not recommended by the way), you could do something like this:

Dynamicweb.eCommerce.Products.Product NP = Product.GetProductByID(e.ID, e.VariantID, e.LanguageID);

 
Jonas Mersholm
Reply

Hi Nicolai.

Will the GetProductById return a new instance of the product, which i can save seperately from the old one? I was recommended to use the clone method half a year ago, because the "Copy" method dident work as expected ( All the product group relations were duplicated etc. etc )

Thanks in advance.

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

Copy gives you the same object instance but with a new record id (autoid), using GetProductById will give you a new instance of the same product.

BR Nicolai

 
Jonas Mersholm
Reply

Aight. I should be using copy then. I need to save the copied product with a new ID :)

 

Best.

Jonas

 
Nicolai Høeg Pedersen
Reply

Hi Jonas.

Yes, but when using copy the way it is implemented, your original and your copy will have the same object reference... So to avoid issues, do something like this:

copyProduct = e.copy();

Dynamicweb.eCommerce.Products.Product NP = Product.GetProductByID(copyProduct.ID, copyProduct.VariantID, copyProduct.LanguageID);

 

You must be logged in to post in the forum