public ActionResult AddCouponCode(string couponCode) { // Gets the current user's shopping cart ShoppingCart currentCart = shoppingService.GetCurrentShoppingCart(); // Adds the coupon code to the shopping cart // The 'ShoppingCart.AddCouponCode' method automatically recalculates the shopping cart, // so there is no need to call the Evaluate() method after if ((couponCode == "") || !currentCart.AddCouponCode(couponCode)) { // Adds an error message to the model state if the entered coupon code is not valid ModelState.AddModelError("CouponCodeError", "The entered coupon code is not valid."); } // Initializes the shopping cart model ShoppingCartViewModel model = new ShoppingCartViewModel { // Assigns the current shopping cart to the model Cart = currentCart, RemainingAmountForFreeShipping = pricingService.CalculateRemainingAmountForFreeShipping(currentCart) }; // Displays the shopping cart return(View("ShoppingCart", model)); }
/// <summary> /// Checks whether entered discount coupon code is usable for this cart and applies it. Returns true if so. /// </summary> private bool ApplyDiscountCoupon() { if (string.IsNullOrEmpty(CouponCode)) { return(false); } var isApplied = ShoppingCart.AddCouponCode(CouponCode); if (isApplied) { CouponCode = null; } ErrorMessage = isApplied ? string.Empty : GetString("ecommerce.error.couponcodeisnotvalid"); return(isApplied); }
protected void btnUpdate_Click(object sender, EventArgs e) { if (ShoppingCart != null) { // Do not update order if it is already paid if (OrderIsPaid) { return; } if (selectCurrency.SelectedID > 0) { ShoppingCart.ShoppingCartCurrencyID = selectCurrency.SelectedID; } // Skip if method was called by btnAddProduct if (sender != btnAddProduct) { foreach (GridViewRow row in gridData.Rows) { // Get shopping cart item Guid Guid cartItemGuid = ValidationHelper.GetGuid(((Label)row.Cells[1].Controls[1]).Text, Guid.Empty); // Try to find shopping cart item in the list var cartItem = ShoppingCartInfoProvider.GetShoppingCartItem(ShoppingCart, cartItemGuid); if (cartItem != null) { // If product and its product options should be removed if (((CMSCheckBox)row.Cells[4].Controls[1]).Checked && (sender != null)) { // Remove product and its product option from list ShoppingCartInfoProvider.RemoveShoppingCartItem(ShoppingCart, cartItemGuid); if (!ShoppingCartControl.IsInternalOrder) { // Log activity if (!cartItem.IsProductOption && !cartItem.IsBundleItem) { var logger = Service.Resolve <IEcommerceActivityLogger>(); logger.LogProductRemovedFromShoppingCartActivity(cartItem.SKU, cartItem.CartItemUnits, ContactID); } // Delete product from database ShoppingCartItemInfoProvider.DeleteShoppingCartItemInfo(cartItem); } } // If product units has changed else if (!cartItem.IsProductOption) { // Get number of units int itemUnits = ValidationHelper.GetInteger(((TextBox)(row.Cells[7].Controls[1])).Text.Trim(), 0); if ((itemUnits > 0) && (cartItem.CartItemUnits != itemUnits)) { // Update units of the parent product ShoppingCartItemInfoProvider.UpdateShoppingCartItemUnits(cartItem, itemUnits); } } } } } var couponCode = txtCoupon.Text.Trim(); if (!string.IsNullOrEmpty(couponCode)) { if (!ShoppingCart.AddCouponCode(couponCode)) { // Discount coupon is not valid lblError.Text = GetString("ecommerce.error.couponcodeisnotvalid"); } else { txtCoupon.Text = string.Empty; } } ShoppingCart.Evaluate(); ReloadData(); // Inventory should be checked checkInventory = true; } }