Exemplo n.º 1
0
        public ProductSummaryMappingSettings GetBestFitProductSummaryMappingSettings(ProductSummaryViewMode viewMode, Action <ProductSummaryMappingSettings> fn)
        {
            var settings = new ProductSummaryMappingSettings
            {
                ViewMode      = viewMode,
                MapPrices     = true,
                MapPictures   = true,
                ThumbnailSize = _mediaSettings.ProductThumbPictureSize
            };

            if (viewMode == ProductSummaryViewMode.Grid)
            {
                settings.MapShortDescription = _catalogSettings.ShowShortDescriptionInGridStyleLists;
                settings.MapManufacturers    = _catalogSettings.ShowManufacturerInGridStyleLists;
                settings.MapColorAttributes  = _catalogSettings.ShowColorSquaresInLists;
                settings.MapAttributes       = _catalogSettings.ShowProductOptionsInLists;
                settings.MapReviews          = _catalogSettings.ShowProductReviewsInProductLists;
                settings.MapDeliveryTimes    = _catalogSettings.ShowDeliveryTimesInProductLists;
            }
            else if (viewMode == ProductSummaryViewMode.List)
            {
                settings.MapShortDescription = true;
                settings.MapLegalInfo        = _taxSettings.ShowLegalHintsInProductList;
                settings.MapManufacturers    = true;
                settings.MapColorAttributes  = _catalogSettings.ShowColorSquaresInLists;
                settings.MapAttributes       = _catalogSettings.ShowProductOptionsInLists;
                //settings.MapSpecificationAttributes = true; // TODO: (mc) What about SpecAttrs in List-Mode (?) Option?
                settings.MapReviews       = _catalogSettings.ShowProductReviewsInProductLists;
                settings.MapDeliveryTimes = _catalogSettings.ShowDeliveryTimesInProductLists;
                settings.MapDimensions    = _catalogSettings.ShowDimensions;
            }
            else if (viewMode == ProductSummaryViewMode.Compare)
            {
                settings.MapShortDescription        = _catalogSettings.IncludeShortDescriptionInCompareProducts;
                settings.MapFullDescription         = _catalogSettings.IncludeFullDescriptionInCompareProducts;
                settings.MapLegalInfo               = _taxSettings.ShowLegalHintsInProductList;
                settings.MapManufacturers           = true;
                settings.MapAttributes              = true;
                settings.MapSpecificationAttributes = true;
                settings.MapReviews       = _catalogSettings.ShowProductReviewsInProductLists;
                settings.MapDeliveryTimes = _catalogSettings.ShowDeliveryTimesInProductLists;
                settings.MapDimensions    = _catalogSettings.ShowDimensions;
            }

            fn?.Invoke(settings);

            return(settings);
        }
Exemplo n.º 2
0
        public virtual ProductSummaryModel MapProductSummaryModel(IList <Product> products, ProductSummaryMappingSettings settings)
        {
            Guard.NotNull(products, nameof(products));

            return(MapProductSummaryModel(new PagedList <Product>(products, 0, int.MaxValue), settings));
        }
