Jump to content

Automatic hide empty categories


Recommended Posts

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?

Link to comment
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).

Link to comment
Share on other sites

  • 4 months later...
  • 1 year later...
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
Link to comment
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 :)
Link to comment
Share on other sites

  • 1 year later...
  • 5 months later...

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
Link to comment
Share on other sites

  • 4 months later...

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?

Link to comment
Share on other sites

  • 10 months later...

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.

Link to comment
Share on other sites

  • 2 weeks later...

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....

Link to comment
Share on other sites

  • 2 months later...

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

Link to comment
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)
Link to comment
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 ...

Link to comment
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.

Link to comment
Share on other sites

  • 1 year later...

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)
Link to comment
Share on other sites

  • 1 month later...

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
Link to comment
Share on other sites

  • 10 months later...

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
Link to comment
Share on other sites

  • 1 month later...
×
×
  • Create New...