public ManufacturerModel()
 {
     PictureModel = new PictureModel();
     FeaturedProducts = new List<ProductOverviewModel>();
     Products = new List<ProductOverviewModel>();
     PagingFilteringContext = new CatalogPagingFilteringModel();
 }
            public ShoppingCartItemModel()
            {
                Picture = new PictureModel();
                AllowedQuantities = new List<SelectListItem>();
                Warnings = new List<string>();
				ChildItems = new List<ShoppingCartItemModel>();
				BundleItem = new BundleItemModel();
            }
 public CategoryModel()
 {
     PictureModel = new PictureModel();
     FeaturedProducts = new List<ProductOverviewModel>();
     Products = new List<ProductOverviewModel>();
     PagingFilteringContext = new CatalogPagingFilteringModel();
     SubCategories = new List<SubCategoryModel>();
     CategoryBreadcrumb = new List<MenuItem>();
 }
 public ProductOverviewModel()
 {
     ProductPrice = new ProductPriceModel();
     DefaultPictureModel = new PictureModel();
     SpecificationAttributeModels = new List<ProductSpecificationModel>();
     Manufacturers = new List<ManufacturerOverviewModel>();
     PagingFilteringContext = new CatalogPagingFilteringModel();
     ColorAttributes = new List<ColorAttributeModel>();
     Weight = "";
     TransportSurcharge = "";
 }
 public ShoppingCartItemModel()
 {
     Picture = new PictureModel();
     BundleItems = new List<ShoppingCartItemBundleItem>();
 }
 public ManufacturerOverviewModel()
 {
     PictureModel = new PictureModel();
 }
Esempio n. 7
0
        public ActionResult HomepageCategories()
        {
            var categories = _categoryService.GetAllCategoriesDisplayedOnHomePage()
                .Where(c => _aclService.Authorize(c) && _storeMappingService.Authorize(c))
                .ToList();

            var listModel = categories
                .Select(x =>
                {
                    var catModel = x.ToModel();

                    //prepare picture model
                    int pictureSize = _mediaSettings.CategoryThumbPictureSize;
                    var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_PICTURE_MODEL_KEY, x.Id, pictureSize, true,
                        _services.WorkContext.WorkingLanguage.Id, _services.WebHelper.IsCurrentConnectionSecured(), _services.StoreContext.CurrentStore.Id);
                    catModel.PictureModel = _services.Cache.Get(categoryPictureCacheKey, () =>
                    {
                        var pictureModel = new PictureModel()
                        {
                            PictureId = x.PictureId.GetValueOrDefault(),
                            FullSizeImageUrl = _pictureService.GetPictureUrl(x.PictureId.GetValueOrDefault()),
                            ImageUrl = _pictureService.GetPictureUrl(x.PictureId.GetValueOrDefault(), pictureSize),
                            Title = string.Format(T("Media.Category.ImageLinkTitleFormat"), catModel.Name),
                            AlternateText = string.Format(T("Media.Category.ImageAlternateTextFormat"), catModel.Name)
                        };
                        return pictureModel;
                    });

                    return catModel;
                })
                .ToList();

            if (listModel.Count == 0)
                return Content("");

            return PartialView(listModel);
        }
