Child Categories and Brands

Store UI, layout, design, look and feel; Discussion on the customer facing pages of your online store. Cascading Style Sheets, Themes, Scriptlets, NVelocity and the components in the ConLib directory.
Post Reply
User avatar
troutlet
Lieutenant (LT)
Lieutenant (LT)
Posts: 77
Joined: Mon Sep 24, 2007 3:01 am
Location: USA
Contact:

Child Categories and Brands

Post by troutlet » Sun Oct 14, 2007 11:29 am

My goal is to have a single drop down from the header that shows all categories with their children expanded beneath them. So far I've taken the SimpleCategoryList and customized a version of it that displays the drop down the way that I want but I'm stuck trying to get at the child categories. I also tried customizing a version of the CategorySearchSidebar in order to achieve this but I was unsuccessful. Is there something easy that I can add to SimpleCategoryList to get what I need?

Also, can you give me an example of what a link to search by one particular Brand might look like?

The more I get into this cart the more I really like it!

User avatar
Shopping Cart Admin
AbleCommerce Admin
AbleCommerce Admin
Posts: 3055
Joined: Mon Dec 01, 2003 8:41 pm
Location: Vancouver, WA
Contact:

Post by Shopping Cart Admin » Sun Oct 14, 2007 4:03 pm

Hello,

The link for the manufacture is on all of the product pages, so I can get that. Someone more technical will have to follow up with you on the other question.

Code: Select all

http://localhost/ac7/Search.aspx?m=2
Thanks for your support

Shopping Cart Guru
AbleCommerce.com
Follow us on Facebook

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

Post by Logan Rhodehamel » Mon Oct 15, 2007 11:24 am

There was a similar feature in one of our admin pages. I modified it to work on the retail side:

Create a new file in your ConLib\Custom folder, named CategoryDropDownList.ascx. Paste this into the contents of the file:

Code: Select all

