Jump to content

[SOLVED]Blockcategories - hide empty categories from the 3rd level


Recommended Posts

thank you...anyway i was able to count products and then do it passing the $currentDepth variable to the tpl file but i have another problem: when i'm at level 2 and all the subcategories (level 3) do not contain products... how to hide the 2nd level <li> completely, too? it's not easy to me...if someone can help is really appreciated. thanks

 

code example:

<li {if $node.curd == 3 && $node.products == 0} style="display:none"{/if} class="{if isset($last) && $last == 'true'}last{/if} {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}actived{/if} toto">
<a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'}</a>
{if $node.children|@count > 0}
 <ul>
 {foreach from=$node.children item=child name=categoryTreeBranch}
  {if isset($smarty.foreach.categoryTreeBranch) && $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>

Edited by mmsh (see edit history)
Link to comment
Share on other sites

If you want to do that you can't handle that in your tpl file. You have a value like "$node.children" only because in the php script there has been a mysql "select" query that retrieved that value and assigned it to smarty.

 

However, such a query hasn't been done for the content of subcategories. So you will need to do those queries yourself.

Link to comment
Share on other sites

how you should do it? 1) select count nodes at 3rd level? 2) count products 3) {if $node.curd == 2 && no nodes && no products} display none {/if} ? can you help me please with a concrete example?

sure that i can't manage it with $node.curd (that is the current depth) and $node.children|@count ? and then a foreach children count the products and calculate if are zero or not?

Edited by mmsh (see edit history)
Link to comment
Share on other sites

As I told you before: you cannot solve this in your tpl file.

 

Instead you should go to your php files. You might start in CategoryController.php in the controllers directory but you might also have a look at category.php in the classes directory.

 

You will get something like "$rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT ...........')" in which you query the table ps_category and ps_product_category.

 

 

This is complicated stuff that can take hours of work to get everything right.

Link to comment
Share on other sites

true until a certain point... because you forget that I have already edited the query to get the number of products and the current depth...and with smarty you can do a lot of things:

explain the code: i'm at level 2, if the children are more than zero, count the products for every child. If the sum of products remains zero -> Hide the current category at level 2 (and do more!...also hide the 3rd which do not contain products) ;-)

it wasn't really complicated.

the new category-tree-branch.tpl:

{if $node.curd == 2 && $node.children|@count > 0}
{assign var=sum value=0}
{foreach from=$node.children item=child}
{$sum=$sum+$child.products}
{/foreach}
{/if}
<li {if ($node.curd == 2 && $sum == 0) || ($node.curd == 3 && $node.products == 0)}style="display:none"{/if} class="{if isset($last) && $last == 'true'}last{/if} {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}actived{/if} toto">
<a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'}{if $node.curd == 3}(<font color="#0b8553"><b>{$node.products}</b></font>){/if}</a>
{if $node.children|@count > 0}
 <ul>
 {foreach from=$node.children item=child name=categoryTreeBranch}
  {if isset($smarty.foreach.categoryTreeBranch) && $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>

now mark as SOLVED. thanks

Link to comment
Share on other sites

  • 2 months later...
  • 4 months later...

Check out my module to hide empty categories in the sidebar and footer

 

Non-empty Categories Module v1.0

Rohit, you did a module with my tips? if you don't show the source code and you let people buy...i can even think that you copied my code to make your module...give me a prove that you didn't follow my guide lines...send me a private message and let me download the code for free. thanks

Edited by mmsh (see edit history)
Link to comment
Share on other sites

Rohit, you did a module with my tips? if you don't show the source code and you let people buy...i can even think that you copied my code to make your module...give me a prove that you didn't follow my guide lines...send me a private message and let me download the code for free. thanks

@mmsh, I have not used your code.

Link to comment
Share on other sites

@mmsh, I have not used your code.

you can say that like me saying that Santa Claus exists...! :D since clearly I don't need your module 'cause i can code my own, please send me a private message and let me download the module for PS 1.4... so I can confirm that you didn't use my code. Otherwise you should better not to sell that code, more honestly...

Link to comment
Share on other sites

  • 1 year later...

you can say that like me saying that Santa Claus exists...! :D since clearly I don't need your module 'cause i can code my own, please send me a private message and let me download the module for PS 1.4... so I can confirm that you didn't use my code. Otherwise you should better not to sell that code, more honestly...

rohit has put the code on github http://github.com/rubrodapa/Empty-Categories-Controller

Link to comment
Share on other sites

×
×
  • Create New...