Example #1
0
        public override async Task <CartAggregate> Handle(ChangeCartItemQuantityCommand request, CancellationToken cancellationToken)
        {
            var cartAggregate = await GetOrCreateCartFromCommandAsync(request);

            if (request.Quantity == 0)
            {
                await cartAggregate.RemoveItemAsync(request.LineItemId);

                return(await SaveCartAsync(cartAggregate));
            }

            var         lineItem = cartAggregate.Cart.Items.FirstOrDefault(x => x.Id.Equals(request.LineItemId));
            CartProduct product  = null;

            if (lineItem != null)
            {
                product = (await _cartProductService.GetCartProductsByIdsAsync(cartAggregate, new[] { lineItem.ProductId })).FirstOrDefault();
            }

            await cartAggregate.ChangeItemQuantityAsync(new ItemQtyAdjustment
            {
                LineItem    = lineItem,
                LineItemId  = request.LineItemId,
                NewQuantity = request.Quantity,
                CartProduct = product
            });

            return(await SaveCartAsync(cartAggregate));
        }
Example #2
0
        public virtual async Task <CartAggregate> AddItemsAsync(ICollection <NewCartItem> newCartItems)
        {
            EnsureCartExists();

            var productIds = newCartItems.Select(x => x.ProductId).Distinct().ToArray();

            var productsByIds =
                (await _cartProductService.GetCartProductsByIdsAsync(this, productIds))
                .ToDictionary(x => x.Id);

            foreach (var item in newCartItems)
            {
                if (productsByIds.TryGetValue(item.ProductId, out var product))
                {
                    await AddItemAsync(new NewCartItem(item.ProductId, item.Quantity)
                    {
                        Comment           = item.Comment,
                        DynamicProperties = item.DynamicProperties,
                        Price             = item.Price,
                        CartProduct       = product
                    });
                }
            }

            return(this);
        }
Example #3
0
        public async Task <IEnumerable <GiftItem> > GetAvailableGiftsAsync(CartAggregate cartAggr)
        {
            var promotionEvalResult = await cartAggr.EvaluatePromotionsAsync();

            var giftRewards = promotionEvalResult.Rewards
                              .OfType <GiftReward>()
                              .Where(reward => reward.IsValid)
                              // .Distinct() is needed as multiplied gifts would be returned otherwise.
                              .Distinct()
                              .ToArray();

            var productIds = giftRewards.Select(x => x.ProductId).Distinct().Where(x => !x.IsNullOrEmpty()).ToArray();

            var productsByIds = (await _cartProductService.GetCartProductsByIdsAsync(cartAggr, productIds)).ToDictionary(x => x.Id);

            var availableProductsIds = productsByIds.Values
                                       .Where(x => (x.Product.IsActive ?? false) &&
                                              (x.Product.IsBuyable ?? false) &&
                                              x.Price != null &&
                                              (!(x.Product.TrackInventory ?? false) || x.AvailableQuantity >= giftRewards
                                               .FirstOrDefault(y => y.ProductId == x.Product.Id)?.Quantity))
                                       .Select(x => x.Product.Id)
                                       .ToHashSet();

            return(giftRewards
                   .Where(x => x.ProductId.IsNullOrEmpty() || availableProductsIds.Contains(x.ProductId))
                   .Select(reward =>
            {
                var result = _mapper.Map <GiftItem>(reward);

                // if reward has assigned product, add data from product
                if (!reward.ProductId.IsNullOrEmpty() && productsByIds.ContainsKey(reward.ProductId))
                {
                    var product = productsByIds[reward.ProductId];
                    result.CatalogId = product.Product.CatalogId;
                    result.CategoryId ??= product.Product.CategoryId;
                    result.ProductId = product.Product.Id;
                    result.Sku = product.Product.Code;
                    result.ImageUrl ??= product.Product.ImgSrc;
                    result.MeasureUnit ??= product.Product.MeasureUnit;
                    result.Name ??= product.Product.Name;
                }

                var giftInCart = cartAggr.GiftItems.FirstOrDefault(x => x.EqualsReward(result));
                // non-null LineItemId indicates that this GiftItem was added to the cart
                result.LineItemId = giftInCart?.Id;

                // CacheKey as Id
                result.Id = result.GetCacheKey();
                return result;
            }).ToList());
        }
