Exemple #1
0
        /// <summary>
        /// Prepare the product breadcrumb model
        /// </summary>
        /// <param name="product">Product</param>
        /// <returns>
        /// Product breadcrumb model
        /// </returns>
        /// <exception cref="System.ArgumentNullException">product</exception>
        protected override ProductDetailsModel.ProductBreadcrumbModel PrepareProductBreadcrumbModel(Product product)
        {
            try
            {
                var miscPlugins = _pluginFinder.GetPlugins <BreadCrumbPlugin>(storeId: _storeContext.CurrentStore.Id).ToList();
                if (miscPlugins.Count > 0)
                {
                    if (product == null)
                    {
                        throw new ArgumentNullException("product");
                    }

                    var cacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_BREADCRUMB_MODEL_KEY,
                                                 product.Id,
                                                 _workContext.WorkingLanguage.Id,
                                                 string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
                                                 _storeContext.CurrentStore.Id);
                    var cachedModel = _cacheManager.Get(cacheKey, () =>
                    {
                        var breadcrumbModel = new ProductDetailsModel.ProductBreadcrumbModel
                        {
                            Enabled       = _catalogSettings.CategoryBreadcrumbEnabled,
                            ProductId     = product.Id,
                            ProductName   = product.GetLocalized(x => x.Name),
                            ProductSeName = product.GetSeName()
                        };

                        var productCategories = _categoryService.GetProductCategoriesByProductId(product.Id);
                        if (productCategories.Any())
                        {
                            var category = GetReferringCategory(_httpContextAccessor.HttpContext.Request.Headers["Referer"].ToString(), productCategories);
                            if (category != null)
                            {
                                foreach (var catBr in category.GetCategoryBreadCrumb(_categoryService, _aclService, _storeMappingService))
                                {
                                    breadcrumbModel.CategoryBreadcrumb.Add(new CategorySimpleModel
                                    {
                                        Id               = catBr.Id,
                                        Name             = catBr.GetLocalized(x => x.Name),
                                        SeName           = catBr.GetSeName(),
                                        IncludeInTopMenu = catBr.IncludeInTopMenu
                                    });
                                }
                            }
                        }
                        return(breadcrumbModel);
                    });

                    return(cachedModel);
                }
                else
                {
                    return(base.PrepareProductBreadcrumbModel(product));
                }
            }
            catch
            {
                return(base.PrepareProductBreadcrumbModel(product));
            }
        }
        /// <summary>
        /// Prepare the product breadcrumb model
        /// </summary>
        /// <param name="product">Product</param>
        /// <returns>Product breadcrumb model</returns>
        protected virtual ProductDetailsModel.ProductBreadcrumbModel PrepareProductBreadcrumbModel(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            var cacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_BREADCRUMB_MODEL_KEY,
                                         product.Id,
                                         _workContext.WorkingLanguage.Id,
                                         string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
                                         _storeContext.CurrentStore.Id);
            var cachedModel = _cacheManager.Get(cacheKey, () =>
            {
                var breadcrumbModel = new ProductDetailsModel.ProductBreadcrumbModel
                {
                    Enabled       = _catalogSettings.CategoryBreadcrumbEnabled,
                    ProductId     = product.Id,
                    ProductName   = _localizationService.GetLocalized(product, x => x.Name),
                    ProductSeName = _urlRecordService.GetSeName(product)
                };
                var productCategories = _categoryService.GetProductCategoriesByProductId(product.Id);
                if (!productCategories.Any())
                {
                    return(breadcrumbModel);
                }

                var category = productCategories[0].Category;
                if (category == null)
                {
                    return(breadcrumbModel);
                }

                foreach (var catBr in _categoryService.GetCategoryBreadCrumb(category))
                {
                    breadcrumbModel.CategoryBreadcrumb.Add(new CategorySimpleModel
                    {
                        Id               = catBr.Id,
                        Name             = _localizationService.GetLocalized(catBr, x => x.Name),
                        SeName           = _urlRecordService.GetSeName(catBr),
                        IncludeInTopMenu = catBr.IncludeInTopMenu
                    });
                }

                return(breadcrumbModel);
            });

            return(cachedModel);
        }
Exemple #3
0
        public ActionResult ProductBreadcrumb(int productId, int productItemType = 0)
        {
            var product = _productService.GetProductById(productId);

            if (product == null)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            if (!_catalogSettings.CategoryBreadcrumbEnabled)
            {
                return(Content(""));
            }

            var customerRolesIds = _workContext.CurrentCustomer.CustomerRoles
                                   .Where(cr => cr.Active).Select(cr => cr.Id).ToList();
            var cacheKey   = string.Format(ModelCacheEventConsumer.PRODUCT_BREADCRUMB_MODEL_KEY, product.Id, _workContext.WorkingLanguage.Id, string.Join(",", customerRolesIds));
            var cacheModel = _cacheManager.Get(cacheKey, () =>
            {
                var model = new ProductDetailsModel.ProductBreadcrumbModel()
                {
                    ProductId             = product.Id,
                    ProductName           = product.GetLocalized(x => x.Name),
                    ProductSeName         = product.GetSeName(),
                    ProductItemTypeAnchor = productItemType > 0 ? ((ProductItemTypeEnum)productItemType).ToString() : "Product"
                };
                var productCategories = _categoryService.GetProductCategoriesByProductId(product.Id);
                if (productCategories.Count > 0)
                {
                    var category = productCategories[0].Category;
                    if (category != null)
                    {
                        foreach (var catBr in GetCategoryBreadCrumb(category))
                        {
                            model.CategoryBreadcrumb.Add(new CategoryModel()
                            {
                                Id     = catBr.Id,
                                Name   = catBr.GetLocalized(x => x.Name),
                                SeName = catBr.GetSeName()
                            });
                        }
                    }
                }
                return(model);
            });

            return(PartialView("_ProductBreadcrumb", cacheModel));
        }