Ejemplo n.º 1
0
        public virtual async Task AddItemAsync(Product product, int quantity)
        {
            EnsureCartExists();

            var isProductAvailable = await _productAvailabilityService.IsAvailable(product, quantity);

            if (isProductAvailable)
            {
                var lineItem = product.ToLineItem(Cart.Language, quantity);
                await AddLineItemAsync(lineItem);
            }
        }
        public virtual async Task <Product[]> GetProductsAsync(string[] ids, ItemResponseGroup responseGroup = ItemResponseGroup.None)
        {
            Product[] result;

            if (ids.IsNullOrEmpty())
            {
                result = new Product[0];
            }
            else
            {
                var workContext = _workContextFactory();

                if (responseGroup == ItemResponseGroup.None)
                {
                    responseGroup = workContext.CurrentProductResponseGroup;
                }

                result = await GetProductsAsync(ids, responseGroup, workContext);

                var allProducts = result.Concat(result.SelectMany(p => p.Variations)).ToList();

                if (!allProducts.IsNullOrEmpty())
                {
                    var taskList = new List <Task>();

                    if (responseGroup.HasFlag(ItemResponseGroup.ItemAssociations))
                    {
                        taskList.Add(LoadProductAssociationsAsync(allProducts));
                    }

                    if (responseGroup.HasFlag(ItemResponseGroup.Inventory))
                    {
                        taskList.Add(LoadProductInventoriesAsync(allProducts, workContext));
                    }

                    if (responseGroup.HasFlag(ItemResponseGroup.ItemWithPrices))
                    {
                        taskList.Add(_pricingService.EvaluateProductPricesAsync(allProducts, workContext));
                    }

                    if (responseGroup.HasFlag(ItemResponseGroup.ItemWithVendor))
                    {
                        taskList.Add(LoadProductVendorsAsync(allProducts, workContext));
                    }

                    if (workContext.CurrentStore.SubscriptionEnabled && responseGroup.HasFlag(ItemResponseGroup.ItemWithPaymentPlan))
                    {
                        taskList.Add(LoadProductPaymentPlanAsync(allProducts, workContext));
                    }

                    await Task.WhenAll(taskList.ToArray());

                    foreach (var product in allProducts)
                    {
                        product.IsAvailable = await _productAvailabilityService.IsAvailable(product, 1);
                    }
                }
            }

            return(result);
        }