News:

Looking for documentation? Take a look on our wiki

Main Menu

Modify Category Module [Count Products] [Hide empty Category]

Started by tobisagt, January 23, 2012, 16:41:56 PM

Previous topic - Next topic

tobisagt

Hey guys,

to manipulate my category tree I modified the template "current.php" in "/modules/mod_virtuemart_category/tmpl/".

In order to hide categories which have no products I need access to all products. But I cannot find these access. I see that "$categories" is used in order to iterate over all existing categories, but it is not initialised in this file. Do you know where it is initialised? Can you help me to get access to all products here?

Many thanks,
tobi

PRO

i believe its

models/category.php

how many categories have no products? if its just a few, You could just use in array, to skip them by listing them in current.php




tobisagt

#2
Quote from: BanquetTables.pro on January 23, 2012, 17:42:36 PM
i believe its

models/category.php

how many categories have no products? if its just a few, You could just use in array, to skip them by listing them in current.php

Thx, for your answer!

A friend helped me out to do it right.

Here's the code for all who want to add the following features to the VM Category Module. (Maybe someone can write a module for that?!)


  • Hide empty categorys
  • display number of products in category
  • add an extra class to "active" child categories

I cant give you detailed explanation about the code!

Just replace this code with the code in current.php in /mod_virtuemart_category/


<?php // no direct access
defined('_JEXEC') or die('Restricted access');
//JHTML::stylesheet ( 'menucss.css', 'modules/mod_virtuemart_category/css/', false );
$ID str_replace('.''_'substr(microtime(true), -88));

/* START MODIFICATION */

/**
* count the products in a category
*
* @author RolandD, Max Milbers
* @return array list of categories product is in
*/
function countProducts($cat_id=0) {

$db JFactory::getDBO();
$vendorId 1;
if (
$cat_id 0) {
$q 'SELECT count(#__virtuemart_products.virtuemart_product_id) AS total
FROM `#__virtuemart_products`, `#__virtuemart_product_categories`
WHERE `#__virtuemart_products`.`virtuemart_vendor_id` = "'
.(int)$vendorId.'"
AND `#__virtuemart_product_categories`.`virtuemart_category_id` = '
.(int)$cat_id.'
AND `#__virtuemart_products`.`virtuemart_product_id` = `#__virtuemart_product_categories`.`virtuemart_product_id`
AND `#__virtuemart_products`.`published` = "1" '
;
$db->setQuery($q);
$count $db->loadResult();
} else 
$count=;

return 
$count;
}

$showCount true;

/* END MODIFICATION */

?>




<ul class="VMmenu<?php echo $class_sfx ?>" ID="<?php echo "VMmenu".$ID ?>" >
<?php foreach ($categories as $category) {
   $active_menu 'class="VmClose"';
$count countProducts($category->virtuemart_category_id);
$caturl JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$category->virtuemart_category_id);
$cattext = ($showCount) ? $category->category_name." <span>(".$count.")</span>" :  $category->category_name;
//if ($active_category_id == $category->virtuemart_category_id) $active_menu = 'class="active"';
if (in_array$category->virtuemart_category_id$parentCategories)) $active_menu 'class="VmOpen"';

  /* START MODIFICATION */
 
  $visible false;
  if ($count == 0) {
   foreach ($category->childs as $child) {
    if (countProducts($child->virtuemart_category_id) > 0) {
     $visible true;
    }
   }
  }
  else {
   $visible true;
  }
 
  if ($visible) {
  /* END MODIFICATION */
 
?>


<li <?php echo $active_menu ?>>
<div class="active">
<?php echo JHTML::link($caturl$cattext);
if ($category->childs) {
  ?>

  <span class="VmArrowdown"> <span>
  <?php ?>
</div>
<?php if ($active_menu=='class="VmOpen"') { ?>
<ul class="menu<?php echo $class_sfx?>">
<?php
foreach ($category->childs as $child) {
$active_submenu '';

// MODIFIED "IF"
$count countProducts($child->virtuemart_category_id);
if ($count 0) {
$caturl JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$child->virtuemart_category_id);
$cattext = ($showCount) ? $child->category_name." <span>(".$count.")</span>" $child->category_name;
//if ($active_category_id == $category->virtuemart_category_id) $active_menu = 'class="active"';
if (in_array$child->virtuemart_category_id$parentCategories)) $active_submenu 'class="active"';
    ?>


<li>
<div <?php echo $active_submenu ?>><?php echo JHTML::link($caturl$cattext); ?></div>
    </li>
<?php
}
}
?>

</ul>
<?php ?>
</li>
<?php
 
}
?>

</ul>


If you dont want to count the number of products just change in Line 32

$showCount = true;

into

$showCount = false;

EDIT: Attachment added

[attachment cleanup by admin]

milimar

I did it and nothing happens. Maybe the template does not allow it? I use a free Virtuemart 2 template called ColorShop, and J2.5.3 and VM 2.0.2.

tobisagt

Quote from: milimar on April 04, 2012, 15:18:16 PM
I did it and nothing happens. Maybe the template does not allow it? I use a free Virtuemart 2 template called ColorShop, and J2.5.3 and VM 2.0.2.

Hi milimar,

im sry! I cant give more information than in my post above. As I said before, a friend helped me out.

You can search a bit in the forum. I think someone write a module that have this features.

pixelism

Hi Guys..

It does work!

I did the code paste and I too thought .. doh it doesn't work..! BUT ;)