Esempio n. 8
0
        public ActionResult Category(int categoryId, CatalogPagingFilteringModel command, string filter)
        {
            var category = _categoryService.GetCategoryById(categoryId);
            if (category == null || category.Deleted)
                return HttpNotFound();

            //Check whether the current user has a "Manage catalog" permission
            //It allows him to preview a category before publishing
            if (!category.Published && !_services.Permissions.Authorize(StandardPermissionProvider.ManageCatalog))
                return HttpNotFound();

            //ACL (access control list)
            if (!_aclService.Authorize(category))
                return HttpNotFound();

            //Store mapping
            if (!_storeMappingService.Authorize(category))
                return HttpNotFound();

            //'Continue shopping' URL
            _genericAttributeService.SaveAttribute(_services.WorkContext.CurrentCustomer,
                SystemCustomerAttributeNames.LastContinueShoppingPage,
                _services.WebHelper.GetThisPageUrl(false),
                _services.StoreContext.CurrentStore.Id);

            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            if (command.ViewMode.IsEmpty() && category.DefaultViewMode.HasValue())
            {
                command.ViewMode = category.DefaultViewMode;
            }

            if (command.OrderBy == (int)ProductSortingEnum.Initial)
            {
                command.OrderBy = (int)_catalogSettings.DefaultSortOrder;
            }

            var model = category.ToModel();

            _helper.PreparePagingFilteringModel(model.PagingFilteringContext, command, new PageSizeContext
            {
                AllowCustomersToSelectPageSize = category.AllowCustomersToSelectPageSize,
                PageSize = category.PageSize,
                PageSizeOptions = category.PageSizeOptions
            });

            //price ranges
            model.PagingFilteringContext.PriceRangeFilter.LoadPriceRangeFilters(category.PriceRanges, _services.WebHelper, _priceFormatter);
            var selectedPriceRange = model.PagingFilteringContext.PriceRangeFilter.GetSelectedPriceRange(_services.WebHelper, category.PriceRanges);
            decimal? minPriceConverted = null;
            decimal? maxPriceConverted = null;

            if (selectedPriceRange != null)
            {
                if (selectedPriceRange.From.HasValue)
                    minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(selectedPriceRange.From.Value, _services.WorkContext.WorkingCurrency);

                if (selectedPriceRange.To.HasValue)
                    maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(selectedPriceRange.To.Value, _services.WorkContext.WorkingCurrency);
            }

            //category breadcrumb
            model.DisplayCategoryBreadcrumb = _catalogSettings.CategoryBreadcrumbEnabled;
            if (model.DisplayCategoryBreadcrumb)
            {
                model.CategoryBreadcrumb = _helper.GetCategoryBreadCrumb(category.Id, 0);
            }

            model.DisplayFilter = _catalogSettings.FilterEnabled;
            model.SubCategoryDisplayType = _catalogSettings.SubCategoryDisplayType;

            var customerRolesIds = _services.WorkContext.CurrentCustomer.CustomerRoles.Where(x => x.Active).Select(x => x.Id).ToList();

            // subcategories
            model.SubCategories = _categoryService
                .GetAllCategoriesByParentCategoryId(categoryId)
                .Select(x =>
                {
                    var subCatName = x.GetLocalized(y => y.Name);
                    var subCatModel = new CategoryModel.SubCategoryModel
                    {
                        Id = x.Id,
                        Name = subCatName,
                        SeName = x.GetSeName(),
                    };

                    //prepare picture model
                    int pictureSize = _mediaSettings.CategoryThumbPictureSize;
                    var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_PICTURE_MODEL_KEY, x.Id, pictureSize, true, _services.WorkContext.WorkingLanguage.Id, _services.WebHelper.IsCurrentConnectionSecured(), _services.StoreContext.CurrentStore.Id);
                    subCatModel.PictureModel = _services.Cache.Get(categoryPictureCacheKey, () =>
                    {
                        var picture = _pictureService.GetPictureById(x.PictureId.GetValueOrDefault());
                        var pictureModel = new PictureModel()
                        {
                            PictureId = x.PictureId.GetValueOrDefault(),
                            FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                            ImageUrl = _pictureService.GetPictureUrl(picture, targetSize: pictureSize),
                            Title = string.Format(T("Media.Category.ImageLinkTitleFormat"), subCatName),
                            AlternateText = string.Format(T("Media.Category.ImageAlternateTextFormat"), subCatName)
                        };
                        return pictureModel;
                    });

                    return subCatModel;
                })
                .ToList();

            // Featured products
            if (!_catalogSettings.IgnoreFeaturedProducts)
            {
                IPagedList<Product> featuredProducts = null;

                string cacheKey = ModelCacheEventConsumer.CATEGORY_HAS_FEATURED_PRODUCTS_KEY.FormatInvariant(categoryId, string.Join(",", customerRolesIds), _services.StoreContext.CurrentStore.Id);
                var hasFeaturedProductsCache = _services.Cache.Get<bool?>(cacheKey);

                var ctx = new ProductSearchContext();
                if (category.Id > 0)
                    ctx.CategoryIds.Add(category.Id);
                ctx.FeaturedProducts = true;
                ctx.LanguageId = _services.WorkContext.WorkingLanguage.Id;
                ctx.OrderBy = ProductSortingEnum.Position;
                ctx.PageSize = int.MaxValue;
                ctx.StoreId = _services.StoreContext.CurrentStoreIdIfMultiStoreMode;
                ctx.VisibleIndividuallyOnly = true;
                ctx.Origin = categoryId.ToString();

                if (!hasFeaturedProductsCache.HasValue)
                {
                    featuredProducts = _productService.SearchProducts(ctx);
                    hasFeaturedProductsCache = featuredProducts.TotalCount > 0;
                    _services.Cache.Set(cacheKey, hasFeaturedProductsCache, 240);
                }

                if (hasFeaturedProductsCache.Value && featuredProducts == null)
                {
                    featuredProducts = _productService.SearchProducts(ctx);
                }

                if (featuredProducts != null)
                {
                    model.FeaturedProducts = _helper.PrepareProductOverviewModels(
                        featuredProducts,
                        prepareColorAttributes: true).ToList();
                }
            }

            // Products
            if (filter.HasValue())
            {
                var context = new FilterProductContext
                {
                    ParentCategoryID = category.Id,
                    CategoryIds = new List<int> { category.Id },
                    Criteria = _filterService.Deserialize(filter),
                    OrderBy = command.OrderBy
                };

                if (_catalogSettings.ShowProductsFromSubcategories)
                    context.CategoryIds.AddRange(_helper.GetChildCategoryIds(category.Id));

                var filterQuery = _filterService.ProductFilter(context);
                var products = new PagedList<Product>(filterQuery, command.PageIndex, command.PageSize);

                model.Products = _helper.PrepareProductOverviewModels(
                    products,
                    prepareColorAttributes: true,
                    prepareManufacturers: command.ViewMode.IsCaseInsensitiveEqual("list")).ToList();
                model.PagingFilteringContext.LoadPagedList(products);
            }
            else
            {	// use old filter
                IList<int> alreadyFilteredSpecOptionIds = model.PagingFilteringContext.SpecificationFilter.GetAlreadyFilteredSpecOptionIds(_services.WebHelper);

                var ctx2 = new ProductSearchContext();
                if (category.Id > 0)
                {
                    ctx2.CategoryIds.Add(category.Id);
                    if (_catalogSettings.ShowProductsFromSubcategories)
                    {
                        // include subcategories
                        ctx2.CategoryIds.AddRange(_helper.GetChildCategoryIds(category.Id));
                    }
                }
                ctx2.FeaturedProducts = _catalogSettings.IncludeFeaturedProductsInNormalLists ? null : (bool?)false;
                ctx2.PriceMin = minPriceConverted;
                ctx2.PriceMax = maxPriceConverted;
                ctx2.LanguageId = _services.WorkContext.WorkingLanguage.Id;
                ctx2.FilteredSpecs = alreadyFilteredSpecOptionIds;
                ctx2.OrderBy = (ProductSortingEnum)command.OrderBy; // ProductSortingEnum.Position;
                ctx2.PageIndex = command.PageNumber - 1;
                ctx2.PageSize = command.PageSize;
                ctx2.LoadFilterableSpecificationAttributeOptionIds = true;
                ctx2.StoreId = _services.StoreContext.CurrentStoreIdIfMultiStoreMode;
                ctx2.VisibleIndividuallyOnly = true;
                ctx2.Origin = categoryId.ToString();

                var products = _productService.SearchProducts(ctx2);

                model.Products = _helper.PrepareProductOverviewModels(
                    products,
                    prepareColorAttributes: true,
                    prepareManufacturers: command.ViewMode.IsCaseInsensitiveEqual("list")).ToList();

                model.PagingFilteringContext.LoadPagedList(products);
                //model.PagingFilteringContext.ViewMode = viewMode;

                //specs
                model.PagingFilteringContext.SpecificationFilter.PrepareSpecsFilters(alreadyFilteredSpecOptionIds,
                    ctx2.FilterableSpecificationAttributeOptionIds,
                    _specificationAttributeService, _services.WebHelper, _services.WorkContext);
            }

            // template
            var templateCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_TEMPLATE_MODEL_KEY, category.CategoryTemplateId);
            var templateViewPath = _services.Cache.Get(templateCacheKey, () =>
            {
                var template = _categoryTemplateService.GetCategoryTemplateById(category.CategoryTemplateId);
                if (template == null)
                    template = _categoryTemplateService.GetAllCategoryTemplates().FirstOrDefault();
                return template.ViewPath;
            });

            // activity log
            _services.CustomerActivity.InsertActivity("PublicStore.ViewCategory", T("ActivityLog.PublicStore.ViewCategory"), category.Name);

            return View(templateViewPath, model);
        }
 public SubCategoryModel()
 {
     PictureModel = new PictureModel();
 }
        public PictureModel PrepareManufacturerPictureModel(Manufacturer manufacturer, string localizedName)
        {
            var model = new PictureModel();

            var pictureSize = _mediaSettings.ManufacturerThumbPictureSize;
            var manufacturerPictureCacheKey = string.Format(ModelCacheEventConsumer.MANUFACTURER_PICTURE_MODEL_KEY,
                manufacturer.Id,
                pictureSize,
                !_catalogSettings.HideManufacturerDefaultPictures,
                _services.WorkContext.WorkingLanguage.Id,
                _services.WebHelper.IsCurrentConnectionSecured(),
                _services.StoreContext.CurrentStore.Id);

            model = _services.Cache.Get(manufacturerPictureCacheKey, () =>
            {
                var pictureModel = new PictureModel
                {
                    PictureId = manufacturer.PictureId.GetValueOrDefault(),
                    //FullSizeImageUrl = _pictureService.GetPictureUrl(manufacturer.PictureId.GetValueOrDefault()),
                    ImageUrl = _pictureService.GetPictureUrl(manufacturer.PictureId.GetValueOrDefault(), pictureSize, !_catalogSettings.HideManufacturerDefaultPictures),
                    Title = string.Format(T("Media.Manufacturer.ImageLinkTitleFormat"), localizedName),
                    AlternateText = string.Format(T("Media.Manufacturer.ImageAlternateTextFormat"), localizedName)
                };
                return pictureModel;
            });

            return model;
        }
        private PictureModel CreatePictureModel(ProductDetailsPictureModel model, Picture picture, int pictureSize)
        {
            var result = new PictureModel
            {
                PictureId = picture.Id,
                ThumbImageUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage),
                ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize, !_catalogSettings.HideProductDefaultPictures),
                FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                Title = model.Name,
                AlternateText = model.AlternateText
            };

            return result;
        }
        public IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(
			IEnumerable<Product> products,
			bool preparePriceModel = true, 
			bool preparePictureModel = true,
			int? productThumbPictureSize = null, 
			bool prepareSpecificationAttributes = false,
			bool forceRedirectionAfterAddingToCart = false, 
			bool prepareColorAttributes = false,
			bool prepareManufacturers = false,
            bool isCompact = false,
			bool prepareFullDescription = false,
			bool isCompareList = false)
        {
            if (products == null)
                throw new ArgumentNullException("products");

            // PERF!!
            var currentStore = _services.StoreContext.CurrentStore;
            var currentCustomer = _services.WorkContext.CurrentCustomer;
            var workingCurrency = _services.WorkContext.WorkingCurrency;
            var displayPrices = _services.Permissions.Authorize(StandardPermissionProvider.DisplayPrices);
            var enableShoppingCart = _services.Permissions.Authorize(StandardPermissionProvider.EnableShoppingCart);
            var enableWishlist = _services.Permissions.Authorize(StandardPermissionProvider.EnableWishlist);
            var taxDisplayType = _services.WorkContext.GetTaxDisplayTypeFor(currentCustomer, currentStore.Id);
            var cachedManufacturerModels = new Dictionary<int, ManufacturerOverviewModel>();

            string taxInfo = T(taxDisplayType == TaxDisplayType.IncludingTax ? "Tax.InclVAT" : "Tax.ExclVAT");
            var legalInfo = "";

            var res = new Dictionary<string, LocalizedString>(StringComparer.OrdinalIgnoreCase)
            {
                { "Products.CallForPrice", T("Products.CallForPrice") },
                { "Products.PriceRangeFrom", T("Products.PriceRangeFrom") },
                { "Media.Product.ImageLinkTitleFormat", T("Media.Product.ImageLinkTitleFormat") },
                { "Media.Product.ImageAlternateTextFormat", T("Media.Product.ImageAlternateTextFormat") },
                { "Products.DimensionsValue", T("Products.DimensionsValue") },
                { "Common.AdditionalShippingSurcharge", T("Common.AdditionalShippingSurcharge") }
            };

            if (_taxSettings.ShowLegalHintsInProductList)
            {
                if (_topicService.Value.GetTopicBySystemName("ShippingInfo", currentStore.Id) == null)
                {
                    legalInfo = T("Tax.LegalInfoFooter2").Text.FormatInvariant(taxInfo);
                }
                else
                {
                    var shippingInfoLink = _urlHelper.RouteUrl("Topic", new { SystemName = "shippinginfo" });
                    legalInfo = T("Tax.LegalInfoFooter").Text.FormatInvariant(taxInfo, shippingInfoLink);
                }
            }

            var cargoData = _priceCalculationService.CreatePriceCalculationContext(products);

            var models = new List<ProductOverviewModel>();

            foreach (var product in products)
            {
                var contextProduct = product;
                var finalPrice = decimal.Zero;

                var model = new ProductOverviewModel
                {
                    Id = product.Id,
                    Name = product.GetLocalized(x => x.Name).EmptyNull(),
                    ShortDescription = product.GetLocalized(x => x.ShortDescription),
                    SeName = product.GetSeName()
                };

                if (prepareFullDescription)
                {
                    model.FullDescription = product.GetLocalized(x => x.FullDescription);
                }

                // price
                if (preparePriceModel)
                {
                    #region Prepare product price

                    var priceModel = new ProductOverviewModel.ProductPriceModel
                    {
                        ForceRedirectionAfterAddingToCart = forceRedirectionAfterAddingToCart,
                        ShowDiscountSign = _catalogSettings.ShowDiscountSign
                    };

                    if (product.ProductType == ProductType.GroupedProduct)
                    {
                        #region Grouped product

                        priceModel.DisableBuyButton = true;
                        priceModel.DisableWishListButton = true;
                        priceModel.AvailableForPreOrder = false;

                        var searchContext = new ProductSearchContext
                        {
                            OrderBy = ProductSortingEnum.Position,
                            StoreId = currentStore.Id,
                            ParentGroupedProductId = product.Id,
                            PageSize = int.MaxValue,
                            VisibleIndividuallyOnly = false
                        };

                        var associatedProducts = _productService.SearchProducts(searchContext);

                        if (associatedProducts.Count > 0)
                        {
                            contextProduct = associatedProducts.OrderBy(x => x.DisplayOrder).First();

                            if (displayPrices && _catalogSettings.PriceDisplayType != PriceDisplayType.Hide)
                            {
                                decimal? displayPrice = null;
                                bool displayFromMessage = false;

                                if (_catalogSettings.PriceDisplayType == PriceDisplayType.PreSelectedPrice)
                                {
                                    displayPrice = _priceCalculationService.GetPreselectedPrice(contextProduct, cargoData);
                                }
                                else if (_catalogSettings.PriceDisplayType == PriceDisplayType.PriceWithoutDiscountsAndAttributes)
                                {
                                    displayPrice = _priceCalculationService.GetFinalPrice(contextProduct, null, currentCustomer, decimal.Zero, false, 1, null, cargoData);
                                }
                                else
                                {
                                    displayFromMessage = true;
                                    displayPrice = _priceCalculationService.GetLowestPrice(product, cargoData, associatedProducts, out contextProduct);
                                }

                                if (contextProduct != null && !contextProduct.CustomerEntersPrice)
                                {
                                    if (contextProduct.CallForPrice)
                                    {
                                        priceModel.OldPrice = null;
                                        priceModel.Price = res["Products.CallForPrice"];
                                    }
                                    else if (displayPrice.HasValue)
                                    {
                                        //calculate prices
                                        decimal taxRate = decimal.Zero;
                                        decimal oldPriceBase = _taxService.GetProductPrice(contextProduct, contextProduct.OldPrice, out taxRate);
                                        decimal finalPriceBase = _taxService.GetProductPrice(contextProduct, displayPrice.Value, out taxRate);
                                        finalPrice = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceBase, workingCurrency);

                                        priceModel.OldPrice = null;

                                        if (displayFromMessage)
                                            priceModel.Price = String.Format(res["Products.PriceRangeFrom"], _priceFormatter.FormatPrice(finalPrice));
                                        else
                                            priceModel.Price = _priceFormatter.FormatPrice(finalPrice);

                                        priceModel.HasDiscount = (finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero);
                                    }
                                    else
                                    {
                                        // Actually it's not possible (we presume that displayPrice always has a value). We never should get here
                                        Debug.WriteLine(string.Format("Cannot calculate displayPrice for product #{0}", product.Id));
                                    }
                                }
                            }
                        }

                        #endregion
                    }
                    else
                    {
                        #region Simple product

                        //add to cart button
                        priceModel.DisableBuyButton = product.DisableBuyButton || !enableShoppingCart || !displayPrices;

                        //add to wishlist button
                        priceModel.DisableWishListButton = product.DisableWishlistButton || !enableWishlist || !displayPrices;

                        //pre-order
                        priceModel.AvailableForPreOrder = product.AvailableForPreOrder;

                        //prices
                        if (displayPrices && _catalogSettings.PriceDisplayType != PriceDisplayType.Hide && !product.CustomerEntersPrice)
                        {
                            if (product.CallForPrice)
                            {
                                //call for price
                                priceModel.OldPrice = null;
                                priceModel.Price = res["Products.CallForPrice"];
                            }
                            else
                            {
                                //calculate prices
                                bool displayFromMessage = false;
                                decimal displayPrice = decimal.Zero;

                                if (_catalogSettings.PriceDisplayType == PriceDisplayType.PreSelectedPrice)
                                {
                                    displayPrice = _priceCalculationService.GetPreselectedPrice(product, cargoData);
                                }
                                else if (_catalogSettings.PriceDisplayType == PriceDisplayType.PriceWithoutDiscountsAndAttributes)
                                {
                                    displayPrice = _priceCalculationService.GetFinalPrice(product, null, currentCustomer, decimal.Zero, false, 1, null, cargoData);
                                }
                                else
                                {
                                    displayPrice = _priceCalculationService.GetLowestPrice(product, cargoData, out displayFromMessage);
                                }

                                decimal taxRate = decimal.Zero;
                                decimal oldPriceBase = _taxService.GetProductPrice(product, product.OldPrice, out taxRate);
                                decimal finalPriceBase = _taxService.GetProductPrice(product, displayPrice, out taxRate);

                                decimal oldPrice = _currencyService.ConvertFromPrimaryStoreCurrency(oldPriceBase, workingCurrency);
                                finalPrice = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceBase, workingCurrency);

                                priceModel.HasDiscount = (finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero);

                                if (displayFromMessage)
                                {
                                    priceModel.OldPrice = null;
                                    priceModel.Price = String.Format(res["Products.PriceRangeFrom"], _priceFormatter.FormatPrice(finalPrice));
                                }
                                else
                                {
                                    if (priceModel.HasDiscount)
                                    {
                                        priceModel.OldPrice = _priceFormatter.FormatPrice(oldPrice);
                                        priceModel.Price = _priceFormatter.FormatPrice(finalPrice);
                                    }
                                    else
                                    {
                                        priceModel.OldPrice = null;
                                        priceModel.Price = _priceFormatter.FormatPrice(finalPrice);
                                    }
                                }
                            }
                        }

                        #endregion
                    }

                    model.ProductPrice = priceModel;
                    model.ProductPrice.CallForPrice = product.CallForPrice;

                    #endregion
                }

                // color squares
                if (prepareColorAttributes && _catalogSettings.ShowColorSquaresInLists)
                {
                    #region Prepare color attributes

                    var attributes = cargoData.Attributes.Load(contextProduct.Id);
                    var colorAttribute = attributes.FirstOrDefault(x => x.AttributeControlType == AttributeControlType.ColorSquares);

                    if (colorAttribute != null)
                    {
                        var colorValues =
                            from a in colorAttribute.ProductVariantAttributeValues.Take(50)
                            where (a.ColorSquaresRgb.HasValue() && !a.ColorSquaresRgb.IsCaseInsensitiveEqual("transparent"))
                            select new ProductOverviewModel.ColorAttributeModel
                            {
                                Color = a.ColorSquaresRgb,
                                Alias = a.Alias,
                                FriendlyName = a.GetLocalized(l => l.Name)
                            };

                        if (colorValues.Any())
                        {
                            model.ColorAttributes.AddRange(colorValues.Distinct());
                        }
                    }

                    #endregion
                }

                // picture
                if (preparePictureModel)
                {
                    #region Prepare product picture

                    //If a size has been set in the view, we use it in priority
                    int pictureSize = productThumbPictureSize.HasValue ? productThumbPictureSize.Value : _mediaSettings.ProductThumbPictureSize;

                    //prepare picture model
                    var defaultProductPictureCacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_DEFAULTPICTURE_MODEL_KEY, product.Id, pictureSize, true,
                        _services.WorkContext.WorkingLanguage.Id, _services.WebHelper.IsCurrentConnectionSecured(), currentStore.Id);

                    model.DefaultPictureModel = _services.Cache.Get(defaultProductPictureCacheKey, () =>
                    {
                        var picture = product.GetDefaultProductPicture(_pictureService);
                        var pictureModel = new PictureModel
                        {
                            ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize, !_catalogSettings.HideProductDefaultPictures),
                            FullSizeImageUrl = _pictureService.GetPictureUrl(picture, 0, !_catalogSettings.HideProductDefaultPictures),
                            Title = string.Format(res["Media.Product.ImageLinkTitleFormat"], model.Name),
                            AlternateText = string.Format(res["Media.Product.ImageAlternateTextFormat"], model.Name)
                        };
                        return pictureModel;
                    });

                    #endregion
                }

                // specs
                if (prepareSpecificationAttributes)
                {
                    model.SpecificationAttributeModels = PrepareProductSpecificationModel(product);
                }

                model.MinPriceProductId = contextProduct.Id;
                model.ShowSku = _catalogSettings.ShowProductSku;
                model.ShowWeight = _catalogSettings.ShowWeight;
                model.ShowDimensions = _catalogSettings.ShowDimensions;
                model.Sku = contextProduct.Sku;
                model.Dimensions = res["Products.DimensionsValue"].Text.FormatCurrent(
                    contextProduct.Width.ToString("F2"),
                    contextProduct.Height.ToString("F2"),
                    contextProduct.Length.ToString("F2")
                );
                model.DimensionMeasureUnit = _measureService.GetMeasureDimensionById(_measureSettings.BaseDimensionId).Name;
                model.ThumbDimension = _mediaSettings.ProductThumbPictureSize;
                model.ShowLegalInfo = _taxSettings.ShowLegalHintsInProductList;
                model.LegalInfo = legalInfo;
                model.RatingSum = product.ApprovedRatingSum;
                model.TotalReviews = product.ApprovedTotalReviews;
                model.ShowReviews = _catalogSettings.ShowProductReviewsInProductLists;
                model.ShowDeliveryTimes = _catalogSettings.ShowDeliveryTimesInProductLists;
                model.InvisibleDeliveryTime = (product.ProductType == ProductType.GroupedProduct);

                model.IsShipEnabled = contextProduct.IsShipEnabled;
                model.DisplayDeliveryTimeAccordingToStock = contextProduct.DisplayDeliveryTimeAccordingToStock(_catalogSettings);
                model.StockAvailablity = contextProduct.FormatStockMessage(_localizationService);

                model.DisplayBasePrice = _catalogSettings.ShowBasePriceInProductLists;
                model.CompareEnabled = _catalogSettings.CompareProductsEnabled;
                model.HideBuyButtonInLists = _catalogSettings.HideBuyButtonInLists;

                if (model.ShowDeliveryTimes)
                {
                    var deliveryTime = _deliveryTimeService.GetDeliveryTime(contextProduct);
                    if (deliveryTime != null)
                    {
                        model.DeliveryTimeName = deliveryTime.GetLocalized(x => x.Name);
                        model.DeliveryTimeHexValue = deliveryTime.ColorHexValue;
                    }
                }

                if (prepareManufacturers)
                {
                    model.Manufacturers = PrepareManufacturersOverviewModel(_manufacturerService.GetProductManufacturersByProductId(product.Id), cachedManufacturerModels, false);
                }

                if (finalPrice != decimal.Zero && (_catalogSettings.ShowBasePriceInProductLists || isCompareList))
                {
                    model.BasePriceInfo = contextProduct.GetBasePriceInfo(finalPrice, _localizationService,	_priceFormatter, workingCurrency);
                }

                if (displayPrices)
                {
                    var addShippingPrice = _currencyService.ConvertCurrency(contextProduct.AdditionalShippingCharge, currentStore.PrimaryStoreCurrency, workingCurrency);

                    if (addShippingPrice > 0)
                    {
                        model.TransportSurcharge = res["Common.AdditionalShippingSurcharge"].Text.FormatCurrent(_priceFormatter.FormatPrice(addShippingPrice, true, false));
                    }
                }

                if (contextProduct.Weight > 0)
                {
                    model.Weight = "{0} {1}".FormatCurrent(contextProduct.Weight.ToString("F2"), _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId).Name);
                }

                // IsNew
                if (_catalogSettings.LabelAsNewForMaxDays.HasValue)
                {
                    model.IsNew = ((DateTime.UtcNow - product.CreatedOnUtc).Days <= _catalogSettings.LabelAsNewForMaxDays.Value);
                }

                models.Add(model);
            }

            return models;
        }
