VirtueMart Forum

VirtueMart 2 + 3 + 4 => Frontend Modules => Topic started by: tobisagt on January 23, 2012, 16:41:56 PM

Title: Modify Category Module [Count Products] [Hide empty Category]
Post by: tobisagt 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
Title: Re: Modify Category Module
Post by: 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



Title: Re: Modify Category Module
Post by: tobisagt on January 24, 2012, 16:08:57 PM
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?!)


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]
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: 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.
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: tobisagt on April 04, 2012, 16:11:34 PM
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.
Title: Re: Modify Category Module [Count Products] [Hide empty Category] [SOLVED]
Post by: pixelism on April 11, 2012, 21:26:32 PM
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]
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: tobisagt 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.
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: leFlea 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:

$catModel = VmModel::getModel('category');
$catModel->countProducts( $category->virtuemart_category_id );
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: Scargo on February 24, 2013, 18:07:31 PM
It's workig very weel with current mode :D

Thanks!

Scargo
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: 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
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: lostmail on May 23, 2013, 15:44:00 PM
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]
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: lostmail 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 ?
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: tobisagt 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!

<?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>
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: lostmail 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..
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: tobisagt on June 03, 2013, 13:03:47 PM
I updated my code above! Pls check it out.
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: lostmail on June 03, 2013, 14:00:04 PM
NO - now all categories are visible - but Categories without products but with su-categories having products say "0"


[attachment cleanup by admin]
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: tobisagt on June 03, 2013, 14:19:47 PM
So, what is it what you exactly want?

You want to show maincategories, which have no prdoucts itself, but the childcategories have prodcuts and the maincategory shouldn't show the number "0" though there are no products in?
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: lostmail on June 03, 2013, 14:23:16 PM
Exactly  ;)

Sample:
Category => (50)
"Category" has no products in itself - the "50" is the summary of Subs which have products....
Sub1  => (10)
Sub2 => (15)
Sub3 => (25)

Otherwise visitors think that there are no products in the category completely...
So the first level of each category (i call it "main category") should have the summary of all products which are in it + products of Subcategories within the main categories..
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: lostmail on June 19, 2013, 14:08:02 PM
At last the categories layout is crashing in IE8....Firefox layout is OK but i recognised today that the layout is crashing in IE8...there seems to be something further wrong to be in the code...

So the count of products have to be modified and the Layout has to be improved....

Deactivated the override to VM default.... :-[
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: vitgioi on August 18, 2013, 18:50:26 PM
Thank to tobisagt, it's work for me
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: Bit Stupid on October 04, 2013, 14:13:33 PM
Thanks Tobisagt and pixelism. 

This is a great fix to some thing I would imagine should be in the core of VM by now, and has saved me.
Title: Re: Modify Category Module [Count Products] [Hide empty Category]
Post by: timlogotim on January 13, 2014, 20:38:05 PM
Has this functionality made its way into VM core yet?  I have version 2.0.26d and I need to be able to hide categories that are empty...all the way down to the subcategories. 

Does the latest code work for this version?  Just wanted to check before making any hacks, as I really prefer not to change things that my change in future releases of VM. 

Please advise if you know - thanks!