Пример #1
0
        public static List <DiscountableItem> GetDiscountableItems(IRuleContext ruleContext, int promoId)
        {
            var discountableItems  = new List <DiscountableItem>();
            var nonDiscountableIds = GetNonDiscountableShoppingCartIds(ruleContext);
            var promotion          = PromotionsData.DataContextProvider.Current
                                     .Promotions
                                     .FirstOrDefault(p => p.Active && p.Id == promoId);

            ruleContext.PromotionId = promotion.Id;

            foreach (var pd in promotion
                     .PromotionDiscounts
                     .Where(p => p.GetType() == typeof(OrderItemPromotionDiscount) || p.GetType() == typeof(GiftProductPromotionDiscount) || p.GetType() == typeof(OrderPromotionDiscount)))
            {
                var categoryIdRule     = new CategoryPromotionRule();
                var sectionIdRule      = new SectionPromotionRule();
                var manufacturerIdRule = new ManufacturerPromotionRule();
                var productIdRule      = new ProductIdPromotionRule();

                if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(CategoryPromotionRule)).Count() > 0)
                {
                    categoryIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(CategoryPromotionRule)).First() as CategoryPromotionRule;
                }

                if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(SectionPromotionRule)).Count() > 0)
                {
                    sectionIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(SectionPromotionRule)).First() as SectionPromotionRule;
                }

                if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ManufacturerPromotionRule)).Count() > 0)
                {
                    manufacturerIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ManufacturerPromotionRule)).First() as ManufacturerPromotionRule;
                }

                if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ProductIdPromotionRule)).Count() > 0)
                {
                    productIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ProductIdPromotionRule)).First() as ProductIdPromotionRule;
                }

                foreach (var cartItem in ruleContext.ShoppingCartItems)
                {
                    var qualifies = true;
                    if (categoryIdRule.CategoryIds != null && categoryIdRule.CategoryIds.Count() > 0 && cartItem.CategoryIds != null && cartItem.CategoryIds.Intersect(categoryIdRule.CategoryIds).Count() == 0)
                    {
                        qualifies = false;
                    }

                    if (sectionIdRule.SectionIds != null && sectionIdRule.SectionIds.Count() > 0 && cartItem.SectionIds != null && cartItem.SectionIds.Intersect(sectionIdRule.SectionIds).Count() == 0)
                    {
                        qualifies = false;
                    }

                    if (manufacturerIdRule.ManufacturerIds != null && manufacturerIdRule.ManufacturerIds.Count() > 0 && cartItem.ManufacturerIds != null && cartItem.ManufacturerIds.Intersect(manufacturerIdRule.ManufacturerIds).Count() == 0)
                    {
                        qualifies = false;
                    }

                    if (productIdRule.ProductIds != null && !productIdRule.ProductIds.Contains(cartItem.ProductId))
                    {
                        qualifies = false;
                    }

                    if (nonDiscountableIds.Contains(cartItem.ShoppingCartRecordId))
                    {
                        qualifies = false;
                    }

                    if (qualifies)
                    {
                        if (pd.GetType() == typeof(OrderItemPromotionDiscount) || pd.GetType() == typeof(GiftProductPromotionDiscount))
                        {
                            var discountableItem = new DiscountableItem();
                            discountableItem.CartPrice            = cartItem.CartPrice;
                            discountableItem.ProductId            = cartItem.ProductId;
                            discountableItem.Quantity             = cartItem.Quantity;
                            discountableItem.ShoppingCartRecordId = cartItem.ShoppingCartRecordId;
                            discountableItem.Sku       = cartItem.Sku;
                            discountableItem.Subtotal  = cartItem.Subtotal;
                            discountableItem.VariantId = cartItem.VariantId;
                            discountableItems.Add(discountableItem);
                        }
                    }
                }
            }

            return(discountableItems);
        }
