Jump to content
frances

Automatic hide empty categories

Recommended Posts

Does anybody knows how can I easy automatic empty hide categories? Thank you for responses.

  • Like 1

Share this post


Link to post
Share on other sites

*bump*

Due to the lack of filtered/guided navigation we're having to create lots of categories and subcategories - having a category hide itself when empty would massively ease maintenance.

Share this post


Link to post
Share on other sites

Good idea. I'll add it to the Categories block v2.0 module I'm working on. I've already added the option to put the number of products in brackets after the names, so it should be easy. I just need to know what should happen when a category is empty, but it contains subcategories that are not empty. Should I hide the category, causing subcategories with products to also be hidden, or should I display these empty categories and only hide the ones where all subcategories are also empty?

Share this post


Link to post
Share on other sites

I'd say that the best would be if the admin could make that choice but if its not possible or to much work then show empty categories with non-empty subcategories.

Another thing if you have the time is the option to hide all main categories and their subcategories if they are not selected. (only showing sub-categories of the selected main category).

Share this post


Link to post
Share on other sites

I really need this feature too, and am really looking forward to this updated module. Thanks for working on it!

Share this post


Link to post
Share on other sites

I need this option too as we have categories that we currently dont have stop for, and others the are high turn over. No sense in showing a customer a category with no items in it or its subcategories.

Share this post


Link to post
Share on other sites

Since my previous post in this topic, I've released my category module here. It has the option to hide empty categories.

Share this post


Link to post
Share on other sites
Does anybody knows how can I easy automatic empty hide categories? Thank you for responses.


You can achieve this executing the following single query:
UPDATE ps_category AS c SET c.active=0 WHERE c.id_category IN (
SELECT tmp.id_category
FROM (
SELECT id_category
FROM ps_category
) AS tmp
WHERE tmp.id_category NOT 
IN (
SELECT cp.id_category
FROM ps_category_product AS cp
GROUP BY cp.id_category
)
)



where "ps_" is your table prefix.

  • Like 2

Share this post


Link to post
Share on other sites
What happens tomorrow morning, when Susie restocks a product assigned to a currently empty category?
She just needs to do 1 click on the inactive category's activating icon ("X"), simple as that :)


how about
SELECT DISTINCT
plus, isn't the GROUP BY superfluous here?
Yes, you are right, good note! You can execute that query as SELECT DISTINCT, not GROUP BY as it returns only 1 column.


Also, if we just look to the category_product table, we're blind to the possibility that all products in a given category are either out-of-stock or have been flagged as inactive products.
You can't find in category_product table a category that has no products. That's the point :)

Share this post


Link to post
Share on other sites

There is a bit easier method that could be also used to show how many items are in the category.

 

I have found most information for the steps 1 & 2 here on the forum: http://www.prestasho...post__p__771194

 

Step 1 - get product count

 

File: /modules/blockcategories.php

Find: getTree

After the code

if (!isset($resultIds[$id_category]))
  return false;

 

add following

$ProductsCount = 0;
 $ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(cp.id_category) FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'product pr WHERE cp.id_category = '.$id_category .' AND cp.id_product = pr.id_product AND pr.active = 1' );

 

Before the end of the function (before return $return) find a $return = array(... and modify the end as follows:

$return = array(.... 'children' => $children, 'products' => $ProductsCount);

 

Step 2 - show product count

 

File: themes\YOUR_THEME\modules\blockcategories\category-tree-branch.tpl

 

Optionally, you can show how many items are in the category. Find first A after the first LI and before the /A closing tag add the following:

 ({$node.products})

 

You may use usual space instead of non-breaking one.

 

Step 3 - hide empty categories

 

In the same file, just before the code begins (before the very first LI) add following:

{if $node.products > 0}

 

and at the end add this

{/if}

 

 

Find the next line

{if $node.children|@count > 0}

 

and add a condition into it

&& $node.products > 0

 

Entire code will look like that:

{if $node.products > 0}
<li {if isset($last) && $last == 'true'}class="last"{/if}>
<a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} ({$node.products})</a>
{if $node.children|@count > 0 && $node.products > 0}
 <ul>
 {foreach from=$node.children item=child name=categoryTreeBranch}
  {if $smarty.foreach.categoryTreeBranch.last}
{include file="$branche_tpl_path" node=$child last='true'}
  {else}
{include file="$branche_tpl_path" node=$child last='false'}
  {/if}
 {/foreach}
 </ul>
{/if}
</li>
{/if}

 

