multiple taxonomies?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hi,

I have multiple taxonomies for examples Genres (Comedy, Thriller) and ProductGroups (DVD, BluRay).

I want customers to be able to browse products byGenre and by ProductGroup.

To achieve this I create a Category structure like below:

> Genres
> Comedy
   > Romantic Comedy
   ...
  > Thriller
  ...
> ProductGroups
  > DVD
   >  DVD rental
   ...
  > BluRay
  ...

(If there's a better way to do this, please let me know.)

Now instead of CategoryNavigation.ascx displaying the complete Category tree, I would like to place two instances of the CategoryNavigation.ascx, one with the root Category set to Genres and one with the root Category set to ProductGroups.

Is this possible out of the box or do I need to do custom coding?

Thanks!
13 years ago
Would be nice if you could optionally set the RootCategoryID?


<nopCommerce:CategoryNavigation ID="ctrlCategoryNavigation" runat="server" RootCategoryID="123" />
13 years ago
Hi  I have just implimented a solution to this need in 1.90.

The affected files are :

CategoryNavigation.ascx and CategoryNavigation.ascx.cs as well as the necessary master page/s.

Replace all code in CategoryNavigation.ascx.cs with:

//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  https://www.nopcommerce.com/License.aspx.
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
//
// Contributor(s): _______.
//------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic.Categories;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class CategoryNavigation: BaseNopFrontendUserControl
    {
        //C47 Mod Begin
        int RCID = 0;

        public int RootCategoryID
        {
            get
            {
                return RCID;
            }
            set
            {
                RCID = value;
            }
        }

        //C47 Mod End

        #region Classes
        public class NopCommerceLi : WebControl, INamingContainer
        {
            public NopCommerceLi()
            {
                this.HyperLink = new HyperLink();
            }

            protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                writer.WriteBeginTag("li");
                writer.WriteAttribute("class", this.CssClass);
                if (!String.IsNullOrEmpty(this.LiLeftMargin))
                {
                    writer.WriteAttribute("style", string.Format("margin-left: {0}px", this.LiLeftMargin));
                }
                writer.Write(HtmlTextWriter.TagRightChar);
                this.HyperLink.RenderControl(writer);
                writer.WriteEndTag("li");
            }

            public string LinkText
            {
                get
                {
                    return this.HyperLink.Text;
                }
                set
                {
                    if (value != null)
                    {
                        this.HyperLink.Text = value;
                    }
                }
            }

            public HyperLink HyperLink { get; set; }

            public string LiLeftMargin
            {
                get
                {
                    object liLeftMargin = this.ViewState["LiLeftMargin"];
                    if (liLeftMargin != null)
                        return Convert.ToString(liLeftMargin);
                    return string.Empty;

                }
                set
                {
                    this.ViewState["LiLeftMargin"] = value;
                }
            }
        }
        #endregion
        
        #region Overrides
        protected override void CreateChildControls()
        {
            if (!this.ChildControlsCreated)
            {
                CreateMenu();
                base.CreateChildControls();
                ChildControlsCreated = true;
            }
        }
        #endregion

        #region Utilities

        protected void CreateMenu()
        {
            List<Category> breadCrumb = null;
            var currentCategory = this.CategoryService.GetCategoryById(CommonHelper.QueryStringInt("CategoryId"));
            if (currentCategory == null)
            {
                var product = this.ProductService.GetProductById(CommonHelper.QueryStringInt("ProductId"));
                if (product != null)
                {
                    var productCategories = product.ProductCategories;
                    if (productCategories.Count > 0)
                    {
                        currentCategory = productCategories[0].Category;
                    }
                }
            }

            if (currentCategory != null)
                breadCrumb = this.CategoryService.GetBreadCrumb(currentCategory.CategoryId);
            else
                breadCrumb = new List<Category>();

            // C47 Mod Begin
            var RootCategory = this.CategoryService.GetCategoryById(RootCategoryID);
            string RootCatName = string.Empty;
            if (RootCategory != null)
            {
                RootCatName = RootCategory.LocalizedName;
            }
            else
            {
                RootCatName = GetLocaleResourceString("Category.Categories");
            }
            phRootCategoryName.Controls.Add(new LiteralControl(RootCatName));
            CreateChildMenu(breadCrumb, RootCategoryID, currentCategory, 0);
            //CreateChildMenu(breadCrumb, 0, currentCategory, 0);
            // C47 Mod End
        }

        protected int GetNumberOfProducts(Category category, bool includeSubCategories)
        {
            int numberOfProducts = 0;
            var products = this.ProductService.GetAllProducts(category.CategoryId,
                        0, 0, null, null, null, string.Empty, false, 1, 0,
                        null, ProductSortingEnum.Position, out numberOfProducts);

            if (includeSubCategories)
            {
                var subCategories = this.CategoryService.GetAllCategoriesByParentCategoryId(category.CategoryId);
                foreach (var subCategory in subCategories)
                {
                    int tmp1 = GetNumberOfProducts(subCategory, includeSubCategories);
                    numberOfProducts += tmp1;
                }
            }
            return numberOfProducts;
        }

        protected void CreateChildMenu(List<Category> breadCrumb, int rootCategoryId, Category currentCategory, int level)
        {
            int padding = level++ * 15;
            foreach (var category in this.CategoryService.GetAllCategoriesByParentCategoryId(rootCategoryId))
            {
                var link = new NopCommerceLi();
                phCategories.Controls.Add(link);

                string categoryURL = SEOHelper.GetCategoryUrl(category);
                if (currentCategory != null && currentCategory.CategoryId == category.CategoryId)
                    link.CssClass = "active";
                else
                    link.CssClass = "inactive";
                link.HyperLink.NavigateUrl = categoryURL;
                string catName = string.Empty;
                if (this.SettingManager.GetSettingValueBoolean("Display.Products.ShowCategoryProductNumber"))
                {
                    //display category name with assigned products number
                    int numberOfProducts = GetNumberOfProducts(category, this.SettingManager.GetSettingValueBoolean("Display.Products.ShowCategoryProductNumber.IncludeSubCategories"));
                    catName = string.Format("{0} ({1})", category.LocalizedName, numberOfProducts);
                }
                else
                {
                    //display only category name
                    catName = category.LocalizedName;
                }
                link.HyperLink.Text = Server.HtmlEncode(catName);
                if (padding > 0)
                    link.LiLeftMargin = padding.ToString();

                for (int i = 0; i <= breadCrumb.Count - 1; i++)
                    if (breadCrumb[i].CategoryId == category.CategoryId)
                        CreateChildMenu(breadCrumb, category.CategoryId, currentCategory, level);
            }
        }
        
        #endregion
    }
}



