public JsonResult GetCurrentCart()
        {
            try
            {
                var          id   = CommerceUserContext.Current.UserId;
                var          cart = CartCacheHelper.GetCart(id);
                CartApiModel cartResult;

                // The following condition stops the creation of empty carts on startup.
                if (cart == null && CartCookieHelper.DoesCookieExistForCustomer(id))
                {
                    var response = CartManager.GetCart(CommerceUserContext.Current.UserId, true);
                    cartResult = new CartApiModel(response.ServiceProviderResult);
                    if (response.ServiceProviderResult.Success && response.Result != null)
                    {
                        cartResult.Initialize(response.ServiceProviderResult.Cart);
                        if (response.ServiceProviderResult.Cart != null)
                        {
                            CartCacheHelper.AddCartToCache(response.ServiceProviderResult.Cart as CommerceCart);
                        }
                    }
                }
                else
                {
                    cartResult = new CartApiModel();
                    cartResult.Initialize(cart);
                }

                return(Json(cartResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("GetCurrentCart", e), JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 2
0
        public ManagerResponse <CartResult, CommerceCart> GetCart(string userId, bool refresh = false)
        {
            if (refresh)
            {
                CartCacheHelper.InvalidateCartCache(userId);
            }

            var cart = CartCacheHelper.GetCart(userId);

            if (cart != null)
            {
                var result = new CartResult {
                    Cart = cart
                };
                AddBasketErrorsToResult(result.Cart as CommerceCart, result);
                return(new ManagerResponse <CartResult, CommerceCart>(result, cart));
            }

            //var cartResult = LoadCartByName(CommerceConstants.CartSettings.DefaultCartName, userId, refresh);
            var cartResult = LoadCartByName(userId, userId, refresh);

            if (cartResult.Success && cartResult.Cart != null)
            {
                cart            = cartResult.Cart as CommerceCart;
                cartResult.Cart = cart;
                CartCacheHelper.AddCartToCache(cart);
            }
            else
            {
                var message = DictionaryPhraseRepository.Current.Get("/System Messages/Cart/Cart Not Found Error", "Could not retrieve the cart for the current user");
                cartResult.SystemMessages.Add(new SystemMessage {
                    Message = message
                });
            }

            AddBasketErrorsToResult(cartResult.Cart as CommerceCart, cartResult);

            return(new ManagerResponse <CartResult, CommerceCart>(cartResult, cart));
        }
Ejemplo n.º 3
0
 public CommerceCart GetCart(string id)
 {
     return(_cartCacheHelper.GetCart(id));
 }