Example #1
0
        public IEnumerable <CartItem> GetCartItems()
        {
            if (_cardhelper.IsEmpty)
            {
                return(Enumerable.Empty <CartItem>());
            }

            var cartItems = new List <CartItem>();
            var lineItems = _cardhelper.Cart.GetAllLineItems();

            //// In order to show the images for the items in the cart, we need to load the variants
            var variants = _contentLoader.GetItems(lineItems.Select(x => _referenceConverter.GetContentLink(x.Code)),
                                                   _preferredCulture).OfType <VariationContent>();

            foreach (var lineItem in lineItems)
            {
                VariationContent variant = variants.FirstOrDefault(x => x.Code == lineItem.Code);
                ProductContent   product = _contentLoader.Get <ProductContent>(variant.GetParentProducts().FirstOrDefault());
                CartItem         item    = new CartItem
                {
                    Code        = lineItem.Code,
                    DisplayName = lineItem.DisplayName,
                    ImageUrl    = variant.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "",
                    Quantity    = lineItem.Quantity,
                    Url         = lineItem.GetUrl(),
                    Variant     = variant,
                    Price       = variant.GetPrices().FirstOrDefault().UnitPrice
                };
                cartItems.Add(item);
            }

            return(cartItems);
        }
Example #2
0
        public IEnumerable <CartItem> GetCartItems()
        {
            if (CartHelper.IsEmpty)
            {
                return(Enumerable.Empty <CartItem>());
            }

            var cartItems = new List <CartItem>();
            var lineItems = CartHelper.Cart.GetAllLineItems();

            // In order to show the images for the items in the cart, we need to load the variants
            var variants = _contentLoader.GetItems(lineItems.Select(x => _referenceConverter.GetContentLink(x.Code)),
                                                   _preferredCulture).OfType <VariationContent>();

            var marketId = _currentMarket.GetCurrentMarket().MarketId;
            var currency = _currencyService.GetCurrentCurrency();

            foreach (var lineItem in lineItems)
            {
                VariationContent variant = variants.FirstOrDefault(x => x.Code == lineItem.Code);
                ProductContent   product = _contentLoader.Get <ProductContent>(variant.GetParentProducts().FirstOrDefault());
                CartItem         item    = new CartItem
                {
                    Code          = lineItem.Code,
                    DisplayName   = lineItem.DisplayName,
                    ImageUrl      = variant.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "",
                    ExtendedPrice = GetExtendedPrice(lineItem, marketId, currency),
                    PlacedPrice   = lineItem.ToMoney(lineItem.PlacedPrice),
                    DiscountPrice = lineItem.ToMoney(currency.Round(((lineItem.PlacedPrice * lineItem.Quantity) - lineItem.Discounts.Cast <LineItemDiscount>().Sum(x => x.DiscountValue)) / lineItem.Quantity)),
                    Quantity      = lineItem.Quantity,
                    Url           = lineItem.GetUrl(),
                    Variant       = variant,
                    Discounts     = lineItem.Discounts.Cast <LineItemDiscount>().Select(x => new OrderDiscountModel
                    {
                        Discount    = new Money(x.DiscountAmount, new Currency(CartHelper.Cart.BillingCurrency)),
                        Displayname = x.DisplayMessage
                    }),
                    IsAvailable = _pricingService.GetCurrentPrice(variant.Code).HasValue
                };

                if (product is FashionProduct)
                {
                    var fashionProduct = (FashionProduct)product;
                    var fashionVariant = (FashionVariant)variant;
                    item.Brand = fashionProduct.Brand;
                    var variations = _productService.GetVariations(fashionProduct);
                    item.AvailableSizes = variations.Where(x => x.Color == fashionVariant.Color).Select(x => x.Size);
                }

                cartItems.Add(item);
            }

            return(cartItems);
        }
        /// <summary>
        /// Will get a variation's parent product. If the variant has more than one product, it will get
        /// the first one returned by Commerce.
        /// </summary>
        /// <remarks>
        /// If you want to change how this logic works, please override this method and register your
        /// class for the IClientScriptFactory interface in the service locator.
        /// </remarks>
        /// <param name="entry"></param>
        /// <returns></returns>
        protected virtual IEnumerable <ProductContent> GetParentProductsForVariation(VariationContent entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

            var parentProductLinks = entry.GetParentProducts();

            IEnumerable <ProductContent> products = _contentLoader.GetItems(parentProductLinks, entry.Language).OfType <ProductContent>();

            return(products);
        }