Then replace the code in CategoryNavigation.ascx with:


<%@ Control Language="C#" AutoEventWireup="true"
    Inherits="NopSolutions.NopCommerce.Web.Modules.CategoryNavigation" Codebehind="CategoryNavigation.ascx.cs" %>
<div class="block block-category-navigation">
    <div class="title">
        <asp:PlaceHolder runat="server" ID="phRootCategoryName" EnableViewState="false" />
    </div>
    <div class="clear"></div>
    <div class="listbox">
        <ul>
            <asp:PlaceHolder runat="server" ID="phCategories" EnableViewState="false" />
        </ul>
    </div>
</div>




Last step is to modify your master page/s (threecolumn.master) by modding the code as below:


            <nopCommerce:CategoryNavigation ID="ctrlCategoryNavigation" runat="server" />
            <div class="clear">
            </div>
or
            <nopCommerce:CategoryNavigation ID="ctrlCategoryNavigation1" runat="server" RootCategoryID="30" />
            <div class="clear">
            </div>
            <nopCommerce:CategoryNavigation ID="ctrlCategoryNavigation2" runat="server" RootCategoryID="34"/>
            <div class="clear">
            </div>
            <nopCommerce:CategoryNavigation ID="ctrlCategoryNavigation3" runat="server" RootCategoryID="43"/>
            <div class="clear">
            </div>


Hope this helps.

Grant
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.