Template Tricks with CMSMS Products Module

There are many great modules for CMS Made Simple. One of those is the Products module, part of Calguy’s eCommerce suite. However, there are some downsides in the template structure, so I want to quickly show you how some long unanswered forum post can suddenly become an answer with a single line of code.

The problem I was facing a few days ago was how could I possibly add “active” class to Products hierarchy navigation and Parent Hierarchy.
If you have ever worked with Products module, you know that the Hierarchy Report Template is not really the best solution as it lacks of some needed variables support like MenuManager provides.

So the default Products install Hierarchy Report Template looks like this:

{* hierarchy report template *}
{if !isset($hdepth) && isset($hierarchy_item)}
<h3>Hierarchy Data for {$hierarchy_item.name} ({$hierarchy_item.id})</h3>
{/if}
{if !isset($hdepth)}{assign var='hdepth' value='0'}{/if}
{*
// create a nested set of unordered lists
// if the active_hierarchy smarty variable exists
// and matches the current hierarchy id
// the active class will be given
// to the ul.  You may want to modify your summary template
// to set this variable
*}
<ul class="products_hierarchy_level{$hdepth}">
{foreach from=$hierdata key='key' item='item'}
{strip}
<li {if isset($active_hierarchy) and $item.id == $active_hierarchy}{/if}>
{if $item.count gt 0}
<a href="{$item.url}">{$item.name} ({$item.count})</a>
{else}     {$item.name} ({$item.count})
{/if}
{if isset($item.children) }
{* there are children call this template again *}
{include file=$smarty.template hierdata=$item.children hdepth=$hdepth+1}
{/if}
</li>
{/strip}
{/foreach}
</ul>

As you can see, the structure is not the best. Not even semantic markup, such as Subhierarchy items, are simply included within the same template, meaning the whole template repeats again and we end up with empty <ul> </ul> and other not so fancy markups.
Like I mentioned above, I was in need for “active” class on the parent item, but how? After banging my head against the wall for around two hours, I came up with a solution that now seems so simple. It is actually a single line of code in your Detail or Summary Product’s template.

For Detail Template:

{assign var='get_hierarchy_info' value=$Products->GetHierarchyInfo($entry->hierarchy_id)}

For Summary Template:

{assign var='get_hierarchy_info' value=$Products->GetHierarchyInfo($entry[0]->hierarchy_id)}

Now you have a variable $get_hierarchy_info, which pulls information from our hierarchy based on the current Product.
This variable actually now gives us answers to questions like, “How can I show the current Hierarchy name on my page?”, “How can I give active class to parent hierarchy or different class then child Hierarchy?”, “How can I show the title of the parent Hierarchy in summary view?” and probably some more.

To see which parameters you can access, simply by using print_r modifier:

<pre>{$get_hierarchy_info|@print_r}</pre>

And the result you should see is something like this:

Array
(
    [id] => 4
    [name] => Current Hierarchy
    [parent_id] => 1
    [hierarchy] => 00001.00009
    [image] =>
    [long_name] => Parent Hierarchy | Current Hierarchy
    [description] =>
    [extra1] =>
    [extra2] =>
)

And you can access these, for example, if you want to show Hierarchy name in your Detail template with {$get_hierarchy_info.name}.

Now, back to my menu problem. Before the above solution can be used in other templates, we need to assign a variable to the value we need.
For example, in the Detail template, I had to add the following:

{assign var='get_hierarchy_info' value=$Products->GetHierarchyInfo($entry->hierarchy_id)}
{assign var='parent' value=$get_hierarchy_info.parent_id} {* get parent id and assign $parent var *}
{assign var=active_hierarchy value=$entry->hierarchy_id} {* get current id and assign $active_hierarchy *}

With the above code, we have two variables available: {$parent} which I can use to add class to the parent Hierarchy and {$active_hierarchy} which I can use for the current, active Hierarchy page.
All we need to do now is put our Hierarchy menu together:

