Exemple #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.DeliveryTimesPresentation = _catalogSettings.DeliveryTimesInLists;
            }
            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.DeliveryTimesPresentation = _catalogSettings.DeliveryTimesInLists;
                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.DeliveryTimesPresentation = _catalogSettings.DeliveryTimesInLists;
                settings.MapDimensions             = _catalogSettings.ShowDimensions;
            }

            fn?.Invoke(settings);

            return(settings);
        }
Exemple #2
0
        public virtual async Task <ProductSummaryModel> MapProductSummaryModelAsync(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,
                    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,
                    DeliveryTimesPresentation         = settings.DeliveryTimesPresentation,
                };

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

                using var scope = new DbContextScope(_db, retainConnection: true, deferCommit: true);

                // PERF!!
                var store       = _services.StoreContext.CurrentStore;
                var customer    = _services.WorkContext.CurrentCustomer;
                var currency    = _services.WorkContext.WorkingCurrency;
                var language    = _services.WorkContext.WorkingLanguage;
                var allowPrices = await _services.Permissions.AuthorizeAsync(Permissions.Catalog.DisplayPrice);

                var allowShoppingCart = await _services.Permissions.AuthorizeAsync(Permissions.Cart.AccessShoppingCart);

                var allowWishlist = await _services.Permissions.AuthorizeAsync(Permissions.Cart.AccessWishlist);

                var taxDisplayType       = _workContext.GetTaxDisplayTypeFor(customer, store.Id);
                var cachedBrandModels    = new Dictionary <int, BrandOverviewModel>();
                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() : Array.Empty <int>();

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

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

                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 = (await _urlHelper.TopicAsync("shippinginfo"));
                    legalInfo = shippingInfoUrl.HasValue()
                        ? T("Tax.LegalInfoShort", taxInfo, shippingInfoUrl)
                        : T("Tax.LegalInfoShort2", taxInfo);
                }

                if (prefetchSlugs)
                {
                    await _urlService.PrefetchUrlRecordsAsync(nameof(Product), new[] { language.Id, 0 }, allProductIds);
                }

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

                // Run in uncommitting scope, because pictures could be updated (IsNew property)
                var batchContext = _dataExporter.Value.CreateProductExportContext(products, customer, null, 1, false);

                if (settings.MapPrices)
                {
                    await batchContext.AppliedDiscounts.LoadAllAsync();

                    await batchContext.TierPrices.LoadAllAsync();
                }

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

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

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

                if (settings.MapManufacturers)
                {
                    await batchContext.ProductManufacturers.LoadAllAsync();
                }

                if (settings.MapSpecificationAttributes)
                {
                    await batchContext.SpecificationAttributes.LoadAllAsync();

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

                        // Prefetch all spec attribute translations
                        await 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,
                    CachedBrandModels = cachedBrandModels,
                    PrimaryCurrency   = store.PrimaryStoreCurrency,
                    StoreCurrency     = 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 = (await _mediaService.GetFilesByIdsAsync(fileIds)).ToDictionarySafe(x => x.Id);
                }

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

                _services.DisplayControl.AnnounceRange(products);

                await scope.CommitAsync();

                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.Brand != null);

                return(model);
            }
        }
 public virtual async Task <ProductSummaryModel> MapProductSummaryModelAsync(IPagedList <Product> products, ProductSummaryMappingSettings settings)
 {
     return(await MapProductSummaryModelAsync(products, null, settings));
 }
Exemple #4
0
        public virtual Task <ProductSummaryModel> MapProductSummaryModelAsync(IList <Product> products, ProductSummaryMappingSettings settings)
        {
            Guard.NotNull(products, nameof(products));

            return(MapProductSummaryModelAsync(products.ToPagedList(0, int.MaxValue), settings));
        }
 public virtual async Task <ProductSummaryModel> MapProductSummaryModelAsync(CatalogSearchResult sourceResult, ProductSummaryMappingSettings settings)
 {
     return(await MapProductSummaryModelAsync(await sourceResult.GetHitsAsync(), sourceResult, settings));
 }