protected void btnAddPromotion_Click(Object sender, EventArgs e) { String promotionCode = txtPromotionCode.Text.ToLower().Trim(); txtPromotionCode.Text = String.Empty; lblPromotionError.Text = String.Empty; IEnumerable <IPromotionValidationResult> validationResults = PromotionManager.ValidatePromotion(promotionCode, PromotionManager.CreateRuleContext(cart)); if (validationResults.Count() > 0 && validationResults.Any(vr => !vr.IsValid)) { foreach (var reason in validationResults.Where(vr => !vr.IsValid).SelectMany(vr => vr.Reasons)) { String message = reason.MessageKey.StringResource(); if (reason.ContextItems != null && reason.ContextItems.Any()) { foreach (var item in reason.ContextItems) { message = message.Replace(String.Format("{{{0}}}", item.Key), item.Value.ToString()); } } lblPromotionError.Text += String.Format("<div class='promotion-reason error-wrap'>{0}</div>", message); } return; } else { PromotionManager.AssignPromotion(ThisCustomer.CustomerID, promotionCode); } UpdateCart(); BindPromotions(); }
public ActionResult AddPromoCode(PromotionsViewModel model) { if (!ModelState.IsValid) { return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout)); } var promoCode = model.EnteredCode.ToLower(); var customer = HttpContext.GetCustomer(); var cart = CachedShoppingCartProvider.Get(customer, CartTypeEnum.ShoppingCart, AppLogic.StoreID()); var validationResults = PromotionManager.ValidatePromotion(promoCode, PromotionManager.CreateRuleContext(cart)); var validationFailureMessages = validationResults .Where(validationResult => !validationResult.IsValid) .SelectMany(validationResult => validationResult.Reasons) .Select(reason => new { message = AppLogic.GetString(reason.MessageKey), context = reason.ContextItems, }) .Select(o => o.context == null ? o.message : o.context.Aggregate( o.message, (message, item) => message.Replace(string.Format("{{{0}}}", item.Key), item.Value.ToString()))); if (validationFailureMessages.Any()) { foreach (var failureMessage in validationFailureMessages) { ModelState.AddModelError("EnteredCode", failureMessage); } return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout)); } PromotionManager.AssignPromotion(customer.CustomerID, promoCode); return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout)); }