News:

Looking for documentation? Take a look on our wiki

Main Menu

module VM - Category only shows 2nodes not the 3de one.

Started by janis, January 22, 2012, 17:36:50 PM

Previous topic - Next topic

designfire_de

I had the same problem in a customers project yesterday.

So here is my solution that gives you the following features:

  • Unlimited category levels
  • All active categories (and their parent categories) have the css class "active"
  • All categories with child categories have the css class "parent"
  • You don't have to modifiy any Virtuemart core module files (so an Virtuemart update won't delete your changes)
  • Attention: This solution might be slow on sites with a huge amount of nested categories
  • Attention: This solution only works, if you select "all" on the "Layout" config field

What to do:

  • Create a folder named "html" in your template folder if it doesn't already exist (i.e. /templates/beez5/html)
  • Create a folder named "mod_virtuemart_category" inside this folder
  • Copy the files "all.php" and "index.html" from "/modules/mod_virtuemart_category/tmpl" into this folder
  • Open the copied "all.php"
  • Delete everything after the first occurrence of "?>"
  • Insert the following lines before the first occurrence of "?>":

echo printCategories($categories, $parentCategories, $class_sfx, $cache, $vendorId);

function printCategories($categories, $parentCategories, $class_sfx, $cache, $vendorId)
{
$html = '<ul class="menu'.$class_sfx.'" >';
foreach($categories as $category)
{
$category->childs = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $category->virtuemart_category_id );

$caturl = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$category->virtuemart_category_id);
$cattext = $category->category_name;
$active_menu = in_array( $category->virtuemart_category_id, $parentCategories) ? 'active' : '';

$html .= '<li class="'.$active_menu.($category->childs ? ' parent' : '').'">
<div>'.JHTML::link($caturl, $cattext.($category->childs ? '<b class="caret"></b>' : '')).'</div>';

if($category->childs)
{
$html .= printCategories($category->childs, $parentCategories, $class_sfx, $cache, $vendorId);
}

$html .= '</li>';
}

$html .= '</ul>';

return $html;
}


This code will simply loop over all categories, create the category list element, check if there are category children and recursively do the same with all the children.

If you find any errors, please feel free to correct this solution!

Best regards,
Jonathan

vladivlad007

 It did not work for me  :(
joomla 2.5.9. VM 2.0.18

Quote from: patrickit on February 06, 2012, 05:37:46 AM
Hi,

@BanquetTables.pro - using the joomla menu system - wouldn't that be fixed? What if we add new categories/sub categories to it?

@Janis There is a module but it didn't work in my vm2.0, if you have an earlier version of vm it may work - http://extensions.joomla.org/extensions/extension-specific/virtuemart-extensions/virtuemart-categories/9902

If it doesn't, and since there are no modules then I have a temporary workaround hack for you. Note that my workaround goes 1 extra level deep (3rd node), you can customize it for more nodes if required.
You can see a working demo here http://x-doria.ae/
Hope it helps!

go to modules/mod_virtuemart_category/mod_virtuemart_category.php
find  (around line 46)
foreach ($categories as $category) {

      $category->childs = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $category->virtuemart_category_id );
add after it
//PL temp hack show 3rd node
      if($category->childs){
         foreach($category->childs as $child){
            $child->childs = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $child->virtuemart_category_id );
         }
      }
      //PL end

go to modules/mod_virtuemart_category/tmpl/all.php
find  (around line 32)
<div ><?php echo JHTML::link($caturl, $cattext); ?></div>

add after it
<?php if ($child->childs ) {   //PL temp hack show 3rd node
   ?>
      <ul class="menu<?php echo $class_sfx; ?>">
      <?php
         foreach ($child->childs as $grandchild) {      
            $caturl = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$grandchild->virtuemart_category_id);
            $cattext = $grandchild->category_name;
            ?>
      <li>
         <div ><?php echo JHTML::link($caturl, $cattext); ?></div>
      </li>
      <?php } ?>
      </ul>
   <?php    } //PL end ?>

VirtueMart.cz

Quote from: designfire_de on February 13, 2013, 09:21:57 AM
I had the same problem in a customers project yesterday.

So here is my solution that gives you the following features:

  • Unlimited category levels
  • All active categories (and their parent categories) have the css class "active"
  • All categories with child categories have the css class "parent"
  • You don't have to modifiy any Virtuemart core module files (so an Virtuemart update won't delete your changes)
  • Attention: This solution might be slow on sites with a huge amount of nested categories
  • Attention: This solution only works, if you select "all" on the "Layout" config field

What to do:

  • Create a folder named "html" in your template folder if it doesn't already exist (i.e. /templates/beez5/html)
  • Create a folder named "mod_virtuemart_category" inside this folder
  • Copy the files "all.php" and "index.html" from "/modules/mod_virtuemart_category/tmpl" into this folder
  • Open the copied "all.php"
  • Delete everything after the first occurrence of "?>"
  • Insert the following lines before the first occurrence of "?>":

echo printCategories($categories, $parentCategories, $class_sfx, $cache, $vendorId);

function printCategories($categories, $parentCategories, $class_sfx, $cache, $vendorId)
{
$html = '<ul class="menu'.$class_sfx.'" >';
foreach($categories as $category)
{
$category->childs = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $category->virtuemart_category_id );

$caturl = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$category->virtuemart_category_id);
$cattext = $category->category_name;
$active_menu = in_array( $category->virtuemart_category_id, $parentCategories) ? 'active' : '';

$html .= '<li class="'.$active_menu.($category->childs ? ' parent' : '').'">
<div>'.JHTML::link($caturl, $cattext.($category->childs ? '<b class="caret"></b>' : '')).'</div>';

if($category->childs)
{
$html .= printCategories($category->childs, $parentCategories, $class_sfx, $cache, $vendorId);
}

$html .= '</li>';
}

$html .= '</ul>';

return $html;
}


This code will simply loop over all categories, create the category list element, check if there are category children and recursively do the same with all the children.

If you find any errors, please feel free to correct this solution!

Best regards,
Jonathan

Hello everyone!

I took a designfire_de's solution that has many benefits and modify it for default current.php. So you have recursive "only view" modification which displays only the active categories and their children. Instructions are the same as all.php, just at the point 3. and 4. shall file current.php. Enjoy  8)

$class_sfx = 'VMmenu';
echo printCategories($categories, $parentCategories, $class_sfx, $ID, $cache, $vendorId);

function printCategories($categories, $parentCategories, $class_sfx, $ID, $cache, $vendorId, $control = true)
{
$html = '<ul class="'.$class_sfx.'" '.($control ? 'id="VMmenu'.$ID.'"' : '').'>';
foreach($categories as $category)
{
$category->childs = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $category->virtuemart_category_id );

$caturl = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$category->virtuemart_category_id);
$cattext = $category->category_name;
$active_menu = in_array( $category->virtuemart_category_id, $parentCategories) ? 'VmOpen' : 'VmClose';

$html .= '<li class="'.$active_menu.($category->childs ? ' parent' : '').'">
<div '.($active_menu == 'VmOpen' ? 'class="active"' : '').'>'.JHTML::link($caturl, $cattext).($category->childs ? '<span class="VmArrowdown"> </span>' : '').'</div>';

if ($active_menu=='VmOpen')
{
if($category->childs)
{
$class_sfx = 'menu';
$html .= printCategories($category->childs, $parentCategories, $class_sfx, $ID, $cache, $vendorId, false);
}
}
$html .= '</li>';
}

$html .= '</ul>';

return $html;
}


And here is CSS for change color of active categorie:

ul.VMmenu div.active a {
color: #FF1F1F;
}

Makis77