Пример #2
0
        public static List <DiscountableItem> GetDiscountableItems(IRuleContext ruleContext, int promoId)
        {
            List <DiscountableItem> discountableItems   = new List <DiscountableItem>();
            List <Int32>            nonDiscountableIds  = GetNonDiscountableShoppingCartIds(ruleContext);
            PromotionController     promotionController = CreatePromotionController();

            PromotionsData.Promotion promotion = PromotionsData.DataContextProvider.Current.Promotions.FirstOrDefault(p => p.Active && p.Id == promoId);
            ruleContext.PromotionId = promotion.Id;

            if (promotionController.ValidatePromotion(promotion, ruleContext, AppLogic.CustomerLevelAllowsCoupons(ruleContext.CustomerLevel)).All(vr => vr.IsValid))
            {
                foreach (PromotionDiscountBase pd in promotion.PromotionDiscounts.Where(p => p.GetType() == typeof(OrderItemPromotionDiscount) || p.GetType() == typeof(GiftProductPromotionDiscount) || p.GetType() == typeof(OrderPromotionDiscount)))
                {
                    CategoryPromotionRule     categoryIdRule     = new CategoryPromotionRule();
                    SectionPromotionRule      sectionIdRule      = new SectionPromotionRule();
                    ManufacturerPromotionRule manufacturerIdRule = new ManufacturerPromotionRule();
                    ProductIdPromotionRule    productIdRule      = new ProductIdPromotionRule();

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(CategoryPromotionRule)).Count() > 0)
                    {
                        categoryIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(CategoryPromotionRule)).First() as CategoryPromotionRule;
                    }

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(SectionPromotionRule)).Count() > 0)
                    {
                        sectionIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(SectionPromotionRule)).First() as SectionPromotionRule;
                    }

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ManufacturerPromotionRule)).Count() > 0)
                    {
                        manufacturerIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ManufacturerPromotionRule)).First() as ManufacturerPromotionRule;
                    }

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ProductIdPromotionRule)).Count() > 0)
                    {
                        productIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ProductIdPromotionRule)).First() as ProductIdPromotionRule;
                    }

                    foreach (ShoppingCartItem cartItem in ruleContext.ShoppingCartItems)
                    {
                        bool qualifies = true;
                        if (categoryIdRule.CategoryIds != null && categoryIdRule.CategoryIds.Count() > 0 && cartItem.CategoryIds != null && cartItem.CategoryIds.Intersect(categoryIdRule.CategoryIds).Count() == 0)
                        {
                            qualifies = false;
                        }

                        if (sectionIdRule.SectionIds != null && sectionIdRule.SectionIds.Count() > 0 && cartItem.SectionIds != null && cartItem.SectionIds.Intersect(sectionIdRule.SectionIds).Count() == 0)
                        {
                            qualifies = false;
                        }

                        if (manufacturerIdRule.ManufacturerIds != null && manufacturerIdRule.ManufacturerIds.Count() > 0 && cartItem.ManufacturerIds != null && cartItem.ManufacturerIds.Intersect(manufacturerIdRule.ManufacturerIds).Count() == 0)
                        {
                            qualifies = false;
                        }

                        if (productIdRule.ProductIds != null && !productIdRule.ProductIds.Contains(cartItem.ProductId))
                        {
                            qualifies = false;
                        }

                        if (nonDiscountableIds.Contains(cartItem.ShoppingCartRecordId))
                        {
                            qualifies = false;
                        }

                        if (qualifies)
                        {
                            if (pd.GetType() == typeof(OrderItemPromotionDiscount) || pd.GetType() == typeof(GiftProductPromotionDiscount))
                            {
                                DiscountableItem discountableItem = new DiscountableItem();
                                discountableItem.CartPrice            = cartItem.CartPrice;
                                discountableItem.ProductId            = cartItem.ProductId;
                                discountableItem.Quantity             = cartItem.Quantity;
                                discountableItem.ShoppingCartRecordId = cartItem.ShoppingCartRecordId;
                                discountableItem.Sku       = cartItem.Sku;
                                discountableItem.Subtotal  = cartItem.Subtotal;
                                discountableItem.VariantId = cartItem.VariantId;
                                discountableItems.Add(discountableItem);
                            }
                        }
                    }
                }
            }
            return(discountableItems);
        }