Recompile/clear your cache and now your CATEGORIES should look like that:

 

post-659223-0-79008200-1375869073_thumb.jpg

  • Like 4

Share this post


Link to post
Share on other sites

i'm looking for a way to hide empty categories, the above seems to work for me when there are no subcats, but for what ever reason not when there are. most of my cats have one, sometimes two, subcats; when i add the product number to the cat tree using the above method it shows (0) on my parent cats even when there are many products in a subcat. it works fine when there are products in a cat with no subcats, so i'm not totally goofing it up. anyone have any ideas?

Share this post


Link to post
Share on other sites

i am having the same problem with prestashop 1.6.9, it is just impossible to hide empties sub-categories which really doesn't make ANY sense, who would like to display nothing for sale?

 

anybody knows how to hide those empties sub-cat?

 

thanks

Share this post


Link to post
Share on other sites

Not off hand, but one could

 

Get all category id's

Now go through one by one

see if a product is associated with it

see if it has sub categories and do they have products

If both are there skip action else disable it

 

This is a simple layout of what you could do.

 

I have a module that is coming out soon for this exact thing as I needed it because the maintaining of categories is very time consuming at times.

Share this post


Link to post
Share on other sites

I have a module for this but trying to set a price and see if it will even be wanted. I was thinking 50 nut you decide and tell me if it's worth adding it to addons site.

Share this post


Link to post
Share on other sites

I have the same problem. I need to hide empty categories and subcategories. Do you have a module to make this?

I use prestashop 1.6

Tank you.

Edited by santi00 (see edit history)

Share this post


Link to post
Share on other sites

I might be missing something here but why not just disable the categories or sub- categories in back office until you have products to fill them then just click enable.

Share this post


Link to post
Share on other sites

yep but problem is : they show back..... in my case i can disable as much i want but as long as there is something inside even if this is only 1 disabled product, they end up as availables and show up  in the store.

 

I have a bunch of categories i want to be disabled, they contain disabled products that i do not want to delete because they will be back in stock at some point....

Share this post


Link to post
Share on other sites

Please write here when your module will be ready to download and use. 
I hope it will work also with PS 1.5
Regards!

Edited by Krzewka (see edit history)

Share this post


Link to post
Share on other sites

It sure does, and I am just waiting on approval as there were some issues that were my fault because I was trying to add other languages, but 2 was incorrect becasue of auto translation. I am sure it will be any time now.

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

In My case

*No products in Category

*But products available in subcategory

*Now main category display "There are no products in category"

 

 

Please tell me how to remove this message. Because if new customer will come to my site & check main category message then may be he will not visit in sub category.

 

Please suggest  me how to remove this message from main category.

I am using PS 1.4.8.3

 

 

for reference please check www.uthmart.com

 

Thanks in advance.

Devendra

Share this post


Link to post
Share on other sites

You need to ad a check to se if a category has products in its children and if it does it has to be active and it will go away.

 

Like below

 

if(categorychild check > 0)

sqlCayegoryupdate('1'); //activate category

else

sqlCayegoryupdate('0'); //deactivate category

Edited by ShopSaveNEarn (see edit history)

Share this post


Link to post
Share on other sites

In my case i need to hide empties categories + empties sub-categories

 

AND

 

categories and sub-categories containing disabled products

 

will your module do this?

Share this post


Link to post
Share on other sites

are they any feedback somewhere from users of your module?

 

i see what you suggested to Devena who obviously bought it, I wouldn't like to buy an add on and out of the box having to start adding queries especially with the little explanation you provide...

 

quote "You need to ad a check to se if a category has products in its children and if it does it has to be active and it will go away. like below..." don't know if Devena was able to fix his problem ...

Share this post


Link to post
Share on other sites

No in my module you have to add nothing. That was without my module and to do it on your own. My module is complete and there is no edits, but if your doing it yourself you need to add checks and such to make it happen and that's what I was explaining to devina and it was nothing to,do with my module.

Share this post


Link to post
Share on other sites

hope someone buy it and give feedback, i am very cautious about module inspected by Prestashop after the mess I went through with Google analytics add-on....

Share this post


Link to post
Share on other sites

Oh wow sorry you had issues with them. I know here soon I am opening my webdesign website and it will have all my modules and custom design or services and I always make sure my modules work great and the customer is satisfied. I even do the edits, but like to customize the modules to the customer. I could do a custom one for you too but rest assured if you buy my module I will ensure it works accordingly to its description and purpose. If there by chance was an issue I would fix it personally and give you a new module with the fixes, but it is rare that I need to do so.