Exemplo n.º 3
0
        public virtual ProductSummaryModel MapProductSummaryModel(IPagedList <Product> products, ProductSummaryMappingSettings settings)
        {
            Guard.NotNull(products, nameof(products));

            if (settings == null)
            {
                settings = new ProductSummaryMappingSettings();
            }

            using (_services.Chronometer.Step("MapProductSummaryModel"))
            {
                // PERF!!
                var store                    = _services.StoreContext.CurrentStore;
                var customer                 = _services.WorkContext.CurrentCustomer;
                var currency                 = _services.WorkContext.WorkingCurrency;
                var allowPrices              = _services.Permissions.Authorize(StandardPermissionProvider.DisplayPrices);
                var sllowShoppingCart        = _services.Permissions.Authorize(StandardPermissionProvider.EnableShoppingCart);
                var allowWishlist            = _services.Permissions.Authorize(StandardPermissionProvider.EnableWishlist);
                var taxDisplayType           = _services.WorkContext.GetTaxDisplayTypeFor(customer, store.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 (settings.MapLegalInfo)
                {
                    if (_topicService.Value.GetTopicBySystemName("ShippingInfo", store.Id) == null)
                    {
                        legalInfo = T("Tax.LegalInfoShort2").Text.FormatInvariant(taxInfo);
                    }
                    else
                    {
                        var shippingInfoLink = _urlHelper.RouteUrl("Topic", new { SystemName = "shippinginfo" });
                        legalInfo = T("Tax.LegalInfoShort").Text.FormatInvariant(taxInfo, shippingInfoLink);
                    }
                }

                using (var scope = new DbContextScope(ctx: _services.DbContext, autoCommit: false, validateOnSave: false))
                {
                    // Run in uncommitting scope, because pictures could be updated (IsNew property)
                    var batchContext = _dataExporter.Value.CreateProductExportContext(products);

                    if (settings.MapPrices)
                    {
                        batchContext.AppliedDiscounts.LoadAll();
                        batchContext.TierPrices.LoadAll();
                    }

                    if (settings.MapAttributes || settings.MapColorAttributes)
                    {
                        batchContext.Attributes.LoadAll();
                    }

                    if (settings.MapManufacturers)
                    {
                        batchContext.ProductManufacturers.LoadAll();
                    }

                    if (settings.MapSpecificationAttributes)
                    {
                        batchContext.SpecificationAttributes.LoadAll();
                    }

                    var model = new ProductSummaryModel(products)
                    {
                        ViewMode                          = settings.ViewMode,
                        GridColumnSpan                    = _catalogSettings.GridStyleListColumnSpan,
                        ShowSku                           = _catalogSettings.ShowProductSku,
                        ShowWeight                        = _catalogSettings.ShowWeight,
                        ShowDimensions                    = settings.MapDimensions,
                        ShowLegalInfo                     = settings.MapLegalInfo,
                        ShowDescription                   = settings.MapShortDescription,
                        ShowFullDescription               = settings.MapFullDescription,
                        ShowRatings                       = settings.MapReviews,
                        ShowDeliveryTimes                 = settings.MapDeliveryTimes,
                        ShowPrice                         = settings.MapPrices,
                        ShowBasePrice                     = settings.MapPrices && _catalogSettings.ShowBasePriceInProductLists && settings.ViewMode != ProductSummaryViewMode.Mini,
                        ShowShippingSurcharge             = settings.MapPrices && settings.ViewMode != ProductSummaryViewMode.Mini,
                        ShowButtons                       = settings.ViewMode != ProductSummaryViewMode.Mini,
                        ShowBrand                         = settings.MapManufacturers,
                        ForceRedirectionAfterAddingToCart = settings.ForceRedirectionAfterAddingToCart,
                        CompareEnabled                    = _catalogSettings.CompareProductsEnabled,
                        WishlistEnabled                   = _permissionService.Value.Authorize(StandardPermissionProvider.EnableWishlist),
                        BuyEnabled                        = !_catalogSettings.HideBuyButtonInLists,
                        ThumbSize                         = settings.ThumbnailSize,
                        ShowDiscountBadge                 = _catalogSettings.ShowDiscountSign,
                        ShowNewBadge                      = _catalogSettings.LabelAsNewForMaxDays.HasValue
                    };

                    var mapItemContext = new MapProductSummaryItemContext
                    {
                        BatchContext             = batchContext,
                        CachedManufacturerModels = cachedManufacturerModels,
                        Currency          = currency,
                        LegalInfo         = legalInfo,
                        Model             = model,
                        Resources         = res,
                        Settings          = settings,
                        Customer          = customer,
                        Store             = store,
                        AllowPrices       = allowPrices,
                        AllowShoppingCart = sllowShoppingCart,
                        AllowWishlist     = allowWishlist,
                        TaxDisplayType    = taxDisplayType
                    };

                    foreach (var product in products)
                    {
                        MapProductSummaryItem(product, mapItemContext);
                    }

                    _services.DisplayControl.AnnounceRange(products);

                    scope.Commit();

                    batchContext.Clear();

                    // don't show stuff without data at all
                    model.ShowDescription = model.ShowDescription && model.Items.Any(x => x.ShortDescription.HasValue());
                    model.ShowBrand       = model.ShowBrand && model.Items.Any(x => x.Manufacturer != null);

                    return(model);
                }
            }
        }
Exemplo n.º 4
0
        public virtual ProductSummaryModel MapProductSummaryModel(IPagedList <Product> products, ProductSummaryMappingSettings settings)
        {
            Guard.NotNull(products, nameof(products));

            if (settings == null)
            {
                settings = new ProductSummaryMappingSettings();
            }

            using (_services.Chronometer.Step("MapProductSummaryModel"))
            {
                var model = new ProductSummaryModel(products)
                {
                    ViewMode                          = settings.ViewMode,
                    GridColumnSpan                    = _catalogSettings.GridStyleListColumnSpan,
                    ShowSku                           = _catalogSettings.ShowProductSku,
                    ShowWeight                        = _catalogSettings.ShowWeight,
                    ShowDimensions                    = settings.MapDimensions,
                    ShowLegalInfo                     = settings.MapLegalInfo,
                    ShowDescription                   = settings.MapShortDescription,
                    ShowFullDescription               = settings.MapFullDescription,
                    ShowRatings                       = settings.MapReviews,
                    ShowDeliveryTimes                 = settings.MapDeliveryTimes,
                    ShowPrice                         = settings.MapPrices,
                    ShowBasePrice                     = settings.MapPrices && _catalogSettings.ShowBasePriceInProductLists && settings.ViewMode != ProductSummaryViewMode.Mini,
                    ShowShippingSurcharge             = settings.MapPrices && settings.ViewMode != ProductSummaryViewMode.Mini,
                    ShowButtons                       = settings.ViewMode != ProductSummaryViewMode.Mini,
                    ShowBrand                         = settings.MapManufacturers,
                    ForceRedirectionAfterAddingToCart = settings.ForceRedirectionAfterAddingToCart,
                    CompareEnabled                    = _catalogSettings.CompareProductsEnabled,
                    WishlistEnabled                   = _services.Permissions.Authorize(Permissions.Cart.AccessWishlist),
                    BuyEnabled                        = !_catalogSettings.HideBuyButtonInLists,
                    ThumbSize                         = settings.ThumbnailSize,
                    ShowDiscountBadge                 = _catalogSettings.ShowDiscountSign,
                    ShowNewBadge                      = _catalogSettings.LabelAsNewForMaxDays.HasValue
                };

                if (products.Count == 0)
                {
                    // No products, stop here.
                    return(model);
                }

                // PERF!!
                var store                    = _services.StoreContext.CurrentStore;
                var customer                 = _services.WorkContext.CurrentCustomer;
                var currency                 = _services.WorkContext.WorkingCurrency;
                var language                 = _services.WorkContext.WorkingLanguage;
                var allowPrices              = _services.Permissions.Authorize(Permissions.Catalog.DisplayPrice);
                var allowShoppingCart        = _services.Permissions.Authorize(Permissions.Cart.AccessShoppingCart);
                var allowWishlist            = _services.Permissions.Authorize(Permissions.Cart.AccessWishlist);
                var taxDisplayType           = _services.WorkContext.GetTaxDisplayTypeFor(customer, store.Id);
                var cachedManufacturerModels = new Dictionary <int, ManufacturerOverviewModel>();
                var prefetchTranslations     = settings.PrefetchTranslations == true || (settings.PrefetchTranslations == null && _performanceSettings.AlwaysPrefetchTranslations);
                var prefetchSlugs            = settings.PrefetchUrlSlugs == true || (settings.PrefetchUrlSlugs == null && _performanceSettings.AlwaysPrefetchUrlSlugs);
                var allProductIds            = prefetchSlugs || prefetchTranslations?products.Select(x => x.Id).ToArray() : new int[0];

                //var productIds = products.Select(x => x.Id).ToArray();

                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 (settings.MapLegalInfo)
                {
                    var shippingInfoUrl = _urlHelper.Topic("shippinginfo").ToString();
                    legalInfo = shippingInfoUrl.HasValue()
                        ? T("Tax.LegalInfoShort").Text.FormatInvariant(taxInfo, shippingInfoUrl)
                        : T("Tax.LegalInfoShort2").Text.FormatInvariant(taxInfo);
                }

                if (prefetchSlugs)
                {
                    _urlRecordService.PrefetchUrlRecords(nameof(Product), new[] { language.Id, 0 }, allProductIds);
                }

                if (prefetchTranslations)
                {
                    // Prefetch all delivery time translations
                    _localizedEntityService.PrefetchLocalizedProperties(nameof(DeliveryTime), language.Id, null);
                }

                using (var scope = new DbContextScope(ctx: _services.DbContext, autoCommit: false, validateOnSave: false))
                {
                    // Run in uncommitting scope, because pictures could be updated (IsNew property)
                    var batchContext = _dataExporter.Value.CreateProductExportContext(products, customer, null, 1, false);

                    if (settings.MapPrices)
                    {
                        batchContext.AppliedDiscounts.LoadAll();
                        batchContext.TierPrices.LoadAll();
                    }

                    if (settings.MapAttributes || settings.MapColorAttributes)
                    {
                        batchContext.Attributes.LoadAll();

                        if (prefetchTranslations)
                        {
                            // Prefetch all product attribute translations
                            PrefetchTranslations(
                                nameof(ProductAttribute),
                                language.Id,
                                batchContext.Attributes.SelectMany(x => x.Value).Select(x => x.ProductAttribute));

                            // Prefetch all variant attribute value translations
                            PrefetchTranslations(
                                nameof(ProductVariantAttributeValue),
                                language.Id,
                                batchContext.Attributes.SelectMany(x => x.Value).SelectMany(x => x.ProductVariantAttributeValues));
                        }
                    }

                    if (settings.MapManufacturers)
                    {
                        batchContext.ProductManufacturers.LoadAll();
                    }

                    if (settings.MapSpecificationAttributes)
                    {
                        batchContext.SpecificationAttributes.LoadAll();

                        if (prefetchTranslations)
                        {
                            // Prefetch all spec attribute option translations
                            PrefetchTranslations(
                                nameof(SpecificationAttributeOption),
                                language.Id,
                                batchContext.SpecificationAttributes.SelectMany(x => x.Value).Select(x => x.SpecificationAttributeOption));

                            // Prefetch all spec attribute translations
                            PrefetchTranslations(
                                nameof(SpecificationAttribute),
                                language.Id,
                                batchContext.SpecificationAttributes.SelectMany(x => x.Value).Select(x => x.SpecificationAttributeOption.SpecificationAttribute));
                        }
                    }

                    // If a size has been set in the view, we use it in priority
                    int thumbSize = model.ThumbSize ?? _mediaSettings.ProductThumbPictureSize;

                    var mapItemContext = new MapProductSummaryItemContext
                    {
                        BatchContext             = batchContext,
                        CachedManufacturerModels = cachedManufacturerModels,
                        Currency          = currency,
                        LegalInfo         = legalInfo,
                        Model             = model,
                        Resources         = res,
                        Settings          = settings,
                        Customer          = customer,
                        Store             = store,
                        AllowPrices       = allowPrices,
                        AllowShoppingCart = allowShoppingCart,
                        AllowWishlist     = allowWishlist,
                        TaxDisplayType    = taxDisplayType
                    };

                    if (settings.MapPictures)
                    {
                        var fileIds = products
                                      .Select(x => x.MainPictureId ?? 0)
                                      .Where(x => x != 0)
                                      .Distinct()
                                      .ToArray();

                        mapItemContext.MediaFiles = _mediaService.GetFilesByIds(fileIds).ToDictionarySafe(x => x.Id);
                    }

                    foreach (var product in products)
                    {
                        MapProductSummaryItem(product, mapItemContext);
                    }

                    _services.DisplayControl.AnnounceRange(products);

                    scope.Commit();

                    batchContext.Clear();

                    // don't show stuff without data at all
                    model.ShowDescription = model.ShowDescription && model.Items.Any(x => x.ShortDescription?.Value?.HasValue() == true);
                    model.ShowBrand       = model.ShowBrand && model.Items.Any(x => x.Manufacturer != null);

                    return(model);
                }
            }
        }