Author Topic: Modify Category Module [Count Products] [Hide empty Category]  (Read 19452 times)

tobisagt

  • Jr. Member
  • **
  • Posts: 106
Modify Category Module [Count Products] [Hide empty Category]
« on: January 23, 2012, 16:41:56 pm »
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

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10423
  • VirtueMart Version: 3+
Re: Modify Category Module
« Reply #1 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



J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

tobisagt

  • Jr. Member
  • **
  • Posts: 106
Re: Modify Category Module
« Reply #2 on: January 24, 2012, 16:08:57 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/

Code: [Select]
<?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

  • Beginner
  • *
  • Posts: 1
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #3 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.

tobisagt

  • Jr. Member
  • **
  • Posts: 106
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #4 on: April 04, 2012, 16:11:34 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

  • Beginner
  • *
  • Posts: 1
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

  • Jr. Member
  • **
  • Posts: 106
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #6 on: April 11, 2012, 22:27:43 pm »
You are right.

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

leFlea

  • Beginner
  • *
  • Posts: 19
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #7 on: May 27, 2012, 03:05:43 am »
using category model is a better solution, so that you wouldn't need to rewrite the function:

Code: [Select]
$catModel = VmModel::getModel('category');
$catModel->countProducts( $category->virtuemart_category_id );

Scargo

  • Beginner
  • *
  • Posts: 8
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #8 on: February 24, 2013, 18:07:31 pm »
It's workig very weel with current mode :D

Thanks!

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

jedzon

  • Beginner
  • *
  • Posts: 30
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #9 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

lostmail

  • Jr. Member
  • **
  • Posts: 280
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #10 on: May 23, 2013, 15:44:00 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 + VM 3.6.x (for Testing without any additional Extensions)

lostmail

  • Jr. Member
  • **
  • Posts: 280
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #11 on: June 03, 2013, 10:39:10 am »
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 + VM 3.6.x (for Testing without any additional Extensions)

tobisagt

  • Jr. Member
  • **
  • Posts: 106
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #12 on: June 03, 2013, 11:09:02 am »
Hi guys... sry for my late response!

I have a much better solution for you now!

This should work for every one!

Code: [Select]
<?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

  • Jr. Member
  • **
  • Posts: 280
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #13 on: June 03, 2013, 12:35:02 pm »
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 + VM 3.6.x (for Testing without any additional Extensions)

tobisagt

  • Jr. Member
  • **
  • Posts: 106
Re: Modify Category Module [Count Products] [Hide empty Category]
« Reply #14 on: June 03, 2013, 13:03:47 pm »
I updated my code above! Pls check it out.