public AddToCartResult AddToCart(ICart cart, EntryContentBase entryContent, decimal quantity, string deliveryMethod, string warehouseCode, List <string> dynamicVariantOptionCodes) { var result = new AddToCartResult(); var contact = PrincipalInfo.CurrentPrincipal.GetCustomerContact(); if (contact?.OwnerId != null) { var org = cart.GetString("OwnerOrg"); if (string.IsNullOrEmpty(org)) { cart.Properties["OwnerOrg"] = contact.OwnerId.Value.ToString().ToLower(); } } IWarehouse warehouse = null; if (deliveryMethod.Equals("instore") && !string.IsNullOrEmpty(warehouseCode)) { warehouse = _warehouseRepository.Get(warehouseCode); } if (entryContent is BundleContent) { foreach (var relation in _relationRepository.GetChildren <BundleEntry>(entryContent.ContentLink)) { var entry = _contentLoader.Get <EntryContentBase>(relation.Child); var recursiveResult = AddToCart(cart, entry, (relation.Quantity ?? 1) * quantity, deliveryMethod, warehouseCode, dynamicVariantOptionCodes); if (recursiveResult.EntriesAddedToCart) { result.EntriesAddedToCart = true; } foreach (var message in recursiveResult.ValidationMessages) { result.ValidationMessages.Add(message); } } return(result); } var form = cart.GetFirstForm(); if (form == null) { form = _orderGroupFactory.CreateOrderForm(cart); form.Name = cart.Name; cart.Forms.Add(form); } var shipment = cart.GetFirstForm().Shipments.FirstOrDefault(x => string.IsNullOrEmpty(warehouseCode) || (x.WarehouseCode == warehouseCode && x.ShippingMethodId == InStorePickupInfoModel.MethodId)); if (warehouse != null) { if (shipment != null && !shipment.LineItems.Any()) { shipment.WarehouseCode = warehouseCode; shipment.ShippingMethodId = InStorePickupInfoModel.MethodId; shipment.ShippingAddress = GetOrderAddressFromWarehosue(cart, warehouse); } else { shipment = form.Shipments.FirstOrDefault(x => !string.IsNullOrEmpty(x.WarehouseCode) && x.WarehouseCode.Equals(warehouse.Code)); if (shipment == null) { if (cart.GetFirstShipment().LineItems.Count > 0) { shipment = _orderGroupFactory.CreateShipment(cart); } else { shipment = cart.GetFirstShipment(); } shipment.WarehouseCode = warehouseCode; shipment.ShippingMethodId = InStorePickupInfoModel.MethodId; shipment.ShippingAddress = GetOrderAddressFromWarehosue(cart, warehouse); if (cart.GetFirstShipment().LineItems.Count > 0) { cart.GetFirstForm().Shipments.Add(shipment); } } } } if (shipment == null) { var cartFirstShipment = cart.GetFirstShipment(); if (cartFirstShipment == null) { shipment = _orderGroupFactory.CreateShipment(cart); cart.GetFirstForm().Shipments.Add(shipment); } else { if (cartFirstShipment.LineItems.Count > 0) { shipment = _orderGroupFactory.CreateShipment(cart); cart.GetFirstForm().Shipments.Add(shipment); } else { shipment = cartFirstShipment; } } } var lineItem = shipment.LineItems.FirstOrDefault(x => x.Code == entryContent.Code); decimal originalLineItemQuantity = 0; if (lineItem == null) { lineItem = cart.CreateLineItem(entryContent.Code, _orderGroupFactory); var lineDisplayName = entryContent.DisplayName; if (dynamicVariantOptionCodes?.Count > 0) { lineItem.Properties[VariantOptionCodesProperty] = string.Join(",", dynamicVariantOptionCodes.OrderBy(x => x)); lineDisplayName += " - " + lineItem.Properties[VariantOptionCodesProperty]; } lineItem.DisplayName = lineDisplayName; lineItem.Quantity = quantity; cart.AddLineItem(shipment, lineItem); } else { if (lineItem.Properties[VariantOptionCodesProperty] != null) { var variantOptionCodesLineItem = lineItem.Properties[VariantOptionCodesProperty].ToString().Split(','); var intersectCodes = variantOptionCodesLineItem.Intersect(dynamicVariantOptionCodes); if (intersectCodes != null && intersectCodes.Any() && intersectCodes.Count() == variantOptionCodesLineItem.Length && intersectCodes.Count() == dynamicVariantOptionCodes.Count) { originalLineItemQuantity = lineItem.Quantity; cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity); } else { lineItem = cart.CreateLineItem(entryContent.Code, _orderGroupFactory); lineItem.Properties[VariantOptionCodesProperty] = string.Join(",", dynamicVariantOptionCodes.OrderBy(x => x)); lineItem.DisplayName = entryContent.DisplayName + " - " + lineItem.Properties[VariantOptionCodesProperty]; lineItem.Quantity = quantity; cart.AddLineItem(shipment, lineItem); } } else { originalLineItemQuantity = lineItem.Quantity; cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity); } } var validationIssues = ValidateCart(cart); var newLineItem = shipment.LineItems.FirstOrDefault(x => x.Code == entryContent.Code); var isAdded = (newLineItem != null ? newLineItem.Quantity : 0) - originalLineItemQuantity > 0; AddValidationMessagesToResult(result, lineItem, validationIssues, isAdded); return(result); }
public AddToCartResult AddToCart(ICart cart, EntryContentBase entryContent, decimal quantity, string deliveryMethod, string warehouseCode) { var result = new AddToCartResult(); var contact = PrincipalInfo.CurrentPrincipal.GetCustomerContact(); if (contact?.OwnerId != null) { var org = cart.GetString("OwnerOrg"); if (string.IsNullOrEmpty(org)) { cart.Properties["OwnerOrg"] = contact.OwnerId.Value.ToString().ToLower(); } } IWarehouse warehouse = null; if (deliveryMethod.Equals("instore") && !string.IsNullOrEmpty(warehouseCode)) { warehouse = _warehouseRepository.Get(warehouseCode); } if (entryContent is BundleContent) { foreach (var relation in _relationRepository.GetChildren <BundleEntry>(entryContent.ContentLink)) { var entry = _contentLoader.Get <EntryContentBase>(relation.Child); var recursiveResult = AddToCart(cart, entry, relation.Quantity ?? 1, deliveryMethod, warehouseCode); if (recursiveResult.EntriesAddedToCart) { result.EntriesAddedToCart = true; } foreach (var message in recursiveResult.ValidationMessages) { result.ValidationMessages.Add(message); } } return(result); } var form = cart.GetFirstForm(); if (form == null) { form = _orderGroupFactory.CreateOrderForm(cart); form.Name = cart.Name; cart.Forms.Add(form); } var shipment = cart.GetFirstShipment(); if (warehouse != null) { if (shipment != null && !shipment.LineItems.Any()) { shipment.WarehouseCode = warehouseCode; shipment.ShippingMethodId = InStorePickupInfoModel.MethodId; shipment.ShippingAddress = GetOrderAddressFromWarehosue(cart, warehouse); } else { shipment = form.Shipments.FirstOrDefault(x => !string.IsNullOrEmpty(x.WarehouseCode) && x.WarehouseCode.Equals(warehouse.Code)); if (shipment == null) { shipment = _orderGroupFactory.CreateShipment(cart); shipment.WarehouseCode = warehouseCode; shipment.ShippingMethodId = InStorePickupInfoModel.MethodId; cart.GetFirstForm().Shipments.Add(shipment); shipment.ShippingAddress = GetOrderAddressFromWarehosue(cart, warehouse); } } } if (shipment == null) { shipment = _orderGroupFactory.CreateShipment(cart); cart.GetFirstForm().Shipments.Add(shipment); } var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == entryContent.Code); if (lineItem == null) { lineItem = cart.CreateLineItem(entryContent.Code, _orderGroupFactory); lineItem.DisplayName = entryContent.DisplayName; lineItem.Quantity = quantity; cart.AddLineItem(shipment, lineItem); } else { cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity); } var validationIssues = ValidateCart(cart); AddValidationMessagesToResult(result, lineItem, validationIssues); return(result); }