{if !isset($hdepth)}{assign var='hdepth' value='0'}{/if}
{if $hdepth == '0'}{if it's level one show *}     
  <ul id="menu">
{/if}
{foreach from=$hierdata key='key' item='item'}
{strip} 
    <li class="item{if $hdepth == '0'} parent{/if}{if isset($active_hierarchy) and $item.id == $active_hierarchy} active{/if}{if isset($parent) && $item.id == $parent} activeparent{/if}"> 
    {if $item.count gt 0}     
      <a href="{$item.url}">{$item.name} ({$item.count})</a> 
    {else}     
      <a href="{$item.url}">{$item.name}</a> 
    {/if}   
    {if isset($item.children)}{* if it's child build template *}  
      <ul>   
       {include file=$smarty.template hierdata=$item.children hdepth=$hdepth+1}   
      </ul> 
    {/if}   
    </li>{* close parent *}
{/strip}
{/foreach}
{if $hdepth == '0'}</ul>{/if}

With this Hierarchy template, we get a nice markup like this:

<ul id="menu">
  <li class="item"><a href=#">Hierarchy</li>
  <li class="item activeparent"><a href=#">Hierarchy (Child is active)</a>
  <ul>
    <li class="item"><a href="#">Sub Hierarchy</a></li>
    <li class="item active"><a href="#">Sub Hierarchy active</a></li>
  </ul>
  </li>
</ul>

And that’s it! I hope I have helped you in your next eCommerce quest with CMSMS and the Products module.

Looking for CMS Made Simple Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under CMS Made Simple | RSS 2.0

Author Spotlight

Goran Ilic

Goran Ilic

Goran Ilic is a designer, all-rounder, husband, father, and founder of a CMS Made Simple related blog called: I do this (www.i-do-this.com). After long search for a perfect tool for his clients he has found CMSMS and devoted his passion to it.

8 Comments on Template Tricks with CMSMS Products Module

  1. Chris says:

    This helped immensely.
    I did notice one error though.

    For the summary template, it should be:
    {assign var=’get_hierarchy_info’ value=$Products->GetHierarchyInfo($items[0]->hierarchy_id)}

    Instead of:
    {assign var=’get_hierarchy_info’ value=$Products->GetHierarchyInfo($entry[0]->hierarchy_id)}

  2. Stephen Heck says:

    This post was helpful. I have a question though can’t find it anywhere. Is there a way to have the item count for the parent include all the child item counts. Right now i just get a 0 for parent and the submenus have there own counts.

    Any help would be appreciated thanks.
    steve

  3. Mark van Kuijk says:

    Is it possible to have access to the summary template code (product id’s or something) in the detail template so you can show other products (thumbs for examples)?

  4. Goran Ilic says:

    Sorry Stephen, as far i know by default only items from each hierarchy are counted and as you can see there is no real parent -> child tree.
    In theory you could maybe achieve it by using {math} where you would count all values from subhierarchy and display it for parent.
    How exactly, i have no idea didn’t play with that yet ;-)

  5. Luke says:

    Hi Goran,

    Thanks for this post. I was also banging my head against the wall untill I came across this post and in particular, this line:

    {assign var=’get_hierarchy_info’ value=$Products->GetHierarchyInfo($entry->hierarchy_id)}

    Is it documented anywhere what methods we can use on $Products? Or how can I find it in the code?

    Cheers,

    Luke

  6. airplain says:

    What do I need for modules and CSS to get this working?

  7. Ingrid says:

    Hi Goran,

    Is there also a way to show the products in the hierarchy. That’s what I’m strugling with at the moment.

    Regards,
    Ingrid

  8. Ken Griffith Ken Griffith says:

    @Ingrid, here is how I put the products in the Hierarchy. Simply put this tag at the end of your hierarchy template.

    {Products hierarchy=$hierarchy_item.name}

Leave a Reply

Your email address will not be published. Required fields are marked *


8 + = 12

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>