Exemple #1
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            if (!_catalogSettings.ShowPopularProductTagsOnHomepage)
            {
                return(Empty());
            }

            var store = Services.StoreContext.CurrentStore;

            var cacheKey   = string.Format(ModelCacheInvalidator.PRODUCTTAG_POPULAR_MODEL_KEY, Services.WorkContext.WorkingLanguage.Id, store.Id);
            var cacheModel = await Services.CacheFactory.GetMemoryCache().GetAsync(cacheKey, async(o) =>
            {
                o.ExpiresIn(TimeSpan.FromHours(1));

                var customer = Services.WorkContext.CurrentCustomer;
                var model    = new PopularProductTagsModel();

                // TODO: (mg) This is gonna explode with large amount of tags. Rethink!
                var allTags = await _db.ProductTags
                              .Where(x => x.Published)
                              .ToListAsync();

                var tags = (from t in allTags
                            let numProducts = _productTagService.CountProductsByTagIdAsync(t.Id, customer, store.Id).Await()
                                              where numProducts > 0
                                              orderby numProducts descending
                                              select new
                {
                    Tag = t,
                    LocalizedName = t.GetLocalized(x => x.Name),
                    NumProducts = numProducts
                }).ToList();

                tags = tags
                       .OrderBy(x => x.LocalizedName.Value)
                       .Take(_catalogSettings.NumberOfProductTags)
                       .ToList();

                model.TotalTags = allTags.Count;

                foreach (var tag in tags)
                {
                    model.Tags.Add(new ProductTagModel
                    {
                        Id           = tag.Tag.Id,
                        Name         = tag.LocalizedName,
                        Slug         = tag.Tag.BuildSlug(),
                        ProductCount = tag.NumProducts
                    });
                }

                return(model);
            });

            return(View(cacheModel));
        }
        public ActionResult PopularProductTags()
        {
            if (!_catalogSettings.ShowPopularProductTagsOnHomepage)
            {
                return(new EmptyResult());
            }

            var store = Services.StoreContext.CurrentStore;

            var cacheKey   = string.Format(ModelCacheEventConsumer.PRODUCTTAG_POPULAR_MODEL_KEY, Services.WorkContext.WorkingLanguage.Id, store.Id);
            var cacheModel = Services.Cache.Get(cacheKey, () =>
            {
                var model = new PopularProductTagsModel();

                var allTags = _productTagService
                              .GetAllProductTags()
                              .Where(x => _productTagService.GetProductCount(x.Id, store.Id) > 0)
                              .OrderByDescending(x => _productTagService.GetProductCount(x.Id, store.Id))
                              .ToList();

                var tags = allTags
                           .Take(_catalogSettings.NumberOfProductTags)
                           .ToList();

                tags = tags.OrderBy(x => x.GetLocalized(y => y.Name)).ToList();

                model.TotalTags = allTags.Count;

                foreach (var tag in tags)
                {
                    model.Tags.Add(new ProductTagModel
                    {
                        Id           = tag.Id,
                        Name         = tag.GetLocalized(y => y.Name),
                        SeName       = tag.GetSeName(),
                        ProductCount = _productTagService.GetProductCount(tag.Id, store.Id)
                    });
                }

                return(model);
            });

            return(PartialView(cacheModel));
        }
Exemple #3
0
        public virtual PopularProductTagsModel PreparePopularProductTagsModel()
        {
            var cacheKey    = string.Format(ModelCacheEventConsumer.PRODUCTTAG_POPULAR_MODEL_KEY, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id);
            var cachedModel = _cacheManager.Get(cacheKey, () =>
            {
                var model = new PopularProductTagsModel();

                //get all tags
                var allTags = _productTagService
                              .GetAllProductTags()
                              //filter by current store
                              .Where(x => _productTagService.GetProductCount(x.Id, _storeContext.CurrentStore.Id) > 0)
                              //order by product count
                              .OrderByDescending(x => _productTagService.GetProductCount(x.Id, _storeContext.CurrentStore.Id))
                              .ToList();

                var tags = allTags
                           .Take(_catalogSettings.NumberOfProductTags)
                           .ToList();
                //sorting
                tags = tags.OrderBy(x => x.GetLocalized(y => y.Name)).ToList();

                model.TotalTags = allTags.Count;

                foreach (var tag in tags)
                {
                    model.Tags.Add(new ProductTagModel
                    {
                        Id           = tag.Id,
                        Name         = tag.GetLocalized(y => y.Name),
                        SeName       = tag.GetSeName(),
                        ProductCount = _productTagService.GetProductCount(tag.Id, _storeContext.CurrentStore.Id)
                    });
                }
                return(model);
            });

            return(cachedModel);
        }
        public async Task <IActionResult> ProductTagsAll()
        {
            // TODO: (mh) (core) This is nearly the same code as in PopularProductTagsViewComponent > implement helper method PreparePopularProductTagsModel?
            var store    = Services.StoreContext.CurrentStore;
            var customer = Services.WorkContext.CurrentCustomer;
            var model    = new PopularProductTagsModel();

            // TODO: (mg) This is gonna explode with large amount of tags. Rethink!
            var allTags = await _db.ProductTags
                          .Where(x => x.Published)
                          .ToListAsync();

            var tags = (from t in allTags
                        let numProducts = _productTagService.CountProductsByTagIdAsync(t.Id, customer, store.Id).Await()
                                          where numProducts > 0
                                          orderby numProducts descending
                                          select new
            {
                Tag = t,
                LocalizedName = t.GetLocalized(x => x.Name),
                NumProducts = numProducts
            })
                       .OrderBy(x => x.LocalizedName.Value)
                       .ToList();

            foreach (var tag in tags)
            {
                model.Tags.Add(new ProductTagModel
                {
                    Id           = tag.Tag.Id,
                    Name         = tag.LocalizedName,
                    Slug         = tag.Tag.BuildSlug(),
                    ProductCount = tag.NumProducts
                });
            }

            return(View(model));
        }
        public ActionResult ProductTagsAll()
        {
            var store = Services.StoreContext.CurrentStore;
            var model = new PopularProductTagsModel();

            model.Tags = _productTagService
                         .GetAllProductTags()
                         .Where(x => _productTagService.GetProductCount(x.Id, store.Id) > 0)
                         .OrderBy(x => x.GetLocalized(y => y.Name))
                         .Select(x =>
            {
                var ptModel = new ProductTagModel
                {
                    Id           = x.Id,
                    Name         = x.GetLocalized(y => y.Name),
                    SeName       = x.GetSeName(),
                    ProductCount = _productTagService.GetProductCount(x.Id, store.Id)
                };
                return(ptModel);
            })
                         .ToList();

            return(View(model));
        }
Exemple #6
0
        public virtual PopularProductTagsModel PrepareProductTagsAllModel()
        {
            var model = new PopularProductTagsModel();

            model.Tags = _productTagService
                         .GetAllProductTags()
                         //filter by current store
                         .Where(x => _productTagService.GetProductCount(x.Id, _storeContext.CurrentStore.Id) > 0)
                         //sort by name
                         .OrderBy(x => x.GetLocalized(y => y.Name))
                         .Select(x =>
            {
                var ptModel = new ProductTagModel
                {
                    Id           = x.Id,
                    Name         = x.GetLocalized(y => y.Name),
                    SeName       = x.GetSeName(),
                    ProductCount = _productTagService.GetProductCount(x.Id, _storeContext.CurrentStore.Id)
                };
                return(ptModel);
            })
                         .ToList();
            return(model);
        }