Example #4
0
#pragma warning restore 649

        public static string GetUrl(this VariationContent variant)
        {
            var productLink = variant.GetParentProducts(_relationRepository.Service).FirstOrDefault();

            if (productLink == null)
            {
                return(string.Empty);
            }
            var urlBuilder = new UrlBuilder(_urlResolver.Service.GetUrl(productLink));

            if (variant.Code != null)
            {
                urlBuilder.QueryCollection.Add("variationId", variant.Code);
            }
            return(urlBuilder.ToString());
        }
        public static string GetUrl(this VariationContent variant, ILinksRepository linksRepository, UrlResolver urlResolver)
        {
            var productLink = variant.GetParentProducts(linksRepository).FirstOrDefault();

            if (productLink == null)
            {
                return(string.Empty);
            }

            var urlBuilder = new UrlBuilder(urlResolver.GetUrl(productLink));

            if (variant.Code != null)
            {
                urlBuilder.QueryCollection.Add("variationCode", variant.Code);
            }

            return(urlBuilder.ToString());
        }
        private ProductViewModel CreateProductViewModel(ProductContent product, VariationContent variation)
        {
            if (variation == null)
            {
                return(null);
            }

            ContentReference productContentReference;

            if (product != null)
            {
                productContentReference = product.ContentLink;
            }
            else
            {
                productContentReference = variation.GetParentProducts(_relationRepository).FirstOrDefault();
                if (ContentReference.IsNullOrEmpty(productContentReference))
                {
                    return(null);
                }
            }
            var market   = _currentMarket.GetCurrentMarket();
            var currency = _currencyService.GetCurrentCurrency();

            var originalPrice   = _pricingService.GetCurrentPrice(variation.Code);
            var discountedPrice = originalPrice.HasValue ? GetDiscountPrice(variation, market, currency, originalPrice.Value) : (Money?)null;

            var image = variation.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "";
            var brand = product is FashionProduct ? ((FashionProduct)product).Brand : string.Empty;

            return(new ProductViewModel
            {
                DisplayName = product != null ? product.DisplayName : variation.DisplayName,
                PlacedPrice = originalPrice.HasValue ? originalPrice.Value : new Money(0, currency),
                DiscountedPrice = discountedPrice,
                ImageUrl = image,
                Url = variation.GetUrl(),
                Brand = brand,
                IsAvailable = originalPrice.HasValue
            });
        }
        public virtual CartItemViewModel CreateCartItemViewModel(ICart cart, ILineItem lineItem, VariationContent variant)
        {
            var productLink = variant.GetParentProducts(_relationRepository).FirstOrDefault();
            var product     = _contentLoader.Get <ProductContent>(productLink) as BaseProduct;

            return(new CartItemViewModel
            {
                Code = lineItem.Code,
                DisplayName = variant.DisplayName,
                ImageUrl = variant.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "",
                DiscountedPrice = GetDiscountedPrice(cart, lineItem),
                PlacedPrice = new Money(lineItem.PlacedPrice, _currencyService.GetCurrentCurrency()),
                Quantity = lineItem.Quantity,
                Url = lineItem.GetUrl(),
                Variant = variant,
                IsAvailable = _pricingService.GetCurrentPrice(variant.Code).HasValue,
                Brand = GetBrand(product),
                AvailableSizes = GetAvailableSizes(product, variant),
                DiscountedUnitPrice = GetDiscountedUnitPrice(cart, lineItem),
                IsGift = lineItem.IsGift
            });
        }
        public virtual CartItemViewModel CreateCartItemViewModel(ICart cart, ILineItem lineItem, VariationContent variant)
        {
            var productLink = variant.GetParentProducts(_relationRepository).FirstOrDefault();
            var product = _contentLoader.Get<ProductContent>(productLink) as FashionProduct;

            return new CartItemViewModel
            {
                Code = lineItem.Code,
                DisplayName = variant.DisplayName,
                ImageUrl = variant.GetAssets<IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "",
                DiscountedPrice = GetDiscountedPrice(cart, lineItem),
                PlacedPrice = new Money(lineItem.PlacedPrice, _currencyService.GetCurrentCurrency()),
                Quantity = lineItem.Quantity,
                Url = lineItem.GetUrl(),
                Variant = variant,
                IsAvailable = _pricingService.GetCurrentPrice(variant.Code).HasValue,
                Brand = GetBrand(product),
                AvailableSizes = GetAvailableSizes(product, variant),
                DiscountedUnitPrice = GetDiscountedUnitPrice(cart, lineItem),
                IsGift = lineItem.IsGift
            };
        }
        private LineItem CreateLineItem(VariationContent variation, decimal quantity, decimal price)
        {
            LineItem lineItem = new LineItem();

            lineItem.DisplayName     = variation.DisplayName;
            lineItem.Code            = variation.Code;
            lineItem.MaxQuantity     = variation.MaxQuantity.HasValue ? variation.MaxQuantity.Value : 100;
            lineItem.MinQuantity     = variation.MinQuantity.HasValue ? variation.MinQuantity.Value : 1;
            lineItem.Quantity        = quantity;
            lineItem.WarehouseCode   = string.Empty; // may not know yet
            lineItem.InventoryStatus = variation.TrackInventory ? (int)InventoryStatus.Enabled : (int)InventoryStatus.Disabled;
            lineItem.ListPrice       = price;
            lineItem.PlacedPrice     = price;
            var productLink = variation.GetParentProducts().FirstOrDefault();

            if (productLink != null)
            {
                var product = _contentLoader.Get <ProductContent>(productLink);
                lineItem.ParentCatalogEntryId = product.Code;
            }
            return(lineItem);
        }
Example #10
0
        private ProductViewModel CreateProductViewModel(ProductContent product, VariationContent variation)
        {
            if (variation == null)
            {
                return null;
            }

            ContentReference productContentReference;
            if (product != null)
            {
                productContentReference = product.ContentLink;
            }
            else
            {
                productContentReference = variation.GetParentProducts(_relationRepository).FirstOrDefault();
                if (ContentReference.IsNullOrEmpty(productContentReference))
                {
                    return null;
                }
            }
            var market = _currentMarket.GetCurrentMarket();
            var currency = _currencyService.GetCurrentCurrency();

            var originalPrice = _pricingService.GetCurrentPrice(variation.Code);
            var discountedPrice = originalPrice.HasValue ? GetDiscountPrice(variation, market, currency, originalPrice.Value) : (Money?)null;

            var image = variation.GetAssets<IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "";
            var brand = product is FashionProduct ? ((FashionProduct)product).Brand : string.Empty;

            return new ProductViewModel
            {
                DisplayName = product != null ? product.DisplayName : variation.DisplayName,
                PlacedPrice = originalPrice.HasValue ? originalPrice.Value : new Money(0, currency),
                DiscountedPrice = discountedPrice,
                ImageUrl = image,
                Url = variation.GetUrl(),
                Brand = brand,
                IsAvailable = originalPrice.HasValue
            };
        }