Question

In my XCart 4.4.2 installation, I have a few major categories of products, each containing several subcategories. On the home page, I'd like to list the subcategories within each category but am having trouble accessing the subcategories from welcome.tpl from within this code:

{foreach from=$categories_menu_list item=c name=categories}
  <a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}">
    <li>
        <img src="{$c.image_path|amp}" alt="{$c.category|escape}"/>
        <strong>{$c.category}</strong><br/>

        <!-- list subcategories here-->
        {php}
          $parentid = $c.categoryid;

          $categoryNames = func_query_column("SELECT category FROM $sql_tbl[categories] WHERE parentid = " . $parentid);
          print_r($categoryNames);
        {/php}
    </li>
  </a>
{/foreach}

Could anybody help me with the PHP/SMARTY code needed to generate lists of subcategories? Thanks!

Was it helpful?

Solution

It will be better to define an array of subcategories in php script rather than do it in template. I can provide you with the following solution for your task:

Patch for include/common.php

@@ -90,6 +90,14 @@
         // Get categories menu data
         if (!empty($categories)) {
             $smarty->assign('categories_menu_list', $categories);
+
+            if (!isset($cat) || 0 == intval($cat)) {
+                $extended_categories = func_get_categories_list(0, true, true, 1);
+
+                if (!empty($extended_categories)) {
+                    $smarty->assign('extended_categories_list', $extended_categories);
+                }
+            }
         }

         if ($active_modules['Manufacturers']) {

(lines marked with + should be added to the code)

skin/your-skin/customer/main/welcome.tpl

{foreach from=$categories_menu_list item=c}
<a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}">
  <li>
  <img src="{$c.image_path|amp}" alt="{$c.category|escape}"/>
          <strong>{$c.category}</strong><br/>
    <!-- list subcategories here-->
    {foreach from=$extended_categories_list item=ec}
      {if $ec.parentid eq $c.categoryid}{$ec.category|escape}<br />{/if}
    {/foreach}
  </li>
</a>
{/foreach}

OTHER TIPS

classics answer didn't quite work for me. Here's what I used:

    // Get categories menu data
    if (!empty($categories)) {
        $smarty->assign('categories_menu_list', $categories);

        foreach($categories as $c){
            $ext_cats = func_get_categories_list($c['categoryid'], true, true, 1);
            if(!empty($ext_cats)){
                $extended_categories[$c['categoryid']] = $ext_cats;
            }
            $ext_cats = "";
        }

        if (!empty($extended_categories)) {
            $smarty->assign('extended_categories_list', $extended_categories);
        }   
    }

This cycles through each category and gets all the sub categories.

I know this is an old thread, but classic's code worked great for me, with some slight modification (to make the subcategories link). What can I do to get level-3+ subcategories (sub-sub-categories) to show up as well?

Apologies for posting in the wrong spot. To make up for it, I do actually have an addition to classic's code. The original code just listed the subcategories as text/images, below is what I used to get rid of the pictures, but make the subcategories click-able links

    <ul>
    {foreach from=$categories_menu_list item=c}
    <li><a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}"><strong>{$c.category}</strong></a></li>

    <!-- list subcategories here-->
    {foreach from=$extended_categories_list item=ec}
    {if $ec.parentid eq $c.categoryid}<li><a href="home.php?cat={$ec.categoryid}" style="font-size:10px;">{$ec.category|escape}</a></li>{/if}
    {/foreach}

    {/foreach}
    </ul>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top