Hi,
I have an issue when saving the product. Instead of updating it is trying to insert the new one. This is the example code.
var dw = Product.GetProductByNumber(bp.sku, "LANG1"); if (dw == null) return; dw.Save();
When dw is null and I call Save it inserts the product just fine but when the product is already in the database it does not update but throws the exception:
System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'DW_PK_EcomProducts'. Cannot insert duplicate key in object 'dbo.EcomProducts'. The duplicate key value is (PROD306, LANG1, ).
The version I am using is 8.8.1.6
Also there is an issue when inserting 2 or more variants for a product when there are no VariantOptions defined in the database.
VariantOption vo = VariantOption.GetAllVariantOptions().FirstOrDefault(c => c.Name == optionName); if (vo != null) return vo; //create new variant option var newOption = new VariantOption(); newOption.GroupID = vg.ID; newOption.LanguageID = "LANG1"; newOption.Name = optionName; newOption.Save();
The workarround I made was, that before adding a variant I try to "clear the cache" for product. The issue is that without clearing the cache VariantNumber.NumberParser(num, vo.ID) returns the empty string.
var dwProduct = GetProductNoCache(productId);
var num = string.Format("{0}", vo.ID); var combo = VariantNumber.NumberParser(num, dwProduct); var var = new VariantCombination(); var.ProductID = dwProduct.ID; var.VariantID = combo; var.Save(dwProduct.ID, combo);
private Product GetProductNoCache(string productId) { if (HttpContext.Current != null) HttpContext.Current.Items.Clear(); return Product.GetProductByID(productId); }
Of course this is not a solution, there may be other bugs lurking arround, can you figure out what's wrong? I can provide the full solution if you want.