I've been struggling to find much info on here for modifying the categories menu to display subcategories on xcart 4.2.0.
I had 3 different requests.
The first was probably the most common, which was that the client wanted the subcategories to appear as you clicked each category and to remain whilst they looked at products.
The Second was to display the categories and subcategories all the time
The Thirs was to display the categories and subcategories all the time but not be able to click on the categories (in effect they are just headings)
To do this I used some code from on here for older versions and made some code up.
For any of these you first need to let the pages have access to the subcategories. This is done by editing the file "include/categories.php" look for the following line:
Code:
if (!empty($subcategories)) {
and just before it add the following block of code:
Code:
function func_getallsubcat(){
$a =func_get_categories_list("", true, "all");
$b=$a['all_categories'];
$c=array();
foreach ($b as $k=>$v){
if($v['parentid']!="0")
$c[$v['parentid']][]=$v;
}
return $c;
}
$smarty->assign("allsubcategories", func_getallsubcat());
Once that has been added you just need to alter the customer/categories.tpl template file to tell it to use the new variable.
For the first method I wrote the following code:
Code:
{capture name=menu}
{if $active_modules.Fancy_Categories}
{include file="modules/Fancy_Categories/categories.tpl"}
{assign var="additional_class" value="menu-fancy-categories-list"}
{else}
{assign var=thiscat value=$cat}
{assign var=par value=0}
{foreach from=$categories item=cats}
{assign var=Mcatid value=$cats.categoryid}
{foreach from=$allsubcategories.$Mcatid item=subb}
{if $subb.categoryid eq $thiscat}
{assign var=par value=$subb.parentid}
{/if}
{/foreach}
{/foreach}
<ul>
{foreach from=$categories_menu_list item=c}
<li><a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}">{$c.category}</a></li>
{if $c.categoryid eq $par}
{foreach from=$allsubcategories.$par item=sub}
{if $sub.parentid eq $c.categoryid}
<a href="home.php?cat={$sub.categoryid}" class="subMenuItem">{$sub.category}</a>
{/if}
{/foreach}
{/if}
{foreach from=$allcategories item=cat}
{if $cat.parentid eq $c.categoryid}
<a href="home.php?cat={$cat.categoryid}" class="subMenuItem">{$cat.category}</a>
{/if}
{/foreach}
{/foreach}
</ul>
{assign var="additional_class" value="menu-categories-list"}
{/if}
{/capture}
{include file="customer/menu_dialog.tpl" title=$lng.lbl_categories content=$smarty.capture.menu}
For the second method I instead used the following code:
Code:
{capture name=menu}
{if $active_modules.Fancy_Categories}
{include file="modules/Fancy_Categories/categories.tpl"}
{assign var="additional_class" value="menu-fancy-categories-list"}
{else}
{assign var=thiscat value=$cat}
{assign var=par value=0}
{foreach from=$categories item=cats}
{assign var=Mcatid value=$cats.categoryid}
{foreach from=$allsubcategories.$Mcatid item=subb}
{if $subb.categoryid eq $thiscat}
{assign var=par value=$subb.parentid}
{/if}
{/foreach}
{/foreach}
<ul>
{foreach from=$categories_menu_list item=c}
<li><a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}">{$c.category}</a></li>
{assign var=in value=$c.categoryid}
{foreach from=$allsubcategories.$in item=cat}
{if $cat.parentid eq $c.categoryid}
<a href="home.php?cat={$cat.categoryid}" class="subMenuItem">{$cat.category}</a>
{/if}
{/foreach}
{/foreach}
</ul>
{assign var="additional_class" value="menu-categories-list"}
{/if}
{/capture}
{include file="customer/menu_dialog.tpl" title=$lng.lbl_categories content=$smarty.capture.menu}
And the third method was very similar to the second method and used the following code:
Code:
{capture name=menu}
{if $active_modules.Fancy_Categories}
{include file="modules/Fancy_Categories/categories.tpl"}
{assign var="additional_class" value="menu-fancy-categories-list"}
{else}
{assign var=thiscat value=$cat}
{assign var=par value=0}
{foreach from=$categories item=cats}
{assign var=Mcatid value=$cats.categoryid}
{foreach from=$allsubcategories.$Mcatid item=subb}
{if $subb.categoryid eq $thiscat}
{assign var=par value=$subb.parentid}
{/if}
{/foreach}
{/foreach}
<ul>
{foreach from=$categories_menu_list item=c}
<li>{$c.category}</li>
{assign var=in value=$c.categoryid}
{foreach from=$allsubcategories.$in item=cat}
{if $cat.parentid eq $c.categoryid}
<a href="home.php?cat={$cat.categoryid}" class="subMenuItem">{$cat.category}</a>
{/if}
{/foreach}
{/foreach}
</ul>
{assign var="additional_class" value="menu-categories-list"}
{/if}
{/capture}
{include file="customer/menu_dialog.tpl" title=$lng.lbl_categories content=$smarty.capture.menu}
Hopefully that will help someone else on version 4.2. I've not extensively tested it yet but it seemed to work fine. I only needed to use it with one level of subcategories so I've no idea if it will work with multiple levels. - I doubt it though!
I made a style up which I put at the end of the main.css file - you can make your own up but here is mine:
Code:
a.subMenuItem{
display:block;
margin-left:23px;
text-decoration:none;
}
a.subMenuItem:hover{
text-decoration:underline;
}
*update* I just tried adding a third level category ( a sub-subcategory ) and as I expected it won't show on the menu but will operate fine but the third level will show in the middle like the basic x-cart way does.