예제 #1
0
        public virtual async Task EvaluatePromotionsAsync()
        {
            EnsureCartExists();

            bool isReadOnlyLineItems = Cart.Items.Any(i => i.IsReadOnly);

            if (!isReadOnlyLineItems)
            {
                //Get product inventory to fill InStockQuantity parameter of LineItem
                var products = await GetCartProductsAsync();

                foreach (var lineItem in Cart.Items.ToList())
                {
                    var product = products.FirstOrDefault(p => p.Id == lineItem.ProductId);
                    lineItem.InStockQuantity = (int)await _productAvailabilityService.GetAvailableQuantity(product);
                }

                var evalContext = Cart.ToPromotionEvaluationContext();
                await _promotionEvaluator.EvaluateDiscountsAsync(evalContext, new IDiscountable[] { Cart });
            }
        }
예제 #2
0
        protected virtual async Task ValidateCartItemsAsync()
        {
            var productIds = Cart.Items.Select(i => i.ProductId).ToArray();
            var cacheKey   = "CartBuilder.ValidateCartItemsAsync:" + Cart.Id + ":" + string.Join(":", productIds);
            var products   = await _cacheManager.GetAsync(cacheKey, "ApiRegion", async() => await _catalogSearchService.GetProductsAsync(productIds, ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory));

            foreach (var lineItem in Cart.Items.ToList())
            {
                lineItem.ValidationErrors.Clear();

                var product = products.FirstOrDefault(p => p.Id == lineItem.ProductId);
                if (product == null || !product.IsActive || !product.IsBuyable)
                {
                    lineItem.ValidationErrors.Add(new UnavailableError());
                    lineItem.IsValid = false;
                }
                else
                {
                    var isProductAvailable = await _productAvailabilityService.IsAvailable(product, lineItem.Quantity);

                    if (!isProductAvailable)
                    {
                        lineItem.IsValid = false;

                        var availableQuantity = await _productAvailabilityService.GetAvailableQuantity(product);

                        lineItem.ValidationErrors.Add(new QuantityError(availableQuantity));
                    }

                    var tierPrice = product.Price.GetTierPrice(lineItem.Quantity);
                    if (tierPrice.Price > lineItem.SalePrice)
                    {
                        lineItem.ValidationErrors.Add(new PriceError(lineItem.SalePrice, lineItem.SalePriceWithTax, tierPrice.Price, tierPrice.PriceWithTax));
                    }
                }
            }
        }