Esempio n. 13
0
        public IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products,
			bool preparePriceModel = true, bool preparePictureModel = true,
			int? productThumbPictureSize = null, bool prepareSpecificationAttributes = false,
			bool forceRedirectionAfterAddingToCart = false, bool prepareColorAttributes = false)
        {
            if (products == null)
                throw new ArgumentNullException("products");

            var models = new List<ProductOverviewModel>();

            foreach (var product in products)
            {
                var minPriceProduct = product;

                var model = new ProductOverviewModel()
                {
                    Id = product.Id,
                    Name = product.GetLocalized(x => x.Name).EmptyNull(),
                    ShortDescription = product.GetLocalized(x => x.ShortDescription),
                    FullDescription = product.GetLocalized(x => x.FullDescription),
                    SeName = product.GetSeName()
                };

                // price
                if (preparePriceModel)
                {
                    #region Prepare product price

                    var priceModel = new ProductOverviewModel.ProductPriceModel()
                    {
                        ForceRedirectionAfterAddingToCart = forceRedirectionAfterAddingToCart,
                        ShowDiscountSign = _catalogSettings.ShowDiscountSign
                    };

                    switch (product.ProductType)
                    {
                        case ProductType.GroupedProduct:
                            {
                                #region Grouped product

                                var searchContext = new ProductSearchContext()
                                {
                                    StoreId = _services.StoreContext.CurrentStore.Id,
                                    ParentGroupedProductId = product.Id,
                                    PageSize = int.MaxValue,
                                    VisibleIndividuallyOnly = false
                                };

                                var associatedProducts = _productService.SearchProducts(searchContext);

                                if (associatedProducts.Count <= 0)
                                {
                                    priceModel.OldPrice = null;
                                    priceModel.Price = null;
                                    priceModel.DisableBuyButton = true;
                                    priceModel.DisableWishListButton = true;
                                    priceModel.AvailableForPreOrder = false;
                                }
                                else
                                {
                                    priceModel.DisableBuyButton = true;
                                    priceModel.DisableWishListButton = true;
                                    priceModel.AvailableForPreOrder = false;

                                    if (_services.Permissions.Authorize(StandardPermissionProvider.DisplayPrices))
                                    {
                                        decimal? minPossiblePrice = _priceCalculationService.GetLowestPrice(product, associatedProducts, out minPriceProduct);

                                        if (minPriceProduct != null && !minPriceProduct.CustomerEntersPrice)
                                        {
                                            if (minPriceProduct.CallForPrice)
                                            {
                                                priceModel.OldPrice = null;
                                                priceModel.Price = T("Products.CallForPrice");
                                            }
                                            else if (minPossiblePrice.HasValue)
                                            {
                                                //calculate prices
                                                decimal taxRate = decimal.Zero;
                                                decimal oldPriceBase = _taxService.GetProductPrice(minPriceProduct, minPriceProduct.OldPrice, out taxRate);
                                                decimal finalPriceBase = _taxService.GetProductPrice(minPriceProduct, minPossiblePrice.Value, out taxRate);
                                                decimal finalPrice = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceBase, _services.WorkContext.WorkingCurrency);

                                                priceModel.OldPrice = null;
                                                priceModel.Price = String.Format(T("Products.PriceRangeFrom"), _priceFormatter.FormatPrice(finalPrice));
                                                priceModel.HasDiscount = finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero;
                                            }
                                            else
                                            {
                                                //Actually it's not possible (we presume that minimalPrice always has a value)
                                                //We never should get here
                                                Debug.WriteLine(string.Format("Cannot calculate minPrice for product #{0}", product.Id));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //hide prices
                                        priceModel.OldPrice = null;
                                        priceModel.Price = null;
                                    }
                                }

                                #endregion
                            }
                            break;
                        case ProductType.SimpleProduct:
                        default:
                            {
                                #region Simple product

                                //add to cart button
                                priceModel.DisableBuyButton = product.DisableBuyButton ||
                                    !_services.Permissions.Authorize(StandardPermissionProvider.EnableShoppingCart) ||
                                    !_services.Permissions.Authorize(StandardPermissionProvider.DisplayPrices);

                                //add to wishlist button
                                priceModel.DisableWishListButton = product.DisableWishlistButton ||
                                    !_services.Permissions.Authorize(StandardPermissionProvider.EnableWishlist) ||
                                    !_services.Permissions.Authorize(StandardPermissionProvider.DisplayPrices);

                                //pre-order
                                priceModel.AvailableForPreOrder = product.AvailableForPreOrder;

                                //prices
                                if (_services.Permissions.Authorize(StandardPermissionProvider.DisplayPrices))
                                {
                                    if (!product.CustomerEntersPrice)
                                    {
                                        if (product.CallForPrice)
                                        {
                                            //call for price
                                            priceModel.OldPrice = null;
                                            priceModel.Price = T("Products.CallForPrice");
                                        }
                                        else
                                        {
                                            //calculate prices
                                            bool isBundlePerItemPricing = (product.ProductType == ProductType.BundledProduct && product.BundlePerItemPricing);

                                            bool displayFromMessage = false;
                                            decimal minPossiblePrice = _priceCalculationService.GetLowestPrice(product, out displayFromMessage);

                                            decimal taxRate = decimal.Zero;
                                            decimal oldPriceBase = _taxService.GetProductPrice(product, product.OldPrice, out taxRate);
                                            decimal finalPriceBase = _taxService.GetProductPrice(product, minPossiblePrice, out taxRate);

                                            decimal oldPrice = _currencyService.ConvertFromPrimaryStoreCurrency(oldPriceBase, _services.WorkContext.WorkingCurrency);
                                            decimal finalPrice = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceBase, _services.WorkContext.WorkingCurrency);

                                            priceModel.HasDiscount = (finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero);

                                            // check tier prices
                                            if (product.HasTierPrices && !isBundlePerItemPricing && !displayFromMessage)
                                            {
                                                var tierPrices = new List<TierPrice>();

                                                tierPrices.AddRange(product.TierPrices
                                                    .OrderBy(tp => tp.Quantity)
                                                    .FilterByStore(_services.StoreContext.CurrentStore.Id)
                                                    .FilterForCustomer(_services.WorkContext.CurrentCustomer)
                                                    .ToList()
                                                    .RemoveDuplicatedQuantities());

                                                // When there is just one tier (with  qty 1), there are no actual savings in the list.
                                                displayFromMessage = (tierPrices.Count > 0 && !(tierPrices.Count == 1 && tierPrices[0].Quantity <= 1));
                                            }

                                            if (displayFromMessage)
                                            {
                                                priceModel.OldPrice = null;
                                                priceModel.Price = String.Format(T("Products.PriceRangeFrom"), _priceFormatter.FormatPrice(finalPrice));
                                            }
                                            else
                                            {
                                                if (priceModel.HasDiscount)
                                                {
                                                    priceModel.OldPrice = _priceFormatter.FormatPrice(oldPrice);
                                                    priceModel.Price = _priceFormatter.FormatPrice(finalPrice);
                                                }
                                                else
                                                {
                                                    priceModel.OldPrice = null;
                                                    priceModel.Price = _priceFormatter.FormatPrice(finalPrice);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //hide prices
                                    priceModel.OldPrice = null;
                                    priceModel.Price = null;
                                }

                                #endregion
                            }
                            break;
                    }

                    model.ProductPrice = priceModel;
                    model.ProductPrice.CallForPrice = product.CallForPrice;

                    #endregion
                }

                // picture
                if (preparePictureModel)
                {
                    #region Prepare product picture

                    //If a size has been set in the view, we use it in priority
                    int pictureSize = productThumbPictureSize.HasValue ? productThumbPictureSize.Value : _mediaSettings.ProductThumbPictureSize;

                    //prepare picture model
                    var defaultProductPictureCacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_DEFAULTPICTURE_MODEL_KEY, product.Id, pictureSize, true,
                        _services.WorkContext.WorkingLanguage.Id, _services.WebHelper.IsCurrentConnectionSecured(), _services.StoreContext.CurrentStore.Id);

                    model.DefaultPictureModel = _services.Cache.Get(defaultProductPictureCacheKey, () =>
                    {
                        var picture = product.GetDefaultProductPicture(_pictureService);
                        var pictureModel = new PictureModel
                        {
                            ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize),
                            FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                            Title = string.Format(T("Media.Product.ImageLinkTitleFormat"), model.Name),
                            AlternateText = string.Format(T("Media.Product.ImageAlternateTextFormat"), model.Name)
                        };
                        return pictureModel;
                    });

                    #endregion
                }

                // specs
                if (prepareSpecificationAttributes)
                {
                    model.SpecificationAttributeModels = PrepareProductSpecificationModel(product);
                }

                // available colors
                if (prepareColorAttributes && _catalogSettings.ShowColorSquaresInLists)
                {
                    #region Prepare color attributes

                    // get the FIRST color type attribute
                    var colorAttr = _productAttributeService.GetProductVariantAttributesByProductId(minPriceProduct.Id)
                        .FirstOrDefault(x => x.AttributeControlType == AttributeControlType.ColorSquares);

                    if (colorAttr != null)
                    {
                        var colorValues =
                            from a in colorAttr.ProductVariantAttributeValues.Take(50)
                            where (a.ColorSquaresRgb.HasValue() && !a.ColorSquaresRgb.IsCaseInsensitiveEqual("transparent"))
                            select new ProductOverviewModel.ColorAttributeModel()
                            {
                                Color = a.ColorSquaresRgb,
                                Alias = a.Alias,
                                FriendlyName = a.GetLocalized(l => l.Name)
                            };

                        if (colorValues.Any())
                        {
                            model.ColorAttributes.AddRange(colorValues.Distinct());
                        }
                    }

                    #endregion
                }

                var currentCustomer = _services.WorkContext.CurrentCustomer;
                var taxDisplayType = _services.WorkContext.GetTaxDisplayTypeFor(currentCustomer, _services.StoreContext.CurrentStore.Id);
                string taxInfo = T(taxDisplayType == TaxDisplayType.IncludingTax ? "Tax.InclVAT" : "Tax.ExclVAT");
                string shippingInfoLink = _urlHelper.RouteUrl("Topic", new { SystemName = "shippinginfo" });

                model.ProductMinPriceId = minPriceProduct.Id;
                model.Manufacturers = PrepareManufacturersOverviewModel(_manufacturerService.GetProductManufacturersByProductId(product.Id));
                model.ShowSku = _catalogSettings.ShowProductSku;
                model.ShowWeight = _catalogSettings.ShowWeight;
                model.ShowDimensions = _catalogSettings.ShowDimensions;
                model.Sku = minPriceProduct.Sku;
                model.Dimensions = T("Products.DimensionsValue").Text.FormatCurrent(
                    minPriceProduct.Width.ToString("F2"),
                    minPriceProduct.Height.ToString("F2"),
                    minPriceProduct.Length.ToString("F2")
                );
                model.DimensionMeasureUnit = _measureService.GetMeasureDimensionById(_measureSettings.BaseDimensionId).Name;
                model.ThumbDimension = _mediaSettings.ProductThumbPictureSize;
                model.ShowLegalInfo = _taxSettings.ShowLegalHintsInProductList;
                model.LegalInfo = T("Tax.LegalInfoFooter").Text.FormatWith(taxInfo, shippingInfoLink);
                model.RatingSum = product.ApprovedRatingSum;
                model.TotalReviews = product.ApprovedTotalReviews;
                model.ShowReviews = _catalogSettings.ShowProductReviewsInProductLists;
                model.ShowDeliveryTimes = _catalogSettings.ShowDeliveryTimesInProductLists;

                var deliveryTime = _deliveryTimeService.GetDeliveryTime(minPriceProduct);
                if (deliveryTime != null)
                {
                    model.DeliveryTimeName = deliveryTime.GetLocalized(x => x.Name);
                    model.DeliveryTimeHexValue = deliveryTime.ColorHexValue;
                }

                model.IsShipEnabled = minPriceProduct.IsShipEnabled;
                model.DisplayDeliveryTimeAccordingToStock = minPriceProduct.DisplayDeliveryTimeAccordingToStock(_catalogSettings);
                model.StockAvailablity = minPriceProduct.FormatStockMessage(_localizationService);

                model.DisplayBasePrice = _catalogSettings.ShowBasePriceInProductLists;
                model.BasePriceInfo = minPriceProduct.GetBasePriceInfo(_localizationService, _priceFormatter);
                model.CompareEnabled = _catalogSettings.CompareProductsEnabled;
                model.HideBuyButtonInLists = _catalogSettings.HideBuyButtonInLists;

                var addShippingPrice = _currencyService.ConvertCurrency(minPriceProduct.AdditionalShippingCharge,
                    _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId), _services.WorkContext.WorkingCurrency);

                if (addShippingPrice > 0)
                {
                    model.TransportSurcharge = T("Common.AdditionalShippingSurcharge").Text.FormatWith(_priceFormatter.FormatPrice(addShippingPrice, true, false));
                }

                if (minPriceProduct.Weight > 0)
                {
                    model.Weight = "{0} {1}".FormatCurrent(minPriceProduct.Weight.ToString("F2"), _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId).Name);
                }

                // IsNew
                if (_catalogSettings.LabelAsNewForMaxDays.HasValue)
                {
                    model.IsNew = (DateTime.UtcNow - product.CreatedOnUtc).Days <= _catalogSettings.LabelAsNewForMaxDays.Value;
                }

                models.Add(model);
            }
            return models;
        }