<%@ Control Language="C#" ClassName="CategoryDropDownList" EnableViewState="false" %>
<%--
<conlib>
<summary>Displays a drop down list of categories in your store.</summary>
<param name="HomeText" default="Home">The string to use for the top level item that directs to your home page.</param>
<param name="Prefix" default=" . . ">The string to place before a subdirectory, repeated for each category level.</param>
<param name="Levels" default="0">The maximum number of category levels to include in the list.  Set to 0 for all levels.</param>
<param name="GoButtonText" default="">The text for the button that when clicked, will display the selected category.  If you set this to an empty string, the button will be hidden.</param>
<param name="AutoPostBack" default="true">If true, the page will automatically redirect to the category when selected.  If false, the button must be clicked to navigate to the selected category.</param>
<param name="CacheDuration" default="1440">Number of minutes the category list will remain cached in memory.  Set to 0 to disable caching.</param>
</conlib>
--%>
<script runat="server">
    string _HomeText = "Home";
    public string HomeText
    {
        get { return _HomeText; }
        set { _HomeText = value; }
    }

    string _Prefix = " . . ";
    public string Prefix
    {
        get { return _Prefix; }
        set { _Prefix = value; }
    }

    int _Levels = 0;
    public int Levels
    {
        get { return _Levels; }
        set { _Levels = value; }
    }

    bool _AutoPostBack = true;
    public bool AutoPostBack
    {
        get { return _AutoPostBack; }
        set { _AutoPostBack = value; }
    }

    string _GoButtonText = "";
    public string GoButtonText
    {
        get { return _GoButtonText; }
        set { _GoButtonText = value; }
    }

    int _CacheDuration = 1440;
    public int CacheDuration
    {
        get { return _CacheDuration; }
        set { _CacheDuration = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        InitializeCategoryList();
        CategoryList.AutoPostBack = _AutoPostBack;
        if (string.IsNullOrEmpty(_GoButtonText))
        {
            GoButton.Visible = false;
        }
        else
        {
            GoButton.Text = _GoButtonText;
        }
    }

    private void InitializeCategoryList()
    {
        //ADD TOP LEVEL ITEM FOR ROOT
        ListItem item = new ListItem(_HomeText, "0");
        CategoryList.Items.Add(item);
        //ADD CATEGORY TREE
        ListItem[] categoryItems = GetCategoryListItems();
        if (categoryItems != null) CategoryList.Items.AddRange(categoryItems);
        //SELECT THE CORRECT INITIAL CATEGORY
        int categoryId = PageHelper.GetCategoryId();
        ListItem selected = CategoryList.Items.FindByValue(categoryId.ToString());
        if (selected != null) CategoryList.SelectedIndex = CategoryList.Items.IndexOf(selected);
    }

    private ListItem[] GetCategoryListItems()
    {
        string cacheKey = "68B9A666401D411E8FEFC9BA96CB2FF2";
        ListItem[] categoryList = null;
        if (_CacheDuration > 0)
        {
            //LOOK IN CACHE FOR LIST ITEMS
            CategoryListCacheWrapper categoryListWrapper = Cache.Get(cacheKey) as CategoryListCacheWrapper;
            if ((categoryListWrapper != null) && (categoryListWrapper.Prefix == _Prefix) && (categoryListWrapper.Levels == Levels))
            {
                categoryList = categoryListWrapper.GetCategoryList();
            }
        }
        if (categoryList == null)
        {
            int startLevel = (_HomeText.Length > 0 ? 1 : 0);
            categoryList = GetCategoryListItemsRecursive(0, startLevel);
            if (_CacheDuration > 0)
            {
                CategoryListCacheWrapper categoryListWrapper = new CategoryListCacheWrapper(_Prefix, _Levels, categoryList);
                Cache.Insert(cacheKey, categoryListWrapper, null, DateTime.Now.AddMinutes(_CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
            }
        }
        return categoryList;
    }

    private ListItem[] GetCategoryListItemsRecursive(int categoryId, int level)
    {
        List<ListItem> subcatList = new List<ListItem>();
        CategoryCollection subcategories = CategoryDataSource.LoadForParent(categoryId, true);
        foreach (Category subcat in subcategories)
        {
            string prefix = new string(' ', level).Replace(" ", Server.HtmlDecode(_Prefix));
            subcatList.Add(new ListItem(prefix + subcat.Name, subcat.CategoryId.ToString()));
            ListItem[] subcatItems = GetCategoryListItemsRecursive(subcat.CategoryId, level + 1);
            if (subcatItems != null) subcatList.AddRange(subcatItems);
        }
        if (subcatList.Count == 0) return null;
        return subcatList.ToArray();
    }

    private class CategoryListCacheWrapper
    {
        private string _Prefix;
        public string Prefix { get { return _Prefix; } }
        private int _Levels;
        public int Levels { get { return _Levels; } }
        public ListItem[] _CategoryList;
        public CategoryListCacheWrapper(string prefix, int levels, ListItem[] categoryList)
        {
            _Prefix = prefix;
            _Levels = levels;
            _CategoryList = categoryList;
        }
        public ListItem[] GetCategoryList()
        {
            return _CategoryList;
        }
    }

    protected void CategoryList_Changed(object sender, EventArgs e)
    {
        CategoryRedir();
    }

    protected void GoButton_Click(object sender, EventArgs e)
    {
        CategoryRedir();
    }

    private void CategoryRedir()
    {
        int categoryId = AlwaysConvert.ToInt(CategoryList.SelectedValue);
        if (categoryId == 0) Response.Redirect(NavigationHelper.GetHomeUrl());
        Category category = CategoryDataSource.Load(categoryId);
        if (category != null) Response.Redirect(category.NavigateUrl);
    }
</script>
<ajax:UpdatePanel ID="CategoryUpdatePanel" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="CategoryList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CategoryList_Changed">
        </asp:DropDownList>
        <asp:LinkButton ID="GoButton" runat="server" SkinID="Button" Text="Go" OnClick="GoButton_Click"></asp:LinkButton>
    </ContentTemplate>
</ajax:UpdatePanel>
Save the file, then place this in your scriptlet where you want the list to appear:

[[ConLib:Custom\CategoryDropDownList HomeText="Home" Prefix=" . . " Levels="0" GoButtonText="" AutoPostBack="true" CacheDuration="1440"]]

There is a minor bug in the ConLib help reference, it will not show you the help text for gobutton because the default text is an empty string. You can adjust the parameters as needed.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

User avatar
troutlet
Lieutenant (LT)
Lieutenant (LT)
Posts: 77
Joined: Mon Sep 24, 2007 3:01 am
Location: USA
Contact:

Alright!

Post by troutlet » Mon Oct 15, 2007 11:50 am

Thanks Logan! I can definitely work with that. I should have looked in the admin for a similar control.

User avatar
Jinx
Lieutenant (LT)
Lieutenant (LT)
Posts: 56
Joined: Thu Sep 28, 2006 8:09 pm
Location: Omaha, NE
Contact:

Changing Drop Down For Tree Level Navigation

Post by Jinx » Tue Jan 08, 2008 3:22 pm

Logan,
Is there a way to modify this so I can replace the SimpleCategoryList file for category display for my home, category and product pages on the left sidebar. There is another thread early on that talks about adding child navigation for display on the category display but it uses Ajax with some pretty cool drop down stuff but I would like to have a category navigation component that works just like the Able 5.5 version had due to upgrade issues. Any help would be appreciated.

Thanks,
Chris

lab_n_chemicals
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 33
Joined: Mon Mar 10, 2008 12:18 am
Location: us
Contact:

Re: Child Categories and Brands

Post by lab_n_chemicals » Sun Apr 20, 2008 4:38 am

Hi Logan,

I've set up my category tree:
Lab
..VendorID (multiple)
....Category1
......Category1_Group1 (each of these groups contain products)
......Category1_Group2 (etc.)
....Category2 (etc.)
......Category2_Group1 (each of these groups contain products)
......Category2_Group2 (etc.)
Chemical
.. (Same structure as above)
Educational
.. (Same structure as above

I need 3 dropdown listboxes - Lab, Chemical, Ed - each listing just the Category names sorted alphabetically without the VendorID and the Category_Groups.
The SQL would look like this:

SELECT esp.ac_Categories.Name,esp.ac_CategoryParents.CategoryId
FROM esp.ac_CategoryParents INNER JOIN
esp.ac_Categories ON esp.ac_CategoryParents.CategoryId = esp.ac_Categories.CategoryId
WHERE
(esp.ac_CategoryParents.ParentLevel = 1) AND
(esp.ac_CategoryParents.ParentNumber = 2) AND
(esp.ac_CategoryParents.ParentId = ParentID)
ORDER BY
esp.ac_Categories.Name

Lab ParentID = 1; Chemicals ParentID = 2; Educational ParentID = 3
Also need the breadcrumbs to not include VendorID.
Tried hiding the VendorID but then everything under it disappears too.
Can you help?

Thanks a bunch for all of your posts and hard work.
Lab supplies, consumables & chemicals
http://www.espchemicals.com

Post Reply