Пример #1
0
        /// <summary>
        /// Checks whether the cart is eligible for any Promotions
        /// </summary>
        /// <returns>A bool whether it is eligible</returns>
        private bool CheckForPromotionEligibility()
        {
            var promotions = _promotionEngine.GetPromotions();

            // If no promotion has been added, then we check for eligibility
            if (!_cart.IsPromotionUsed)
            {
                foreach (var promotion in promotions)
                {
                    // Create temporary dict to store a promotion like the SKUs
                    // are stored in the cart.
                    var tempDict = new Dictionary <SKUEnum, int>();
                    foreach (var data in promotion.Promotions)
                    {
                        tempDict.Add(data.SKU, data.Count);
                    }

                    // Check if any promotion has matched
                    var matchCount = tempDict
                                     .Where(entry => _cart.SKUs.ContainsKey(entry.Key) && _cart.SKUs[entry.Key] % entry.Value == 0)
                                     .ToDictionary(entry => entry.Key, entry => entry.Value);

                    // If any promotion has matched, we save the promotion type
                    // and break out of the loop.
                    if (tempDict.Count == matchCount.Count)
                    {
                        _cart.IsPromotionUsed = true;
                        _cart.ActivePromotion = promotion;
                        break;
                    }
                }
            }

            return(_cart.IsPromotionUsed);
        }