Example #4
0
        protected virtual async Task <CartAggregate> InnerGetCartAggregateFromCartAsync(ShoppingCart cart, string language, CartAggregateResponseGroup responseGroup = CartAggregateResponseGroup.Full)
        {
            if (cart == null)
            {
                throw new ArgumentNullException(nameof(cart));
            }

            var storeLoadTask         = _storeService.GetByIdAsync(cart.StoreId);
            var allCurrenciesLoadTask = _currencyService.GetAllCurrenciesAsync();

            await Task.WhenAll(storeLoadTask, allCurrenciesLoadTask);

            var store         = storeLoadTask.Result;
            var allCurrencies = allCurrenciesLoadTask.Result;

            if (store == null)
            {
                throw new OperationCanceledException($"store with id {cart.StoreId} not found");
            }
            if (string.IsNullOrEmpty(cart.Currency))
            {
                cart.Currency = store.DefaultCurrency;
            }

            var currency = allCurrencies.GetCurrencyForLanguage(cart.Currency, language ?? store.DefaultLanguage);

            var member = await _memberResolver.ResolveMemberByIdAsync(cart.CustomerId);

            var aggregate = _cartAggregateFactory();

            aggregate.GrabCart(cart, store, member, currency);


            //Load cart products explicitly if no validation is requested
            var cartProducts = await _cartProductsService.GetCartProductsByIdsAsync(aggregate, aggregate.Cart.Items.Select(x => x.ProductId).ToArray());

            //Populate aggregate.CartProducts with the  products data for all cart  line items
            foreach (var cartProduct in cartProducts)
            {
                aggregate.CartProducts[cartProduct.Id] = cartProduct;
            }

            foreach (var lineItem in aggregate.LineItems)
            {
                var cartProduct = aggregate.CartProducts[lineItem.ProductId];
                await aggregate.SetItemFulfillmentCenterAsync(lineItem, cartProduct);
            }

            await aggregate.RecalculateAsync();

            return(aggregate);
        }
        public async Task <CartValidationContext> CreateValidationContextAsync(CartAggregate cartAggr)
        {
            var availPaymentsTask      = _availMethods.GetAvailablePaymentMethodsAsync(cartAggr);
            var availShippingRatesTask = _availMethods.GetAvailableShippingRatesAsync(cartAggr);
            var cartProductsTask       = _cartProducts.GetCartProductsByIdsAsync(cartAggr, cartAggr.Cart.Items.Select(x => x.ProductId).ToArray());
            await Task.WhenAll(availPaymentsTask, availShippingRatesTask, cartProductsTask);

            return(new CartValidationContext
            {
                AllCartProducts = cartProductsTask.Result,
                AvailPaymentMethods = availPaymentsTask.Result,
                AvailShippingRates = availShippingRatesTask.Result
            });
        }
Example #6
0
        public override async Task <CartAggregate> Handle(AddCartItemCommand request, CancellationToken cancellationToken)
        {
            var cartAggregate = await GetOrCreateCartFromCommandAsync(request);

            var product = (await _cartProductService.GetCartProductsByIdsAsync(cartAggregate, new[] { request.ProductId })).FirstOrDefault();
            await cartAggregate.AddItemAsync(new NewCartItem(request.ProductId, request.Quantity)
            {
                Comment           = request.Comment,
                DynamicProperties = request.DynamicProperties,
                Price             = request.Price,
                CartProduct       = product
            });

            return(await SaveCartAsync(cartAggregate));
        }
        public override async Task <CartAggregate> Handle(ChangeCartItemCommentCommand request, CancellationToken cancellationToken)
        {
            var cartAggregate = await GetOrCreateCartFromCommandAsync(request);

            var lineItem = cartAggregate.Cart.Items.FirstOrDefault(x => x.Id.Equals(request.LineItemId));

            if ((await _cartProductService.GetCartProductsByIdsAsync(cartAggregate, new[] { lineItem.ProductId })).FirstOrDefault() == null)
            {
                return(cartAggregate);
            }

            await cartAggregate.ChangeItemCommentAsync(new NewItemComment(request.LineItemId, request.Comment));

            return(await SaveCartAsync(cartAggregate));
        }