Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 07, 2014 @ 16:51
    Dennis Aaen
    0

    Keep selected on first category level

    Hi,

    I am working on my first ecommerce project using the uCommerce platform. I need to keep the first category get the class selected. So when I on one of the categories below, the level 1 category need to keep the class selected.

    My category structure is like this:

    Catalog
    -Category 1
    --Sub category
    ----Sub category
    ----Sub category
    --Sub category
    -Category 2

    With code I got now when I on Category 1 it´s gets the class selected. So what I am after if that when I on one of the other categories under Category 1 e.g the page I have mark as bold, then Category 1 should still have the class selected.

    The code I have so far looks like this:

    @using UCommerce.Api
    @using UCommerce.EntitiesV2
    @using UCommerce.Extensions
    @using UCommerce.Runtime

    @helper RenderNavigation(ICollection<Category> categories)
        {
     
        if (categories.Any()){
            <ul class="nav nav-list">
                @foreach (var category in categories.OrderBy(x => x.SortOrder)) {
                    var currentCategory = SiteContext.Current.CatalogContext.CurrentCategory;
                   
                    var css = "";
                 
                    if (currentCategory != null && currentCategory.CategoryId == category.CategoryId)
                    {
                        css = " class=\"selected\"";
                    }

                    <li @Html.Raw(css)>
                       
                        <span>               
                            <a href="@CatalogLibrary.GetNiceUrlForCategory(category)">@category.DisplayName()</a>
                         </span>
                        @RenderNavigation(category.Categories)
                   
                    </li>

                }
            </ul>
        }
    }

    @{
        var allowedProductCatalog = umbraco.cms.businesslogic.member.Member.GetCurrentMember().getProperty("allowedProductCatalog").Value;
        @RenderNavigation(CatalogLibrary.GetRootCategories(Convert.ToInt32(allowedProductCatalog)))
    }

    The Umbraco version I am using is Umbraco 4.11.10 and uCommerce 6.0.3.14141

    Any help would be very much appreciated

    /Dennis

  • Martin 181 posts 740 karma points
    Jul 07, 2014 @ 17:44
    Martin
    0

    Hi Dennis,

    You have to extend your if statement to also check if the category is a parent of your current category. I would write a helper that can check every category from current node to topnode. 

    It could be done in this way:

    public bool IsParentOf(Category parentCandidate, Category child)
    {
        var cat = child.Parent;
    
        while (cat != null)
        {
            if (cat == parentCandidate)
            {
                return true;
            }
            else
            {
                cat = cat.ParentCategory;
            }
        }
        return false;
    }  

    I have not tested the code but something similar should do it.

    If you have any questions just ask :)

    EDIT: 
    Then you extend this line:

    if(currentCategory !=null&& currentCategory.CategoryId== category.CategoryId)

    with:

    if(currentCategory !=null&& currentCategory.CategoryId== category.CategoryId || IsParentOf(category, currentCategory)) 

    Best regards
    Martin 

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 07, 2014 @ 20:30
    Dennis Aaen
    0

    Hi Martin,

    I have tried your code, but I get that the IsParentOf can not be resolve, so maybe I have placed helper method at the wrong place?

    My code looks like this now:

    @using UCommerce.Api
    @using UCommerce.EntitiesV2
    @using UCommerce.Extensions
    @using UCommerce.Runtime
    
    
    @helper bool IsParentOf(Category parentCandidate, Category child){
    
        var cat = child.ParentCategory;
    
        while (cat != null)
        {
            if (cat == parentCandidate)
            {
                return true;
            }
            else
            {
                cat = cat.ParentCategory;
            }
        }
        return false;
     }
    
    @helper RenderNavigation(ICollection<Category> categories){
    
        if (categories.Any()){
    
    
            <ul class="nav nav-list">
                @foreach (var category in categories.OrderBy(x => x.SortOrder)) {
                    var currentCategory = SiteContext.Current.CatalogContext.CurrentCategory;
    
                    var css = "";
    
    
                   if(currentCategory !=null&& currentCategory.CategoryId== category.CategoryId || IsParentOf(category, currentCategory)) 
                    {
                        css = " class=\"selected\"";
                    }
    
                    <li @Html.Raw(css)>
    
                        <span>                
                            <a href="@CatalogLibrary.GetNiceUrlForCategory(category)">@category.DisplayName()</a>
                         </span>
                        @RenderNavigation(category.Categories)
    
    
                    </li>
    
                }
            </ul>
        }
    }
    
    @{
        var allowedProductCatalog = umbraco.cms.businesslogic.member.Member.GetCurrentMember().getProperty("allowedProductCatalog").Value;
        @RenderNavigation(CatalogLibrary.GetRootCategories(Convert.ToInt32(allowedProductCatalog)))
    }
    

    Maybe you can spot what I´am doing wrong.

    /Dennis

  • Martin 181 posts 740 karma points
    Jul 09, 2014 @ 09:19
    Martin
    100

    Hi Dennis,

    I actually think that it should be a function instead of a helper. Take a look at this: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix

    Best regards
    Martin 

Please Sign in or register to post replies

Write your reply to:

Draft