Example #1
0
        public Result <CartModel> AddProductVariantToCart(string productId, string variantId, decimal quantity)
        {
            var model = this.CartManager.GetCurrentCart(this.StorefrontContext.ShopName, this.VisitorContext.ContactId);

            var cartLineArgument = new CartLineArgument
            {
                CatalogName = this.StorefrontContext.CatalogName,
                ProductId   = productId,
                Quantity    = quantity,
                VariantId   = variantId
            };

            var cart = this.CartManager.AddLineItemsToCart(
                model.Result,
                new[]
            {
                cartLineArgument
            },
                null,
                null);

            var result = this.GetCart(cart.ServiceProviderResult, cart.Result);

            return(result);
        }
Example #2
0
        public Result <CartModel> UpdateProductVariantQuantity(string productId, string variantId, decimal quantity)
        {
            var model = this.CartManager.GetCurrentCart(this.StorefrontContext.ShopName, this.VisitorContext.ContactId);

            var cartLine = model.Result.Lines.FirstOrDefault(
                x =>
            {
                var current = x.Product as CommerceCartProduct;
                return((current?.ProductId == productId) && (current?.ProductVariantId == variantId));
            });

            if (cartLine != null)
            {
                if (quantity <= 0)
                {
                    return(this.RemoveProductVariantFromCart(cartLine.ExternalCartLineId));
                }

                var cartLineArgument = new CartLineArgument
                {
                    CatalogName = this.StorefrontContext.CatalogName,
                    ProductId   = productId,
                    Quantity    = quantity,
                    VariantId   = variantId
                };

                var cart = this.CartManager.UpdateLineItemsInCart(
                    model.Result,
                    new[]
                {
                    cartLineArgument
                },
                    null,
                    null);
                var result = this.GetCart(cart.ServiceProviderResult, cart.Result);
                return(result);
            }

            if (quantity > 0)
            {
                return(this.AddProductVariantToCart(productId, variantId, quantity));
            }

            return(null);
        }