private bool AddFreeItemToCart(PromotionContext context, LineItem listItem, Product prod, List <LineItem> outProducts, bool FreeQuantity) { var wasAdded = false; var productInCartList = context.Order.Items.Where(i => i.ProductId.ToUpperInvariant() == listItem.ProductId.ToUpperInvariant()) .ToList(); var addProd = false; if (productInCartList != null && productInCartList.Any()) { foreach (var productInCart in productInCartList) { var areEqual = listItem.SelectionData.Equals(productInCart.SelectionData); if (areEqual && productInCart.GetFreeCountByPromotionId(context.PromotionId) == listItem.Quantity) { if ( !productInCart.DiscountDetails.Any(s => s.DiscountType == PromotionType.OfferForFreeItems && s.PromotionId == context.PromotionId)) { productInCart.DiscountDetails.Add(new DiscountDetail { Amount = -listItem.BasePricePerItem * listItem.Quantity, DiscountType = PromotionType.OfferForFreeItems, Description = context.CustomerDescription, PromotionId = context.PromotionId, ActionId = Id }); if (FreeQuantity) { productInCart.CustomProperties.SetProperty(HCC_KEY, "freeQuantity", "true"); } } if ( !CheckProduct(context, prod, productInCart, Math.Max(productInCart.Quantity, listItem.Quantity))) { outProducts.Add(listItem); return(false); } wasAdded = true; break; } if (areEqual) { if (listItem.IsUserSuppliedPrice) { if (productInCart.BasePricePerItem == listItem.BasePricePerItem) { addProd = true; } } else { addProd = true; } if (addProd) { productInCart.DiscountDetails.Add(new DiscountDetail { Amount = -listItem.BasePricePerItem * listItem.Quantity, DiscountType = PromotionType.OfferForFreeItems, Description = context.CustomerDescription, PromotionId = context.PromotionId, ActionId = Id }); productInCart.AddPromotionId(context.PromotionId, listItem.Quantity); var IsQuantity = true; if (FreeQuantity) { productInCart.PromotionIds = ""; var noOfFreeItem = productInCart.CustomProperties.GetProperty(HCC_KEY, "freeNoOfQuantity"); var noOfItem = productInCart.CustomProperties.GetProperty(HCC_KEY, "NoOfQuantity"); var isFreeQty = productInCart.CustomProperties.GetProperty(HCC_KEY, "freeQuantity"); if (productInCart.Quantity <= productInCart.FreeQuantity || (productInCart.Quantity.ToString() == noOfItem && productInCart.FreeQuantity.ToString() == noOfFreeItem) || string.IsNullOrEmpty(isFreeQty)) { productInCart.CustomProperties.SetProperty(HCC_KEY, "freeNoOfQuantity", productInCart.FreeQuantity); productInCart.CustomProperties.SetProperty(HCC_KEY, "NoOfQuantity", productInCart.Quantity); IsQuantity = false; productInCart.Quantity += productInCart.FreeQuantity; } productInCart.CustomProperties.SetProperty(HCC_KEY, "freeQuantity", "true"); } if (IsQuantity) { productInCart.Quantity = Math.Max(productInCart.Quantity, productInCart.FreeQuantity); } wasAdded = true; if ( !CheckProduct(context, prod, productInCart, Math.Max(productInCart.Quantity, listItem.Quantity))) { outProducts.Add(listItem); return(false); } } } } } if (!wasAdded) { listItem.DiscountDetails.Add(new DiscountDetail { Amount = -listItem.BasePricePerItem * listItem.Quantity, DiscountType = PromotionType.OfferForFreeItems, Description = context.CustomerDescription, PromotionId = context.PromotionId, ActionId = Id }); listItem.AddPromotionId(context.PromotionId, listItem.Quantity); context.Order.IsAbandonedEmailSent = false; context.Order.TimeOfOrderUtc = DateTime.UtcNow; context.HccApp.OrderServices.AddItemToOrder(context.Order, listItem); context.HccApp.OrderServices.Orders.Upsert(context.Order); wasAdded = true; if (!CheckProduct(context, prod, listItem, listItem.Quantity)) { outProducts.Add(listItem); return(false); } } return(wasAdded); }