public static IRuleContext CreateRuleContext(ShoppingCart cart) { var excludeConfig = AppLogic.AppConfig("Promotions.ExcludeStates"); var filterByStore = false; var allowPromoFiltering = GlobalConfig.GetGlobalConfig("AllowPromotionFiltering"); if (allowPromoFiltering != null) { bool.TryParse(allowPromoFiltering.ConfigValue, out filterByStore); } var cartItems = cart.CartItems .Where(ci => !ci.IsGift) .Select(ci => new ShoppingCartItem { CartPrice = ci.Price, CategoryIds = Array.ConvertAll <string, int>(AppLogic.GetProductEntityMappings(ci.ProductID, "CATEGORY").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), ManufacturerIds = Array.ConvertAll <string, int>(AppLogic.GetProductEntityMappings(ci.ProductID, "MANUFACTURER").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), ProductId = ci.ProductID, Quantity = ci.Quantity, SectionIds = Array.ConvertAll <string, int>(AppLogic.GetProductEntityMappings(ci.ProductID, "SECTION").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), ShoppingCartRecordId = ci.ShoppingCartRecordID, Sku = ci.SKU, Name = ci.ProductName, Subtotal = ci.Price * ci.Quantity, VariantId = ci.VariantID, IsGift = ci.IsGift } ); //To check is Promotion applied Before Quantity Discount. var includeDiscounts = AppLogic.AppConfigBool("Promotions.ApplyDiscountsBeforePromoApplied"); //Get the checkout context so we can use the shipping method the customer chose var persistedCheckoutContextProvider = (IPersistedCheckoutContextProvider)System.Web.Mvc.DependencyResolver.Current.GetService(typeof(IPersistedCheckoutContextProvider)); var persistedCheckoutContext = persistedCheckoutContextProvider.LoadCheckoutContext(cart.ThisCustomer); var shippingMethodId = persistedCheckoutContext.SelectedShippingMethodId != null ? (int)persistedCheckoutContext.SelectedShippingMethodId : 0; var ruleContext = new SimpleRuleContext { ShoppingCartItems = cartItems, StoreId = AppLogic.StoreID(), CustomerId = cart.ThisCustomer.CustomerID, IsRegistered = cart.ThisCustomer.IsRegistered, BillingAddressId = cart.ThisCustomer.PrimaryBillingAddressID, ShippingAddressId = cart.ThisCustomer.PrimaryShippingAddressID, EmailAddress = cart.ThisCustomer.EMail, CustomerLevel = cart.ThisCustomer.CustomerLevelID, ShippingMethodId = shippingMethodId, State = cart.ThisCustomer.PrimaryShippingAddress.State, ZipCode = cart.ThisCustomer.PrimaryShippingAddress.Zip, CountryCode = cart.ThisCustomer.PrimaryShippingAddress.Country, ShippingTotal = cart.ShippingTotal(true, true), SubTotal = cart.SubTotal(includeDiscounts, false, true, true), ExcludeStates = excludeConfig.Length > 0 ? excludeConfig.Split(',') : null, FilterByStore = filterByStore, AddItemToCart = (int productId, int variantId, int quantity) => { var cartActionProvider = DependencyResolver.Current.GetService <CartActionProvider>(); return(cartActionProvider.PromotionAdd(cart.ThisCustomer, cart.CartType, productId, variantId, quantity)); }, CartType = (int?)cart.CartType }; return(ruleContext); }
public static IRuleContext CreateRuleContext(ShoppingCart cart) { if (!AppLogic.AppConfigExists("AspDotNetStorefront.Promotions.excludestates")) { AppLogic.AddAppConfig("AspDotNetStorefront.Promotions.excludestates", "states to be excluded from shipping promotions", String.Empty, "CUSTOM", false); } string excludeConfig = AppLogic.AppConfig("AspDotNetStorefront.Promotions.excludestates"); bool filterByStore = false; var allowCouponFiltering = GlobalConfig.getGlobalConfig("AllowCouponFiltering"); if (allowCouponFiltering != null) { Boolean.TryParse(allowCouponFiltering.ConfigValue, out filterByStore); } var cartItems = cart.CartItems .Where(ci => !ci.IsGift) .Select(ci => new ShoppingCartItem { CartPrice = ci.Price, CategoryIds = Array.ConvertAll <string, int>(AppLogic.GetProductEntityMappings(ci.ProductID, "CATEGORY").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), ManufacturerIds = Array.ConvertAll <string, int>(AppLogic.GetProductEntityMappings(ci.ProductID, "MANUFACTURER").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), ProductId = ci.ProductID, Quantity = ci.Quantity, SectionIds = Array.ConvertAll <string, int>(AppLogic.GetProductEntityMappings(ci.ProductID, "SECTION").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), ShoppingCartRecordId = ci.ShoppingCartRecordID, Sku = ci.SKU, Name = ci.ProductName, Subscription = new Subscription() { Interval = ci.SubscriptionInterval, IntervalType = (int)ci.SubscriptionIntervalType }, Subtotal = ci.Price * ci.Quantity, VariantId = ci.VariantID, IsGift = ci.IsGift } ); //To check is Promotion applied Before Quantity Discount. bool includeDiscounts = AppLogic.AppConfigBool("Promotions.ApplyDiscountsBeforePromoApplied"); var ruleContext = new SimpleRuleContext { ShoppingCartItems = cartItems, StoreId = AppLogic.StoreID(), CustomerId = cart.ThisCustomer.CustomerID, IsRegistered = cart.ThisCustomer.IsRegistered, BillingAddressId = cart.ThisCustomer.PrimaryBillingAddressID, ShippingAddressId = cart.ThisCustomer.PrimaryShippingAddressID, EmailAddress = cart.ThisCustomer.EMail, CustomerLevel = cart.ThisCustomer.CustomerLevelID, ShippingMethodId = cart.CartItems.Count > 0 ? cart.FirstItem().ShippingMethodID : 0, State = cart.ThisCustomer.PrimaryShippingAddress.State, ZipCode = cart.ThisCustomer.PrimaryShippingAddress.Zip, CountryCode = cart.ThisCustomer.PrimaryShippingAddress.Country, ShippingTotal = cart.ShippingTotal(true, true), SubTotal = cart.SubTotal(includeDiscounts, false, true, true), ExcludeStates = excludeConfig.Length > 0 ? excludeConfig.Split(',') : null, FilterByStore = filterByStore, AddItemToCart = cart.AddItemToCart, MultiShipEnabled = AppLogic.AppConfigBool("AllowMultipleShippingAddressPerOrder") || AppLogic.AppConfigBool("ShowGiftRegistryButtons"), CartType = (Int32?)cart.CartType }; return(ruleContext); }