private void UpdateProductAttributeCombinations(Product product, List <ProductAttributeCombinationDto> productAttributeCombinations) { if (productAttributeCombinations == null) { return; } // REMOVE OLD ProductAttributeCombinations! // IF ID IS NOT IN LIST THEN REMOVE! if (product.ProductAttributeCombinations.Count > 0) { var ids = productAttributeCombinations.Select(c => c.Id).ToList(); var dataCombinations = new ProductAttributeCombination[product.ProductAttributeCombinations.Count]; product.ProductAttributeCombinations.CopyTo(dataCombinations, 0); foreach (var combi in dataCombinations.Where(t => !ids.Contains(t.Id))) { _productAttributeService.DeleteProductAttributeCombination(combi); } } var attributesXml = ""; foreach (var combi in productAttributeCombinations) { attributesXml = string.Empty; if (combi.Id == 0) { for (int i = 0; i < combi.Records.Count; i++) { foreach (var mapping in product.ProductAttributeMappings) { if (mapping.ProductAttributeValues.Count == 0) { var productAttributeValues = _productAttributeService.GetProductAttributeValues(mapping.Id); } var mappingValue = mapping.ProductAttributeValues.Where(v => _genericAttributeService.GetAttribute <int>(v, "nop.product.attributevalue.recordid") == combi.Records[i]).FirstOrDefault(); if (mappingValue != null) { attributesXml = _productAttributeParser.AddProductAttribute(attributesXml, mapping, mappingValue.Id.ToString()); } } } // OPRET NY COMBINATION var attributeCombi = new ProductAttributeCombination() { ProductId = product.Id, AttributesXml = attributesXml, Sku = combi.Sku, Gtin = combi.Gtin, StockQuantity = combi.StockQuantity, AllowOutOfStockOrders = combi.AllowOutOfStockOrders, ManufacturerPartNumber = combi.ManufacturerPartNumber, NotifyAdminForQuantityBelow = combi.NotifyAdminForQuantityBelow, OverriddenPrice = combi.OverriddenPrice }; _productAttributeService.InsertProductAttributeCombination(attributeCombi); _genericAttributeService.SaveAttribute(attributeCombi, "nop.product.attribute.combination.records", combi.Records); _genericAttributeService.SaveAttribute(attributeCombi, "nop.product.attribute.combination.admind_id", combi.AdmindCombinationId); } else { // OPDATERE COMBINATION var currentCombi = _productAttributeService.GetProductAttributeCombinationById(combi.Id); currentCombi.Gtin = combi.Gtin != currentCombi.Gtin ? combi.Gtin : currentCombi.Gtin; currentCombi.Sku = combi.Sku != currentCombi.Sku ? combi.Sku : currentCombi.Sku; currentCombi.StockQuantity = combi.StockQuantity != currentCombi.StockQuantity ? combi.StockQuantity : currentCombi.StockQuantity; currentCombi.AllowOutOfStockOrders = combi.AllowOutOfStockOrders != currentCombi.AllowOutOfStockOrders ? combi.AllowOutOfStockOrders : currentCombi.AllowOutOfStockOrders; currentCombi.ManufacturerPartNumber = combi.ManufacturerPartNumber != currentCombi.ManufacturerPartNumber ? combi.ManufacturerPartNumber : currentCombi.ManufacturerPartNumber; currentCombi.NotifyAdminForQuantityBelow = combi.NotifyAdminForQuantityBelow != currentCombi.NotifyAdminForQuantityBelow ? combi.NotifyAdminForQuantityBelow : currentCombi.NotifyAdminForQuantityBelow; currentCombi.OverriddenPrice = combi.OverriddenPrice != currentCombi.OverriddenPrice ? combi.OverriddenPrice : currentCombi.OverriddenPrice; _productAttributeService.UpdateProductAttributeCombination(currentCombi); } } }