After modifying the file -  current.php in /mod_virtuemart_category/tmpl

what you need to do is..

Go back to the Module Manager and go to the VM Category Module you are using.. then in the params for this module in the Layout drop down - select - Current ;)

I'm guessing you could possibly make a NEW file? with new name and then select this?? eg: instead save the new coded files as "numbered.php or something and this may possibly then appear in the drop down.. but for simplicity.. I simply replaced the code in orig current.php file with the code above.. you just have to then go to the module and select "current" in the drop down..

It will then work.. simple thing to miss and believe me it took a while for me to realise this..

All good and hope this helps... who-ever did the code.. thanks you're a life saver...

PP

EDIT: I just saved an additional file and uploaded it called numbered.php (which is a copy of the modified code as explained above) and it indeed does allow you to select this file named "numbered" and this works.. So that said you could then make another copy and have the additional selections of numbered with show empty cats and also one that is numbered without/not showing empty cats.. if you get me..





[attachment cleanup by admin]

tobisagt

You are right.

Sry. I forget to say that this will only work with the "current mode" of the module.

leFlea

using category model is a better solution, so that you wouldn't need to rewrite the function:

$catModel = VmModel::getModel('category');
$catModel->countProducts( $category->virtuemart_category_id );

Scargo

Joomla 2.5; Virtuemart  2.0.18a
The old: gsmmuzeum.hu
The under construction: gsmmuzeum.hu/gsmnew/

jedzon

Hi,

Great solution, everything works fine, everthing except parent category - I have 0 (subcategories have for example 2 products but parent still shows 0). Can anybody help me to solve this problem?

Thank you,
Regards,
Karol

lostmail

Quote from: jedzon on April 02, 2013, 17:42:35 PM
Hi,

Great solution, everything works fine, everthing except parent category - I have 0 (subcategories have for example 2 products but parent still shows 0). Can anybody help me to solve this problem?

Thank you,
Regards,
Karol

Same problem.

Main Category is "0" but Sub-Categories have products....Main Category should show count of products included in su categories....

[attachment cleanup by admin]
Joomla 3.x | VirtueMart 4.0.12 107771 | HORME3 PRO 1.9.6 / 2.0
VirtueMart 4.2.4 | Joomla 4.4.1 | PHP 8.1 | Vp_neoteric 1.3

lostmail

Nobody uses "show number of products in category" ?

It´s working only for categories which have products.

Category "0"
- Sub Category "5"
- Sub Category "9"
....

Category should be "14" - not "0"

Any idea how to modify the hack above ?
Joomla 3.x | VirtueMart 4.0.12 107771 | HORME3 PRO 1.9.6 / 2.0
VirtueMart 4.2.4 | Joomla 4.4.1 | PHP 8.1 | Vp_neoteric 1.3

tobisagt

Hi guys... sry for my late response!

I have a much better solution for you now!

This should work for every one!

<?php // no direct access
defined('_JEXEC') or die('Restricted access');
//JHTML::stylesheet ( 'menucss.css', 'modules/mod_virtuemart_category/css/', false );
$ID str_replace('.''_'substr(microtime(true), -88));
$hidecat VmModel::getModel('category');
$showCount true;
?>


<ul class="VMmenu<?php echo $class_sfx ?>" id="<?php echo "VMmenu".$ID ?>" >
<?php foreach ($categories as $category) {
$count $hidecat->countProducts$category->virtuemart_category_id );
 $active_menu 'class="VmClose"';
$caturl JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$category->virtuemart_category_id);
$cattext = ($showCount) ? $category->category_name .'<span>('.$count .')</span>'$category->category_name;
//if ($active_category_id == $category->virtuemart_category_id) $active_menu = 'class="active"';
if (in_array$category->virtuemart_category_id$parentCategories)) $active_menu 'class="VmOpen"';

$visible false;
  if ($count == 0) {
   foreach ($category->childs as $child) {
    if ($hidecat->countProducts($child->virtuemart_category_id) > 0) {
     $visible true;
    }
   }
  }
  else
  {
   $visible true;
  }
 
  if ($visible) {

?>


<li <?php echo $active_menu ?>>
<div >
<?php echo JHTML::link($caturl$cattext);
if ($category->childs) {
?>

<span class="VmArrowdown"> </span>
<?php
}
?>

</div>
<?php if ($active_menu=='class="VmOpen"') {


?>

<ul class="menu<?php echo $class_sfx?>">
<?php
foreach ($category->childs as $child) {
if ( $hidecat->countProducts$child->virtuemart_category_id ) > ) {
$count '<span>'.$hidecat->countProducts$child->virtuemart_category_id ).'</span>';
$caturl JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$child->virtuemart_category_id);
$cattext = ($showCount) ? $child->category_name .'<span>('.$count .')</span>' $child->category_name;
?>


<li>
<div ><?php echo JHTML::link($caturl$cattext); ?></div>
</li>
<?php } ?>
<?php ?>
</ul>
<?php
}
?>

</li>
<?php ?>
<?php ?>
</ul>

lostmail

Hm - now the main categories which have no products (only sub-cats have products) are completely hidden and only Main Cats with products in it are visible..
Joomla 3.x | VirtueMart 4.0.12 107771 | HORME3 PRO 1.9.6 / 2.0
VirtueMart 4.2.4 | Joomla 4.4.1 | PHP 8.1 | Vp_neoteric 1.3

tobisagt