Example #1
0
        public ActionResult ApplyCoupon(string couponCode, bool renderItems = false)
        {
            _catalogClient.CustomerSession.CouponCode = couponCode;
            var helper   = GetCartHelper(CartHelper.CartName);
            var warnings = SaveChanges(helper);

            // Display the confirmation message
            var results = new CartJsonModel
            {
                CartSubTotal  = StoreHelper.FormatCurrency(helper.Cart.Subtotal, helper.Cart.BillingCurrency),
                CartTotal     = StoreHelper.FormatCurrency(helper.Cart.Total, helper.Cart.BillingCurrency),
                CartCount     = helper.LineItems.Count(),
                LineItemsView = renderItems ? RenderRazorViewToString("LineItems", helper.CreateCartModel(true)) : null,
                CartName      = CartHelper.CartName,
                Source        = "LineItems"
            };

            if (warnings != null)
            {
                foreach (var warning in warnings.Values)
                {
                    results.Messages.Add(new MessageModel(warning));
                }
            }

            if (warnings == null || !warnings.ContainsKey(WorkflowMessageCodes.COUPON_NOT_APPLIED))
            {
                results.Messages.Add(string.IsNullOrEmpty(couponCode)
                    ? new MessageModel("Coupon has been cleared".Localize())
                    : new MessageModel("Coupon has been applied".Localize()));
            }


            return(Json(results));
        }
Example #2
0
        public ActionResult Add(string name, string itemId, string parentItemId, decimal?quantity, string[] relatedItemId = null)
        {
            decimal qty = 1;

            if (quantity.HasValue)
            {
                qty = quantity.Value;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = CartHelper.CartName;
            }

            var helper = GetCartHelper(name);

            var catalogItem = _catalogClient.GetItem(itemId);
            var parentItem  = !string.IsNullOrEmpty(parentItemId) ? _catalogClient.GetItem(parentItemId) : null;

            var addedLineItems = new List <LineItemModel>();

            var addedLineItem = DoAddToCart(name, qty, catalogItem, parentItem);

            if (addedLineItem != null)
            {
                addedLineItems.Add(new LineItemModel(addedLineItem, catalogItem, parentItem, helper.Cart.BillingCurrency));
            }

            if (relatedItemId != null && relatedItemId.Length > 0)
            {
                addedLineItems.AddRange(from relItemId in relatedItemId
                                        let relItem = _catalogClient.GetItem(relItemId)
                                                      let relatedItem = DoAddToCart(name, 1, relItem, null)
                                                                        where relatedItem != null
                                                                        select new LineItemModel(relatedItem, relItem, null, helper.Cart.BillingCurrency));
            }

            if (Request.UrlReferrer != null)
            {
                UserHelper.CustomerSession.LastShoppingPage = Request.UrlReferrer.AbsoluteUri;
            }

            //helper.ClearCache();
            //helper = GetCartHelper(name);

            var results = new CartJsonModel
            {
                CartSubTotal  = StoreHelper.FormatCurrency(helper.Cart.Subtotal, helper.Cart.BillingCurrency),
                CartTotal     = StoreHelper.FormatCurrency(helper.Cart.Total, helper.Cart.BillingCurrency),
                CartCount     = helper.LineItems.Count(),
                LineItemsView = null,
                CartName      = name
            };

            results.Messages.Add(new MessageModel(string.Format("{0} items added to your {1}".Localize(), addedLineItems.Sum(li => li.Quantity), name.Localize())));

            return(Json(results));
        }