public static bool HasQualifyingItemGiftProductPromotion(IRuleContext ruleContext) { bool isQualifyingItemGiftProductPromotion = false; PromotionController promotionController = CreatePromotionController(); foreach (PromotionsData.PromotionUsage promotionUsage in PromotionsData.DataContextProvider.Current.PromotionUsages.Where(pu => pu.CustomerId == ruleContext.CustomerId && pu.Complete == false)) { PromotionsData.Promotion promotion = PromotionsData.DataContextProvider.Current.Promotions.FirstOrDefault(p => p.Id == promotionUsage.PromotionId); 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(GiftProductPromotionDiscount))) { CategoryPromotionRule categories = new CategoryPromotionRule(); SectionPromotionRule sections = new SectionPromotionRule(); ManufacturerPromotionRule manufacturers = new ManufacturerPromotionRule(); ProductIdPromotionRule productids = new ProductIdPromotionRule(); if (promotion.PromotionRules.Where(pr => (pr.GetType() == typeof(CategoryPromotionRule)) || (pr.GetType() == typeof(SectionPromotionRule)) || (pr.GetType() == typeof(ManufacturerPromotionRule)) || (pr.GetType() == typeof(ProductIdPromotionRule))).Count() > 0) { isQualifyingItemGiftProductPromotion = true; } } } } return(isQualifyingItemGiftProductPromotion); }
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); }
private void EditForm(Int32 id) { ClearForm(); AspDotNetStorefront.Promotions.Data.Promotion promotion = AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.FirstOrDefault(l => l.Id == id); if (promotion == null) { ToggleView(true, true); return; } else { ToggleView(true, false); } txtId.Value = promotion.Id.ToString(); lblTitle.Text = txtName.Text = promotion.Name; txtDescription.Text = promotion.Description; txtUsageText.Text = promotion.UsageText; txtCode.Text = promotion.Code; txtPriority.Text = promotion.Priority.ToString(); rblActive.SelectedValue = promotion.Active.ToString().ToLower(); chkAutoAssigned.Checked = promotion.AutoAssigned; CallToActionTextbox.Text = promotion.CallToAction; ssPromotion.SelectedStoreIDs = promotion.PromotionStores.Select(ps => ps.StoreID).ToArray(); foreach (PromotionRuleBase rule in promotion.PromotionRules) { StartDatePromotionRule startDateRule = rule as StartDatePromotionRule; if (startDateRule != null) { optRuleStartDate.Checked = true; txtRuleStartDate.SelectedDate = startDateRule.StartDate; } ExpirationDatePromotionRule expirationDateRule = rule as ExpirationDatePromotionRule; if (expirationDateRule != null) { optRuleExpirationDate.Checked = true; txtRuleExpirationDate.SelectedDate = expirationDateRule.ExpirationDate; } ExpirationNumberOfUsesPromotionRule expirationDateNumberOfUsesRule = rule as ExpirationNumberOfUsesPromotionRule; if (expirationDateNumberOfUsesRule != null) { optRuleExpirationNumberOfUses.Checked = true; txtRuleExpirationNumberOfUses.Text = expirationDateNumberOfUsesRule.NumberOfUsesAllowed.ToString(); chkRuleExpirationNumberOfUsesPerCustomer.Checked = false; } ExpirationNumberOfUsesPerCustomerPromotionRule expirationDateNumberOfUsesPerCustomerRule = rule as ExpirationNumberOfUsesPerCustomerPromotionRule; if (expirationDateNumberOfUsesPerCustomerRule != null) { optRuleExpirationNumberOfUses.Checked = true; txtRuleExpirationNumberOfUses.Text = expirationDateNumberOfUsesPerCustomerRule.NumberOfUsesAllowed.ToString(); chkRuleExpirationNumberOfUsesPerCustomer.Checked = true; } EmailAddressPromotionRule emailAddressRule = rule as EmailAddressPromotionRule; if (emailAddressRule != null) { chkRuleEmail.Checked = true; txtRuleEmailAddresses.Text = String.Join(",", emailAddressRule.EmailAddresses); continue; } MinimumCartAmountPromotionRule cartAmountRule = rule as MinimumCartAmountPromotionRule; if (cartAmountRule != null) { chkRuleCartAmount.Checked = true; txtRuleCartAmount.Text = cartAmountRule.CartAmount.ToString(); continue; } CustomerLevelPromotionRule customerLevelRule = rule as CustomerLevelPromotionRule; if (customerLevelRule != null) { chkRuleCustomerLevel.Checked = true; String[] customerLevels = Array.ConvertAll <int, string>(customerLevelRule.CustomerLevels, new Converter <int, string>(Convert.ToString)); txtRuleCustomerLevels.Text = String.Join(",", customerLevels); continue; } CategoryPromotionRule categoryPromotionRule = rule as CategoryPromotionRule; if (categoryPromotionRule != null) { chkRuleCategories.Checked = true; String[] categories = Array.ConvertAll <int, string>(categoryPromotionRule.CategoryIds, new Converter <int, string>(Convert.ToString)); txtRuleCategories.Text = String.Join(",", categories); } SectionPromotionRule sectionPromotionRule = rule as SectionPromotionRule; if (sectionPromotionRule != null) { chkRuleSections.Checked = true; String[] sections = Array.ConvertAll <int, string>(sectionPromotionRule.SectionIds, new Converter <int, string>(Convert.ToString)); txtRuleSections.Text = String.Join(",", sections); } ManufacturerPromotionRule manufacturerPromotionRule = rule as ManufacturerPromotionRule; if (manufacturerPromotionRule != null) { chkRuleManufacturers.Checked = true; String[] manufacturers = Array.ConvertAll <int, string>(manufacturerPromotionRule.ManufacturerIds, new Converter <int, string>(Convert.ToString)); txtRuleManufacturers.Text = String.Join(",", manufacturers); } ProductIdPromotionRule productIdRule = rule as ProductIdPromotionRule; if (productIdRule != null) { chkRuleProductId.Checked = true; String[] productIds = Array.ConvertAll <int, string>(productIdRule.ProductIds, new Converter <int, string>(Convert.ToString)); txtRuleProductIds.Text = String.Join(",", productIds); rblProductsAllOrAny.SelectedIndex = CommonLogic.IIF(productIdRule.AndTogether, 0, 1); txtRuleProductIdsRequireQuantity.Text = productIdRule.Quantity.ToString(); continue; } StatePromotionRule stateRule = rule as StatePromotionRule; if (stateRule != null) { chkRuleState.Checked = true; txtRuleStates.Text = String.Join(",", stateRule.States); continue; } ZipCodePromotionRule zipCodeRule = rule as ZipCodePromotionRule; if (zipCodeRule != null) { chkRuleZipCode.Checked = true; txtRuleZipCodes.Text = String.Join(",", zipCodeRule.ZipCodes); continue; } CountryPromotionRule countryRule = rule as CountryPromotionRule; if (countryRule != null) { chkRuleCountryCodes.Checked = true; txtRuleCountryCodes.Text = String.Join(",", countryRule.CountryCodes); continue; } MinimumOrdersPromotionRule minimumOrdersRule = rule as MinimumOrdersPromotionRule; if (minimumOrdersRule != null) { chkRuleMinimumOrders.Checked = true; txtRuleMinimumOrders.Text = minimumOrdersRule.MinimumOrdersAllowed.ToString(); txtRuleMinimumOrdersCustomStartDate.SelectedDate = minimumOrdersRule.CustomStartDate; txtRuleMinimumOrdersCustomEndDate.SelectedDate = minimumOrdersRule.CustomEndDate; } MinimumOrderAmountPromotionRule minimumOrderAmountRule = rule as MinimumOrderAmountPromotionRule; if (minimumOrderAmountRule != null) { chkRuleMinimumOrderAmount.Checked = true; txtRuleMinimumOrderAmount.Text = minimumOrderAmountRule.MinimumOrderAmountAllowed.ToString(); txtRuleMinimumOrderAmountCustomStartDate.SelectedDate = minimumOrderAmountRule.CustomStartDate; txtRuleMinimumOrderAmountCustomEndDate.SelectedDate = minimumOrderAmountRule.CustomEndDate; } MinimumProductsOrderedPromotionRule minimumProductsOrderedRule = rule as MinimumProductsOrderedPromotionRule; if (minimumProductsOrderedRule != null) { chkRuleMinimumProductsOrdered.Checked = true; txtRuleMinimumProductsOrdered.Text = minimumProductsOrderedRule.MinimumProductsOrderedAllowed.ToString(); txtRuleMinimumProductsOrderedCustomStartDate.SelectedDate = minimumProductsOrderedRule.CustomStartDate; txtRuleMinimumProductsOrderedCustomEndDate.SelectedDate = minimumProductsOrderedRule.CustomEndDate; txtRuleMinimumProductsOrderedProductIds.Text = String.Join(",", Array.ConvertAll <int, string>(minimumProductsOrderedRule.ProductIds, i => i.ToString())); } MinimumProductAmountOrderedPromotionRule minimumProductAmountOrderedRule = rule as MinimumProductAmountOrderedPromotionRule; if (minimumProductAmountOrderedRule != null) { chkRuleMinimumProductsOrderedAmount.Checked = true; txtRuleMinimumProductsOrderedAmount.Text = minimumProductAmountOrderedRule.MinimumProductAmountOrderedAllowed.ToString(); txtRuleMinimumProductsOrderedAmountCustomStartDate.SelectedDate = minimumProductAmountOrderedRule.CustomStartDate; txtRuleMinimumProductsOrderedAmountCustomEndDate.SelectedDate = minimumProductAmountOrderedRule.CustomEndDate; txtRuleMinimumProductsOrderedAmountProductIds.Text = String.Join(",", Array.ConvertAll <int, string>(minimumProductAmountOrderedRule.ProductIds, i => i.ToString())); } } foreach (PromotionDiscountBase discount in promotion.PromotionDiscounts) { ShippingPromotionDiscount shippingDiscount = discount as ShippingPromotionDiscount; if (shippingDiscount != null) { chkRuleShippingDiscount.Checked = true; ddlRuleShippingDiscountType.SelectedValue = ((Int32)shippingDiscount.DiscountType).ToString(); if (shippingDiscount.DiscountType == DiscountType.Fixed) { txtRuleShippingDiscountAmount.Text = shippingDiscount.DiscountAmount.ToString("0.0000"); } else { txtRuleShippingDiscountAmount.Text = (shippingDiscount.DiscountAmount * 100).ToString("0.0000"); } String shipMethodIds = String.Join(",", shippingDiscount.ShippingMethodIds.Select(i => i.ToString()).ToArray()); txtRuleShippingMethodID.Text = shipMethodIds; continue; } GiftProductPromotionDiscount giftProductDiscount = discount as GiftProductPromotionDiscount; if (giftProductDiscount != null && giftProductDiscount.GiftSkus != null) { chkRuleGiftWithPurchase.Checked = true; chkMatchQuantites.Checked = giftProductDiscount.MatchQuantities; txtGiftWithPurchaseDiscountAmount.Text = giftProductDiscount.GiftDiscountPercentage.ToString("0.0000"); } if (giftProductDiscount != null && giftProductDiscount.GiftProductIds != null) { chkRuleGiftWithPurchase.Checked = true; txtRuleGiftWithPurchaseProductId.Text = String.Join(",", Array.ConvertAll <int, string>(giftProductDiscount.GiftProductIds, i => i.ToString())); chkMatchQuantites.Checked = giftProductDiscount.MatchQuantities; txtGiftWithPurchaseDiscountAmount.Text = giftProductDiscount.GiftDiscountPercentage.ToString("0.0000"); } OrderPromotionDiscount orderDiscount = discount as OrderPromotionDiscount; if (orderDiscount != null) { chkRuleOrderDiscount.Checked = true; ddlRuleOrderDiscountType.SelectedValue = ((Int32)orderDiscount.DiscountType).ToString(); if (orderDiscount.DiscountType == DiscountType.Fixed) { txtRuleOrderDiscountAmount.Text = orderDiscount.DiscountAmount.ToString("0.0000"); } else { txtRuleOrderDiscountAmount.Text = (orderDiscount.DiscountAmount * 100).ToString("0.0000"); } continue; } OrderItemPromotionDiscount orderItemDiscount = discount as OrderItemPromotionDiscount; if (orderItemDiscount != null) { chkRuleLineItemDiscount.Checked = true; ddlRuleLineItemDiscountType.SelectedValue = ((Int32)orderItemDiscount.DiscountType).ToString(); if (orderItemDiscount.DiscountType == DiscountType.Fixed) { txtRuleLineItemDiscountAmount.Text = orderItemDiscount.DiscountAmount.ToString("0.0000"); } else { txtRuleLineItemDiscountAmount.Text = (orderItemDiscount.DiscountAmount * 100).ToString("0.0000"); } continue; } } }
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); }