public void MiniCartDetails_WhenCreatingViewModel_ShouldCreateModel()
        {
            var expectedResult = new MiniCartViewModel
            {
                ItemCount    = 6,
                CheckoutPage = new ContentReference(444),
                CartItems    = new List <CartItem>
                {
                    new CartItem
                    {
                        Code          = "code",
                        DiscountPrice = new Money(45, Currency.USD),
                        DisplayName   = "red",
                        ExtendedPrice = new Money(270, Currency.USD),
                        PlacedPrice   = new Money(50, Currency.USD),
                        Url           = "url",
                        Quantity      = 6
                    }
                },
                Total = new Money(270, Currency.USD)
            };
            var result = ((PartialViewResult)_subject.MiniCartDetails()).Model as MiniCartViewModel;

            result.ShouldBeEquivalentTo(expectedResult);
        }
        private MiniCartViewModel GetViewModel()
        {
            var model            = new MiniCartViewModel();
            var quantityProperty = WebSession.Current.GetPersistentProperty(WebSession.ShoppingCartCount);

            if (!String.IsNullOrEmpty(quantityProperty))
            {
                model.Quantity = Convert.ToInt32(quantityProperty);
            }
            else
            {
                var cart = _cartApi.GetCartAsync().Result;
                if (cart != null)
                {
                    model.Quantity = cart.TotalItemsInCart;
                }
                WebSession.Current.SetPersistentProperty(WebSession.ShoppingCartCount,
                                                         model.Quantity.ToString(CultureInfo.InvariantCulture));
            }
            SiteInfo siteInfo;

            if (WebSession.Current.TryGetSiteInfo(out siteInfo) && siteInfo.IsCartHandledBySite)
            {
                model.IsCartHandledBySite = true;
                model.CartLink            = _linkGenerator.GenerateShoppingCartLink();
            }
            else
            {
                model.IsCartHandledBySite = false;
                model.CartLink            = siteInfo.DrShoppingCartUrl;
            }
            return(model);
        }
        public ActionResult MiniCartDetails()
        {
            var _cartService = new CartService();
            var viewModel    = new MiniCartViewModel
            {
                ItemCount = _cartService.GetLineItemsTotalQuantity(),
                CartItems = _cartService.GetCartItems(),
                Total     = _cartService.GetSubTotal()
            };

            return(Json(new { Quantity = viewModel.ItemCount }));
        }
Exemple #4
0
        public ActionResult MiniCartDetails()
        {
            var viewModel = new MiniCartViewModel
            {
                ItemCount    = _cartService.GetLineItemsTotalQuantity(),
                CheckoutPage = _contentLoader.Get <StartPage>(ContentReference.StartPage).CheckoutPage,
                CartItems    = _cartService.GetCartItems(),
                Total        = _cartService.GetSubTotal()
            };

            return(PartialView("_MiniCartDetails", viewModel));
        }
        public void CreateMiniCartViewModel_WhenCartIsNull_ShouldCreateViewModel()
        {
            var viewModel = _subject.CreateMiniCartViewModel(null);

            var expectedViewModel = new MiniCartViewModel
            {
                ItemCount    = 0,
                CheckoutPage = _startPage.CheckoutPage,
                Shipments    = Enumerable.Empty <ShipmentViewModel>(),
                Total        = new Money(0, Currency.USD)
            };

            viewModel.ShouldBeEquivalentTo(expectedViewModel);
        }
        public void CreateMiniCartViewModel_ShouldCreateViewModel()
        {
            var viewModel = _subject.CreateMiniCartViewModel(_cart);

            var expectedViewModel = new MiniCartViewModel
            {
                ItemCount = 1,
                CheckoutPage = _startPage.CheckoutPage,
                Shipments = new[] { new ShipmentViewModel { CartItems = _cartItems } },
                Total = _totals.SubTotal
            };

            viewModel.Should().BeEquivalentTo(expectedViewModel);
        }
        public void CreateMiniCartViewModel_WhenItemIsDeleted_ShouldNotCountIt()
        {
            _cart.Forms.Single().Shipments.Single().LineItems.Add(new InMemoryLineItem { Code = "Deleted", Quantity = 1, PlacedPrice = 100 });
            _referenceConverterMock.Setup(c => c.GetContentLink(It.Is<string>(code => code == "Deleted"))).Returns(ContentReference.EmptyReference);
            var viewModel = _subject.CreateMiniCartViewModel(_cart);

            var expectedViewModel = new MiniCartViewModel
            {
                ItemCount = 1,
                CheckoutPage = _startPage.CheckoutPage,
                Shipments = new[] { new ShipmentViewModel { CartItems = _cartItems } },
                Total = _totals.SubTotal
            };

            viewModel.Should().BeEquivalentTo(expectedViewModel);
        }
Exemple #8
0
        public IHttpActionResult AddItemToCart([Bind(Include = "Code,Quantity")] CodeAndQuantityViewModel codeAndQuantityViewModel)
        {
            var code     = codeAndQuantityViewModel.Code;
            var quantity = codeAndQuantityViewModel.Quantity;
            //Validation

            var variantRef = _referenceConverter.GetContentLink(code);

            if (variantRef == null || variantRef == ContentReference.EmptyReference)
            {
                return(Ok(new { hasError = true, message = "some message" }));
            }

            var cart = _cartRepository.LoadCart(true);

            if (cart == null)
            {
                return(Ok(new { hasError = true, message = "Cart misssing" }));
            }

            try
            {
                cart.AddItemToCart(code, quantity);
                _shoppingCartValidator.ValidateCartItems(cart.ShoppingCart);
                _cartRepository.Save(cart);
            }
            catch (System.Exception)
            {
                return(Ok(new { hasError = true, message = "Item could not be added" }));
            }

            MiniCartViewModel miniCart = new MiniCartViewModel {
                Quantity = quantity
            };

            return(Ok(new { miniCart }));
        }