Пример #1
0
        public override async Task CalculateAsync(CalculatorContext context, CalculatorDelegate next)
        {
            var product = context.Product;

            if (product.ProductType != ProductType.GroupedProduct)
            {
                // Proceed with pipeline and omit this calculator, it is made for grouped products only.
                await next(context);

                return;
            }

            var options = context.Options;

            if (options.IgnoreGroupedProducts)
            {
                await next(context);

                return;
            }

            await EnsureAssociatedProductsAreLoaded(product, context);

            if (context.AssociatedProducts.Count == 0)
            {
                // No children, get out.
                return;
            }

            CalculatorContext lowestPriceCalculation = null;

            if (options.DetermineLowestPrice && context.AssociatedProducts.Count > 1)
            {
                foreach (var associatedProduct in context.AssociatedProducts)
                {
                    // Get the final price of associated product
                    var childCalculation = await CalculateChildPriceAsync(associatedProduct, context);

                    if (lowestPriceCalculation == null || childCalculation.FinalPrice < lowestPriceCalculation.FinalPrice)
                    {
                        // Set the lowest price calculation
                        lowestPriceCalculation = childCalculation;
                    }
                }

                lowestPriceCalculation.HasPriceRange = true;
            }
            else
            {
                // Get the final price of first associated product
                lowestPriceCalculation = await CalculateChildPriceAsync(context.AssociatedProducts.First(), context);
            }

            // Copy data from child context to this context
            lowestPriceCalculation.CopyTo(context);
        }