Share this post


Link to post
Share on other sites

Great article. But just in case you want an instant fix this module is available on the Prestashop Addons webite - http://addons.prestashop.com/en/18334-auto-category-maintenance.html

We are working on making all our modules available on other carts as well so check out the Programmers Direct website.

Edited by Programmers Direct (see edit history)

Share this post


Link to post
Share on other sites

SIMPLE WORKING SOLUTION FOR PRESTASHOP 1.6.x.x

 

Modification adjusted to PS 1.6.x.x (tested on 1.6.1.5) for hiding categories and / or subcategories if there are no products in them. Based on solution from first page, but more elegant - using internal PS category class instead of SQL query.

 

Open file:

/modules/blockcategories/blockcategories.php

Find getTree function, it should look similar to this:

public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0)
{
if (is_null($id_category))
$id_category = $this->context->shop->getCategory();
$children = array();
if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth))
foreach ($resultParents[$id_category] as $subcat)
$children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
if (isset($resultIds[$id_category]))
{
$link = $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']);
$name = $resultIds[$id_category]['name'];
$desc = $resultIds[$id_category]['description'];
}
else
$link = $name = $desc = ''; 
 
$return = array(
'id' => $id_category,
'link' => $link,
'name' => $name,
'desc'=> $desc,
'children' => $children
);
return $return;
}

Add new lines from below or just replace whole function with this one:

	public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0)
	{
		if (is_null($id_category))
			$id_category = $this->context->shop->getCategory();
		$children = array();
		if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth))
			foreach ($resultParents[$id_category] as $subcat)
				$children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
		if (isset($resultIds[$id_category]))
		{
			$link = $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']);
			$name = $resultIds[$id_category]['name'];
			$desc = $resultIds[$id_category]['description'];
		}
		else
			$link = $name = $desc = '';
    
    /* new 2 lines to add below */
    $CategoryObj = new Category($id_category,$this->context->language->id,$this->context->shop->id);
    $ProductsCount = $CategoryObj->getProducts($this->context->language->id,1,1000,null,null,true,true,false,1,false,$this->context);

		$return = array(
			'id' => $id_category,
			'link' => $link,
			'name' => $name,
			'desc'=> $desc,
                        'products' => $ProductsCount, // new line to add
			'children' => $children
      
		);
		return $return;
	}

Open file (if you are not using default theme your path will be different in place where it says "default-bootstrap")

themes/default-bootstrap/modules/blockcategories/category-tree-branch.tpl

Replace all contents in this file with this below (there are only added 2 lines - first and last, and one additional condition at line 7)

{if $node.products > 0}
<li {if isset($last) && $last == 'true'}class="last"{/if}>
	<a 
	href="{$node.link|escape:'html':'UTF-8'}"{if isset($currentCategoryId) && $node.id == $currentCategoryId} class="selected"{/if} title="{$node.desc|strip_tags|trim|escape:'html':'UTF-8'}">
		{$node.name|escape:'html':'UTF-8'}
	</a>
	{if $node.children|@count > 0 && $node.products > 0}
		<ul>
			{foreach from=$node.children item=child name=categoryTreeBranch}
				{if $smarty.foreach.categoryTreeBranch.last}
					{include file="$branche_tpl_path" node=$child last='true'}
				{else}
					{include file="$branche_tpl_path" node=$child last='false'}
				{/if}
			{/foreach}
		</ul>
	{/if}
</li>
{/if}

Save, refresh, done.

Remember when you update block categories module those modifications in your php file will be overwriten. Simply don't update block categories module or reapply solution after each update.

 

Hope this helps someone.

Edited by hootersam (see edit history)
  • Like 4

Share this post


Link to post
Share on other sites

Thanks @hootersam it's  a great solution :) 

 

 

Remember when you update block categories module those modifications in your php file will be overwriten. Simply don't update block categories module or reapply solution after each update.

 

 

A little advice here : you could consider overriding the gettree function in order to keep the changes even after updating the module or prestashop.
Just create the following file "override/modules/blockcategories/blockcategories.php"  :

<?php

if (!defined('_PS_VERSION_'))
    exit;

class BlockCategoriesOverride extends BlockCategories
{

/** THE NEW GETTREE FUNCTION GOES GERE **/

}

;)

  • Like 2

Share this post


Link to post
Share on other sites

It works great, I just hide the empty categories in the left column, but it is still in the product column. How do I hide it from here?

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More