コード例 #1
0
        public Product ProcessCatalog(Product product, int testOfferRuleId = 0)
        {
            if (product.OpenForOffer)
            {
                var catalogRules = _offerBuilder.GetActiveOfferRulesByType(OfferRuleType.Catalog);

                if (testOfferRuleId > 0)
                {
                    var testRule = _offerBuilder.PrepareTestOfferRule(testOfferRuleId);
                    catalogRules = _offerBuilder.CloneActiveOfferRulesByType(OfferRuleType.Catalog);
                    catalogRules.Remove(testRule);
                    catalogRules.Add(testRule);
                    catalogRules = catalogRules.OrderBy(x => x.Priority).ToList();
                }

                if (catalogRules.Count > 0)
                {
                    for (int i = 0; i < catalogRules.Count; i++)
                    {
                        // check status and dates
                        // in theory, all inactive / expired rules will not be loaded from database
                        if (catalogRules[i].IsActive)
                        {
                            // check dates
                            if ((catalogRules[i].StartDate.HasValue == false || catalogRules[i].StartDate.Value.CompareTo(DateTime.Now) <= 0) &&
                                (catalogRules[i].EndDate.HasValue == false || catalogRules[i].EndDate.Value.CompareTo(DateTime.Now) >= 0))
                            {
                                bool conditionMatched = _catalogUtility.ProcessCatalogCondition(catalogRules[i].Id, catalogRules[i].Condition, product);

                                if (conditionMatched)
                                {
                                    // run action here
                                    product = _catalogUtility.ProcessCatalogOfferAction(product, catalogRules[i]);
                                }

                                if (!catalogRules[i].ProceedForNext)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(product);
        }
コード例 #2
0
        public CartOffer ProcessCart(int profileId, string shippingCountryCode, string promoCode = "", int testOfferRuleId = 0)
        {
            promoCode = promoCode.ToLower().Trim();
            CartOffer cartOffer = new CartOffer();

            var cartRules = _offerBuilder.GetActiveOfferRulesByType(OfferRuleType.Cart);

            if (testOfferRuleId > 0)
            {
                var testRule = _offerBuilder.PrepareTestOfferRule(testOfferRuleId);
                cartRules = _offerBuilder.CloneActiveOfferRulesByType(OfferRuleType.Cart);
                cartRules.Remove(testRule);
                cartRules.Add(testRule);
                cartRules = cartRules.OrderBy(x => x.Priority).ToList();
            }

            if (cartRules.Count > 0)
            {
                // Get all items by profile ID
                var items = _cartItemRepository.Table
                            .Where(x => x.ProfileId == profileId)
                            .ToList()
                            .Select(x => _cartItemBuilder.Build(x))
                            .ToList();

                // Do a basket cleanup
                items = _cartUtility.RemoveFreeItemWithOffers(items, cartRules.Select(x => x.Id).ToArray());

                for (int i = 0; i < cartRules.Count; i++)
                {
                    // Check status and dates
                    // In theory, all inactive / expired rules will not be loaded from database
                    if ((cartRules[i].IsActive) &&
                        ((cartRules[i].StartDate.HasValue == false || cartRules[i].StartDate.Value.CompareTo(DateTime.Now) <= 0) &&
                         (cartRules[i].EndDate.HasValue == false || cartRules[i].EndDate.Value.CompareTo(DateTime.Now) >= 0)))
                    {
                        bool proceed = true;

                        if (profileId != 0 && !string.IsNullOrEmpty(promoCode) && promoCode.CompareTo(cartRules[i].PromoCode.ToLower()) == 0)
                        {
                            int orderQuantity = _cartUtility.GetNumberOfPaidOrders(promoCode, profileId);

                            // Check promocode and uses per customer
                            // value <= 0 will be considered as proceed
                            proceed = cartRules[i].UsesPerCustomer <= 0 || orderQuantity < cartRules[i].UsesPerCustomer;

                            if (cartRules[i].NewCustomerOnly && orderQuantity > 0)
                            {
                                proceed = false;
                            }
                        }

                        // Set html message from this offer
                        if (string.IsNullOrEmpty(cartRules[i].PromoCode) == false && cartRules[i].PromoCode.ToLower().CompareTo(promoCode) == 0)
                        {
                            cartOffer.Description = cartRules[i].HtmlMessage;
                        }

                        // If entered promocode is empty and promocode from offer is not empty, do not proceed
                        if (string.IsNullOrEmpty(cartRules[i].PromoCode) == false && string.IsNullOrEmpty(promoCode))
                        {
                            proceed = false;
                        }

                        bool conditionMatched = false;

                        if (proceed)
                        {
                            conditionMatched = _cartUtility.ProcessCartCondition(items, cartRules[i].Condition, cartRules[i].OfferedItemIncluded, cartRules[i].UseInitialPrice);

                            if (conditionMatched)
                            {
                                // If cart rule does not have promocode, proceed
                                // If cart rule does not match the given promocode, do not proceed
                                if (string.IsNullOrEmpty(cartRules[i].PromoCode) ||
                                    cartRules[i].PromoCode.ToLower().CompareTo(promoCode) == 0)
                                {
                                    // Run action here
                                    cartOffer = _cartUtility.ProcessCartOfferAction(profileId, items, shippingCountryCode, cartRules[i], cartRules[i].Action, cartOffer);
                                }
                            }

                            if (!cartRules[i].ProceedForNext)
                            {
                                break;
                            }
                        }
                    }
                }

                // Update all cart items in database at the end of the loop.
                foreach (var item in items)
                {
                    if (item.Id > 0)
                    {
                        _cartItemRepository.Update(item);
                    }
                    else
                    {
                        _cartItemRepository.Create(item);
                    }
                }
            }

            return(cartOffer);
        }