public virtual async Task <CartAggregate> AddItemAsync(NewCartItem newCartItem)
        {
            EnsureCartExists();

            if (newCartItem == null)
            {
                throw new ArgumentNullException(nameof(newCartItem));
            }

            var validationResult = await new NewCartItemValidator().ValidateAsync(newCartItem, ruleSet: ValidationRuleSet);

            if (!validationResult.IsValid)
            {
                ValidationErrors.AddRange(validationResult.Errors);
            }
            else if (newCartItem.CartProduct != null)
            {
                var lineItem = _mapper.Map <LineItem>(newCartItem.CartProduct);
                lineItem.Quantity = newCartItem.Quantity;

                if (newCartItem.Price != null)
                {
                    lineItem.ListPrice = newCartItem.Price.Value;
                    lineItem.SalePrice = newCartItem.Price.Value;
                }

                if (!string.IsNullOrEmpty(newCartItem.Comment))
                {
                    lineItem.Note = newCartItem.Comment;
                }

                if (!newCartItem.DynamicProperties.IsNullOrEmpty())
                {
                    lineItem.DynamicProperties = newCartItem.DynamicProperties.Select(x => new DynamicObjectProperty
                    {
                        Name   = x.Key,
                        Values = new[] { new DynamicPropertyObjectValue {
                                             Value = x.Value
                                         } }
                    }).ToList();
                }

                CartProducts[newCartItem.CartProduct.Id] = newCartItem.CartProduct;
                await InnerAddLineItemAsync(lineItem, newCartItem.CartProduct);
            }

            return(this);
        }
Example #2
0
        public virtual async Task <CartAggregate> AddItemAsync(NewCartItem newCartItem)
        {
            EnsureCartExists();

            if (newCartItem == null)
            {
                throw new ArgumentNullException(nameof(newCartItem));
            }

            var validationResult = await AbstractTypeFactory <NewCartItemValidator> .TryCreateInstance().ValidateAsync(newCartItem, options => options.IncludeRuleSets(ValidationRuleSet));

            if (!validationResult.IsValid)
            {
                ValidationErrors.AddRange(validationResult.Errors);
            }
            else if (newCartItem.CartProduct != null)
            {
                var lineItem = _mapper.Map <LineItem>(newCartItem.CartProduct);
                lineItem.Quantity = newCartItem.Quantity;

                if (newCartItem.Price != null)
                {
                    lineItem.ListPrice = newCartItem.Price.Value;
                    lineItem.SalePrice = newCartItem.Price.Value;
                }
                else
                {
                    SetLineItemTierPrice(newCartItem.CartProduct.Price, newCartItem.Quantity, lineItem);
                }

                if (!string.IsNullOrEmpty(newCartItem.Comment))
                {
                    lineItem.Note = newCartItem.Comment;
                }

                CartProducts[newCartItem.CartProduct.Id] = newCartItem.CartProduct;
                await SetItemFulfillmentCenterAsync(lineItem, newCartItem.CartProduct);
                await InnerAddLineItemAsync(lineItem, newCartItem.CartProduct, newCartItem.DynamicProperties);
            }

            return(this);
        }