Jump to content

Displaying all products from Subcategories on the Category page


Recommended Posts

Hi there,

I am new to PrestaShop and I am using it for the first time for a project which is not a classic "shop" so I will have to make a few alterations to the code itself (and I am not really a developer).

So hopefully you will be able to help me.

My first problem is the following.

The site has around 20 main categories (restaurants) and each category will have 3-4 subcategories (starters, mains, desserts).

But we don't want to show the products only on the Subcategories pages as it adds one more click.
We want to show all the products of Restaurant 1 on "Restaurant 1" page but sorted by Subcategories.

So we would get:

RESTAURANT 1

STARTERS
Dish 1
Dish 2
Dish 3

MAINS
Dish 1
Dish 2
Dish 3

I already changed a few things on the category.tpl and the subcategories are displayed, but the products are not filtered and are displaying under each subcategory. I suppose there is something to change in Category.php.

Here is my code if it can help... I suppose I must add a filter somewhere, but I thought I'd ask before spening 2 hours on that issue.

Thank you.

Johanna

{if $products}
   {include file=$tpl_dir./product-sort.tpl}



        {if isset($subcategories)}   
               {foreach from=$subcategories item=subcategory}
{$subcategory.name|escape:'htmlall':'UTF-8'}
                   {include file=$tpl_dir./product-list.tpl products=$products}

               {/foreach}
         {else} 
               {include file=$tpl_dir./product-list.tpl products=$products}    
         {/if}


   {include file=$tpl_dir./pagination.tpl}

Link to comment
Share on other sites

Any idea anybody?

Maybe I should move this post into the Development section?

I would take anything even just a tip on which direction to take, a module which does almost the same thing I could work from...

Thanks!

Link to comment
Share on other sites

Not sure if my solution is really what you need but I think you can do it from BO without a change on the code.

So, you have your Categories and Subcategories.
Add a product (dish 1) in starters under the RESTAURANT 1 and check the boxes Starters and RESTAURANT 1 to include the Dish 1.
In the same way add all your dishes for all.

Then go BO> Preferences> Products and will find "Default order by:"
Select "Position inside category".

Link to comment
Share on other sites

Thanks for your answer!

I already did that (I think) but I don't have the titles of the subcategories displaying on the page.

I have:

RESTAURANT

Items from subcategories 1
Items from subcategories 2
...

Because the system is "repeating" the item in both categories...

If I can't display the text, I'll just keep it that way... Because I tried different things (if... blabla) and it only repeated all the products under each subtitle...

Link to comment
Share on other sites

I put back the code the way it was... But the subcategories title are not displayed...

Are they displayed for you when you try this option?
I feel like the page is only calling the products from "Restaurant 1" and doesn't care if they are also part of subcategories...

Link to comment
Share on other sites

Sorry.... But the "Block Category" is the category menu on the left sidebar is it?

The change you gave me didn't change anything on the Category page itself anyway...

On the left menu, I have:

Restaurant 1
Restaurant 2
Restaurant 3
Restaurant 4
Restaurant 5

If I click on Restaurant 2, I have:

Restaurant 2

  • List of the subcategories (links to subcategories pages - Starters, Mains etc.)

Sort by drop down

  • List of the products of "Restaurant 2"
  • Salad 1
  • Salad 2
  • Risotto
  • Pasta

    While I would like:

    Restaurant 2

    • List of the subcategories (links to subcategories pages - Starters, Mains etc.)

    Sort by drop down

    Starters

  • Salad 1
  • Salad 2

    Mains

  • Risotto
  • Pasta


    I really thing the code should be changed and instead of displaying the items of the category "Restaurant 2" it should display the products of the subcategories of Restaurant 2... and that way, the titles would be there anyway...

    But it's a bit too advanced for me.

    Thanks for trying anyway!!
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...

hello hello u people had some luck with it i am trying to do the same thing but i am having no luck i saw ur site sjohanna seems that u came up wit some solution can u help me please as thats the only thing i am left with in my project help me plzz plzzz

Link to comment
Share on other sites

  • 6 months later...
  • 2 months later...

hi guys/girls

not sure if you got your answer, I assume you did, but anyways I needed it too and I implemented this in my website
if anybody needs this in the future, please let me know and I'll send you my changes, no problem

Link to comment
Share on other sites

ok so I hope you can use this. While I am an experienced php developer, I'm not that familiar with smarty so please bare with me :)

Open the root folder of prestashop, you will find there the "category.php" file.
Open this file, and at the bottom where you see

if (isset($subCategories))
{
   $smarty->assign(array(
       'subcategories_nb_total' => sizeof($subCategories),
       'subcategories_nb_half' => ceil(sizeof($subCategories) / 2)));


you put in this code

if (isset($subCategories))
{
   $smarty->assign(array(
       'subcategories_nb_total' => sizeof($subCategories),
       'subcategories_nb_half' => ceil(sizeof($subCategories) / 2)));

$subcategory_objects=array();
$smarty->assign("andr_lang",intval($cookie->id_lang));
foreach ($subCategories as $subcategory)
   {
       $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang) );
       $subcategory_objects[$subcategory['id_category']]=$sub;
   }
   //var_dump($subcategory_objects);
   $smarty->assign("subcategories_objects",$subcategory_objects);
}



so what this does is to make sure you have your subcategory objects passed to the template file, which we will now open
it is called category.tpl and you can find it in you theme directory "/themes/theme_name/category.tpl"
look for something like

<!-- Subcategories -->


I believe this is the original code in my theme:

{l s='Subcategories'}


getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" title="{$subcategory.name|escape:'htmlall':'UTF-8'}">
                   {if $subcategory.id_image}
getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" />
                   {else}

                   {/if}



getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategory.name|escape:'htmlall':'UTF-8'}








this is what I have replaced it with in order to show the products from subcategories

{l s='Subcategories'}

       {foreach from=$subcategories item=subcategory}
{$subcategory.name|escape:'htmlall':'UTF-8'}
                   {assign var="subcategory_id" value=$subcategory.id_category}
                   {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
                  {include file=$tpl_dir./product-list.tpl products=$subcategory_object->getProducts($andr_lang,'1','10')}
       {/foreach}





When you decide to use this, make sure you don't mix up your variable names.
You should be able to see this on my website (this link will take you directly to the category where multiple subcategories are displayed) stickere de perete

Link to comment
Share on other sites

ok so I hope you can use this. While I am an experienced php developer, I'm not that familiar with smarty so please bare with me :)

Open the root folder of prestashop, you will find there the "category.php" file.
Open this file, and at the bottom where you see
if (isset($subCategories))
{
   $smarty->assign(array(
       'subcategories_nb_total' => sizeof($subCategories),
       'subcategories_nb_half' => ceil(sizeof($subCategories) / 2)));


you put in this code

if (isset($subCategories))
{
   $smarty->assign(array(
       'subcategories_nb_total' => sizeof($subCategories),
       'subcategories_nb_half' => ceil(sizeof($subCategories) / 2)));

$subcategory_objects=array();
$smarty->assign("andr_lang",intval($cookie->id_lang));
foreach ($subCategories as $subcategory)
   {
       $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang) );
       $subcategory_objects[$subcategory['id_category']]=$sub;
   }
   //var_dump($subcategory_objects);
   $smarty->assign("subcategories_objects",$subcategory_objects);
}



so what this does is to make sure you have your subcategory objects passed to the template file, which we will now open
it is called category.tpl and you can find it in you theme directory "/themes/theme_name/category.tpl"
look for something like

<!-- Subcategories -->


I believe this is the original code in my theme:

{l s='Subcategories'}


getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" title="{$subcategory.name|escape:'htmlall':'UTF-8'}">
                   {if $subcategory.id_image}
getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" />
                   {else}

                   {/if}



getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategory.name|escape:'htmlall':'UTF-8'}








this is what I have replaced it with in order to show the products from subcategories

{l s='Subcategories'}

       {foreach from=$subcategories item=subcategory}
{$subcategory.name|escape:'htmlall':'UTF-8'}
                   {assign var="subcategory_id" value=$subcategory.id_category}
                   {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
                  {include file=$tpl_dir./product-list.tpl products=$subcategory_object->getProducts($andr_lang,'1','10')}
       {/foreach}





When you decide to use this, make sure you don't mix up your variable names.
You should be able to see this on my website (this link will take you directly to the category where multiple subcategories are displayed) stickere de perete



How can I modify this code to work only under simpe condition like:
if category/subcategoryID = 3, 45, 67, 34 then applay this modyfication ??

thanks,
SonnyBoy
Link to comment
Share on other sites

I'm not on my development machine right now, but I think you can use this condition in the template (this is not the most efficient way). Assuming you want to do this for 2-3 categories, you can place an IF. For a reasonable number of cases you can you a switch, but I'm not that good with Smarty. I'll exemplify for an IF, you try to do the rest:)


1. If you just want subcategory with ID=3 to be displayed

{l s='Subcategories'}

       {foreach from=$subcategories item=subcategory}
{$subcategory.name|escape:'htmlall':'UTF-8'}
                   {assign var="subcategory_id" value=$subcategory.id_category}
{if $subcategory.id_category == '3' }  //this will only display the products in the subcategory with ID = 3
                   {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
                  {include file=$tpl_dir./product-list.tpl products=$subcategory_object->getProducts($andr_lang,'1','10')}
{/if}
       {/foreach}





2. if you want to display subcategories only for the category with ID=5 (for example)

{l s='Subcategories'}

{if $category['id_category'] == '5' }  //so only do the following for the category with ID=5
       {foreach from=$subcategories item=subcategory}
{$subcategory.name|escape:'htmlall':'UTF-8'}
                   {assign var="subcategory_id" value=$subcategory.id_category}
                  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
                  {include file=$tpl_dir./product-list.tpl products=$subcategory_object->getProducts($andr_lang,'1','10')}
       {/foreach}
{/if}





As I said, I can't test the code right now, but it should be a good start

Link to comment
Share on other sites

hi guys,
indeed, i'm running prestashop Version 1.3.2.3
unfortunately I can't help you with version 1.4 until I switch to it myself, but I'm not sure when this is going to happen
hopefully someone will post a solution for this problem using prestashop 1.4, or someone running 1.4 could update the solution I posted


best of luck

Link to comment
Share on other sites

hi guys,
indeed, i'm running prestashop Version 1.3.2.3
unfortunately I can't help you with version 1.4 until I switch to it myself, but I'm not sure when this is going to happen
hopefully someone will post a solution for this problem using prestashop 1.4, or someone running 1.4 could update the solution I posted


best of luck


But does your code for ver 1.3 in this post works for sure?
Im going to analyse it line by line and try to convert to ps 1.4.
Link to comment
Share on other sites

yes it does,
take a look here http://magazinul-de-stickere.ro/5-autocolante-animale
as you can see there are 4 subcategories being displayed in the category. I limited each subcategory to only show 4 products.


That is exactly what I need to finish the project....

;( ;( ;(

If I apply modification to the php file the site stopped display categories at all ....
Link to comment
Share on other sites

  • 3 weeks later...
hi guys,
indeed, i'm running prestashop Version 1.3.2.3
unfortunately I can't help you with version 1.4 until I switch to it myself, but I'm not sure when this is going to happen
hopefully someone will post a solution for this problem using prestashop 1.4, or someone running 1.4 could update the solution I posted


best of luck


But does your code for ver 1.3 in this post works for sure?
Im going to analyse it line by line and try to convert to ps 1.4.



THANKS DEIUTZ !!! I have got it working with presta 1.4x

here is my working code for ps1.4.3 :

go to Controller/categorycontroller.php
below these lines:
   'subcategories_nb_total' => sizeof($subCategories),
   'subcategories_nb_half' => ceil(sizeof($subCategories) / 2)));



put this:

               'subcategories_nb_total' => sizeof($subCategories),
                       'subcategories_nb_half' => ceil(sizeof($subCategories) / 2)));


                   $subcategory_objects=array();
                   self::$smarty->assign("andr_lang",(int)(self::$cookie->id_lang));
                   foreach ($subCategories as $subcategory)
                       {
                           $sub=new Category(($subcategory['id_category']),(int)(self::$cookie->id_lang));
                           $subcategory_objects[$subcategory[id_category]]=$sub;
                       }
                       //var_dump($subcategory_objects);
                       self::$smarty->assign("subcategories_objects",$subcategory_objects); 

               }





then in category.tpl file in template directory, my 'subategory' div looks like this:


{l s='Subcategories'}


      {foreach from=$subcategories item=subcategory}
{$subcategory.name|escape:'htmlall':'UTF-8'}

                   {assign var="subcategory_id" value=$subcategory.id_category}
                   {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
                  {include file="$tpl_dir./product-list.tpl" products=$subcategory_object->getProducts($andr_lang,'1','100')}
       {/foreach}




 




once again: big thanks, deiutz!

regards,
SonnyBoy

Link to comment
Share on other sites

  • 3 months later...

Thanks for this thread man... I managed to make it work in PS 1.4.5.0 limited to 2 parent categories.

 

Just remember in PS 1.4.5 to limit it to a category, instead of

 

{if $category['id_category'] == '5' } //so only do the following for the category with ID=5

 

use

 

{if $category->id_category == '5'} //so only do the following for the category with ID=5

 

I think it would be a really nice feature if this was an option for the categories in the BO, to either show the subcategories with their thumbnails or show the subcategories with their products in the same view. That would be awesome. There could also be an option to hide the subcategory from the categories menu block, which is what I did via CSS changing category.tpl so it would assignthe value of the menu name as a class to the li element of the menu (and after do a display:none on those I dont want to show, or a li."categoryname" ul {display:none} to hide all the subcategories from the category menu block because they already show when you click on the parent category with all their products.) Like that a parent category could have multiple subcategories and show all their products in one view (this helps if there are not too many products per subcategory) avoiding a huge list of subcategories on the menu block.

Link to comment
Share on other sites

  • 2 months later...
  • 5 months later...

Hello,

I'm using prestashop 1.4.8.2 and I have a problem that my first and main category displays the products all of the products of its subcategories instead of showing the subcategories name, Please help me how can I fix it?

Link to comment
Share on other sites

  • 5 weeks later...
  • 6 months later...
  • 5 months later...

Here is my Tutorial (Tested with PrestaShop v. 1.5.2.0 AND 1.5.4.1):

 

open this file: controllers/front/CategoryController.php

 

search this lines:

if ($subCategories = $this->category->getSubCategories($this->context->language->id))
 {

 

insert this code after the: {

  $subcategory_objects=array();
  $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));

  foreach ($subCategories as $subcategory)
	  {
	   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
	   $subcategory_objects[$subcategory['id_category']]=$sub;
   }
   //var_dump($subcategory_objects);
   $this->context->smarty->assign("subcategories_objects",$subcategory_objects);

 

save this file and upload.

 

now open the file: themes/"your_theme"/category.tpl

 

search this lines:

  <div class="content_sortPagiBar">
{include file="$tpl_dir./pagination.tpl"}
<div class="sortPagiBar clearfix">
 {include file="./product-sort.tpl"}
 {include file="./product-compare.tpl"}
 {include file="./nbr-product-page.tpl"}
</div>
  </div>

 

insert after the </div></div>:

   	 {if isset($subcategories)}

			{foreach from=$subcategories item=subcategory}

					  {assign var="subcategory_id" value=$subcategory.id_category}
					  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}

	   					{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}

	   				{/foreach}

				{else}

					{include file="./product-list.tpl" products=$products}

				{/if}

 

save and upload.

 

now open this file: themes/"your_theme"/product-list.tpl

 

search this line:

<!-- Products list -->

 

after the line insert this:

<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>

 

search this line:

{foreach from=$products item=product name=products}

 

after this line add:

{if $products != ''}

 

now search this lines:

<a class="button lnk_view" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a>
  </div>
 </li>

 

after this lines add:

{else}

 No Products in this Category

{/if}

 

save the product-list.tpl as product-list-sub.tpl and upload the files.

 

DONE!!!

 

I hope it will works... contact me if you have problems....

 

sorry for my bad english

 

best regards

Edited by BIgZumo (see edit history)
  • Like 5
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

 

....insert after the </div></div>:

	   	 {if isset($subcategories)}
			
				{foreach from=$subcategories item=subcategory}
	
						  {assign var="subcategory_id" value=$subcategory.id_category}
						  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
					  
		   					{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
					  
		   				{/foreach}
					
					{else}
				
						{include file="./product-list.tpl" products=$products}
				
					{/if}

 

Thank you, that worked for me, I only modified .tpls in some other way, according to my needs.

 

However, there is one thing that can be improved. Your code sets specific language for displayed data of products ...getProducts('1','1','100')...

it is OK, if you use one language on your site, but if you have several languages, you better use a variable instead of '1'.

 

I have replaced '1' with $cart->id_lang and it works fine now even if you use more than 1 language.

getProducts($cart->id_lang,'1','100')

Edited by Amazzing (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 5 months later...

Hi guys,

 

This kinda works, but it seems only to fetch first sub categories for some, and then all for others. And when moving products from different categories they sometimes stop showing at all...and then just starts to show again when moving product back. Haven't really found a pattern to this yet what categories make.

 

...any one have this working on 1.5.6.x?

Link to comment
Share on other sites

  • 4 weeks later...

Here is my Tutorial (Tested with PrestaShop v. 1.5.2.0 AND 1.5.4.1):

 

open this file: controllers/front/CategoryController.php

 

search this lines:

if ($subCategories = $this->category->getSubCategories($this->context->language->id))
  {
insert this code after the: {

   $subcategory_objects=array();
   $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));
  
   foreach ($subCategories as $subcategory)
		  {
		   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
		   $subcategory_objects[$subcategory['id_category']]=$sub;
	   }
	   //var_dump($subcategory_objects);
	   $this->context->smarty->assign("subcategories_objects",$subcategory_objects);
save this file and upload.

 

now open the file: themes/"your_theme"/category.tpl

 

search this lines:

   <div class="content_sortPagiBar">
	{include file="$tpl_dir./pagination.tpl"}
	<div class="sortPagiBar clearfix">
	 {include file="./product-sort.tpl"}
	 {include file="./product-compare.tpl"}
	 {include file="./nbr-product-page.tpl"}
	</div>
   </div>
insert after the </div></div>:

	   	 {if isset($subcategories)}
			
				{foreach from=$subcategories item=subcategory}
	
						  {assign var="subcategory_id" value=$subcategory.id_category}
						  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
					  
		   					{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
					  
		   				{/foreach}
					
					{else}
				
						{include file="./product-list.tpl" products=$products}
				
					{/if}
save and upload.

 

now open this file: themes/"your_theme"/product-list.tpl

 

search this line:

<!-- Products list -->
after the line insert this:

<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>
search this line:

{foreach from=$products item=product name=products}
after this line add:

{if $products != ''}
now search this lines:

<a class="button lnk_view" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a>
   </div>
  </li>
after this lines add:

{else}

  No Products in this Category

{/if}
save the product-list.tpl as product-list-sub.tpl and upload the files.

 

DONE!!!

 

I hope it will works... contact me if you have problems....

 

sorry for my bad english

 

best regards

 

 

I tried this in Prestashop 1.5.6.0 its not working...

Do you have a solution for Ps version 1.5.6

 

I want something like the following:

 

Sub-category one 

  product 1

  product 2

  product 3

  ...

Sub-category two

  product 1

  product 2

  product 3

  ...

 

Sub-category three 

  product 1

  product 2

  product 3

  ...

Sub-category Four 

  product 1

  product 2

  product 3

  ...

 

Thanks in advance..

Link to comment
Share on other sites

Here is my Tutorial (Tested with PrestaShop v. 1.5.2.0 AND 1.5.4.1):

 

open this file: controllers/front/CategoryController.php

 

search this lines:

if ($subCategories = $this->category->getSubCategories($this->context->language->id))
  {
insert this code after the: {

   $subcategory_objects=array();
   $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));
  
   foreach ($subCategories as $subcategory)
		  {
		   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
		   $subcategory_objects[$subcategory['id_category']]=$sub;
	   }
	   //var_dump($subcategory_objects);
	   $this->context->smarty->assign("subcategories_objects",$subcategory_objects);
save this file and upload.

 

now open the file: themes/"your_theme"/category.tpl

 

search this lines:

   <div class="content_sortPagiBar">
	{include file="$tpl_dir./pagination.tpl"}
	<div class="sortPagiBar clearfix">
	 {include file="./product-sort.tpl"}
	 {include file="./product-compare.tpl"}
	 {include file="./nbr-product-page.tpl"}
	</div>
   </div>
insert after the </div></div>:

	   	 {if isset($subcategories)}
			
				{foreach from=$subcategories item=subcategory}
	
						  {assign var="subcategory_id" value=$subcategory.id_category}
						  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
					  
		   					{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
					  
		   				{/foreach}
					
					{else}
				
						{include file="./product-list.tpl" products=$products}
				
					{/if}
save and upload.

 

now open this file: themes/"your_theme"/product-list.tpl

 

search this line:

<!-- Products list -->
after the line insert this:

<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>
search this line:

{foreach from=$products item=product name=products}
after this line add:

{if $products != ''}
now search this lines:

<a class="button lnk_view" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a>
   </div>
  </li>
after this lines add:

{else}

  No Products in this Category

{/if}
save the product-list.tpl as product-list-sub.tpl and upload the files.

 

DONE!!!

 

I hope it will works... contact me if you have problems....

 

sorry for my bad english

 

best regards

 

 

Can somebody post the solution for Prestashop v 1.5.6.0

Link to comment
Share on other sites

Its working on 1.5.6, you just have to add one product in the main category

 

Category

product 1

 

Sub-category one 

  product 1

  product 2

  product 3

  ...

Sub-category two

  product 1

  product 2

  product 3

 

and so on

Link to comment
Share on other sites

  • 2 months later...
  • 3 months later...

It's now working for me at all on PS 1.6. The only difference I found on the code you ask us to change is this line:

<a class="button lnk_view" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a>
   </div>
  </li>

But the rest of the files I changed them as explained. The thing is that after doing it, no category pages are loaded, just a blank page.

Any options for PS 1.6 users?

 

Thanks

Link to comment
Share on other sites

  • 4 weeks later...
  • 2 weeks later...

Hello every one,

 

I want to show my subcategories products under parent category .

I triyed above soultion in ps 1.5.6.2 but it didn't work for me.

 

http://www.luxishop.cz/index.php check site i have category like following 

 

Telephony(main category)

-Mobile Phones(sub category)

--smartphone(sub sub category)

--iphone(sub sub category)

 

I want to show my all sub sub categories(smartphone,iphone etc) products under main (Telephone) and sub category(mobile phone).

 

i also triyed with layered navigation module but no luck

Please help i am stuck with this problem since two days.

 

Thanks,

Ganesh

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

hello

solution for 1.5.4, 1.5.2 will work also in 1.5.6

Hello vekia,

 

I want to show my subcategories products under parent category .

I triyed above soultion in ps 1.5.6.2 but it didn't work for me.

 

http://www.luxishop.cz/index.php check site i have category like following 

 

Telephony(main category)

-Mobile Phones(sub category)

--smartphone(sub sub category)

--iphone(sub sub category)

 

I want to show my all sub sub categories(smartphone,iphone etc) products under main (Telephone) and sub category(mobile phone).

 

i also triyed with layered navigation module but no luck

Please help i am stuck with this problem since two days.

 

Thanks,

Ganesh

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

 

Hello,
I'm using the last version prestashop 1.6.0.8 => IT's WORK, PERFECT
 
THX a LOT

 

Hi MedTN,

 

I also have 1.6.0.8 but it doesn't work for me at all. For example

 

<a class="button lnk_view" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a>

</div>

</li>

 

looks kind of different in my set up. Could you please tell me how you did it?

 

thanks

Link to comment
Share on other sites

  • 4 weeks later...

My solution for displaying all product from subcategories on category page for Prestashop 1.6.0.11

After hours of debug I discovered that blocklayered module can do the trick. Common idea is to show all products filtered by blocklayered. So some configuration is needed.

 

1. Open <theme_folder>/category.tpl

Find lines

       {if $products}
           <div class="content_sortPagiBar clearfix">
               <div class="sortPagiBar clearfix">
                   {include file="./product-sort.tpl"}
                   {include file="./nbr-product-page.tpl"}
               </div>
               <div class="top-pagination-content clearfix">
                   {include file="./product-compare.tpl"}
                   {include file="$tpl_dir./pagination.tpl"}
               </div>
           </div>
           {include file="./product-list.tpl" products=$products}
           <div class="content_sortPagiBar">
               <div class="bottom-pagination-content clearfix">
                   {include file="./product-compare.tpl" paginationId='bottom'}
                   {include file="./pagination.tpl" paginationId='bottom'}
               </div>
           </div>
       {/if}

end remove wrapping {if $products}{/if}, so the content needed to be displayed

 

2. Open <theme_folder>/product-list.tpl

Find lines at the bottom of file

{addJsDef comparedProductsIds=$compared_products}
{/if}

and add additional contodion to {if}

{addJsDef comparedProductsIds=$compared_products}
{else}
    <ul{if isset($id) && $id} id="{$id}"{/if} class="product_list grid row{if isset($class) && $class} {$class}{/if}">
    </ul>
{/if}

This ul.product_list needed to put content in there by ajax by blocklayered

 

3. Open <theme_folder>/js/modules/blocklayered/blocklayered.js and find initLayered() function.

function initLayered()
{
    initFilters();
    initSliders();
    initLocationChange();
    updateProductUrl();
    if (window.location.href.split('#').length == 2 && window.location.href.split('#')[1] != '')
    {
        var params = window.location.href.split('#')[1];
        reloadContent('&selected_filters='+params);
    }
}

 

Extend condition like this

   if (window.location.href.split('#').length == 2 && window.location.href.split('#')[1] != '')
   {
       var params = window.location.href.split('#')[1];
       reloadContent('&selected_filters='+params);
   } else {
       // Reload content if no filters enabled, to show all products on category page
       reloadContent(true);
   }

So when page is loaded and no filters are applied blocklayered will return all products withiout filtering on accessing that page.

 

Also you need to enable diplaying products from subcategories in module configuration.

Edited by aprex (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

My solution for displaying all product from subcategories on category page for Prestashop 1.6.0.11

After hours of debug I discovered that blocklayered module can do the trick. Common idea is to show all products filtered by blocklayered. So some configuration is needed.

 

1. Open <theme_folder>/category.tpl

Find lines

       {if $products}
           <div class="content_sortPagiBar clearfix">
               <div class="sortPagiBar clearfix">
                   {include file="./product-sort.tpl"}
                   {include file="./nbr-product-page.tpl"}
               </div>
               <div class="top-pagination-content clearfix">
                   {include file="./product-compare.tpl"}
                   {include file="$tpl_dir./pagination.tpl"}
               </div>
           </div>
           {include file="./product-list.tpl" products=$products}
           <div class="content_sortPagiBar">
               <div class="bottom-pagination-content clearfix">
                   {include file="./product-compare.tpl" paginationId='bottom'}
                   {include file="./pagination.tpl" paginationId='bottom'}
               </div>
           </div>
       {/if}

end remove wrapping {if $products}{/if}, so the content needed to be displayed

 

2. Open <theme_folder>/product-list.tpl

Find lines at the bottom of file

{addJsDef comparedProductsIds=$compared_products}
{/if}

and add additional contodion to {if}

{addJsDef comparedProductsIds=$compared_products}
{else}
    <ul{if isset($id) && $id} id="{$id}"{/if} class="product_list grid row{if isset($class) && $class} {$class}{/if}">
    </ul>
{/if}

This ul.product_list needed to put content in there by ajax by blocklayered

 

3. Open <theme_folder>/js/modules/blocklayered/blocklayered.js and find initLayered() function.

function initLayered()
{
    initFilters();
    initSliders();
    initLocationChange();
    updateProductUrl();
    if (window.location.href.split('#').length == 2 && window.location.href.split('#')[1] != '')
    {
        var params = window.location.href.split('#')[1];
        reloadContent('&selected_filters='+params);
    }
}

 

Extend condition like this

   if (window.location.href.split('#').length == 2 && window.location.href.split('#')[1] != '')
   {
       var params = window.location.href.split('#')[1];
       reloadContent('&selected_filters='+params);
   } else {
       // Reload content if no filters enabled, to show all products on category page
       reloadContent(true);
   }

So when page is loaded and no filters are applied blocklayered will return all products withiout filtering on accessing that page.

 

Also you need to enable diplaying products from subcategories in module configuration.

 

 

@aprex nice piece of code, but unfortunetaly it does not work on 1.6.0.11 in my case, that is weird as I am triing few different methods and nothing works, even on clean install with default template. Do you have any idea why could it be?

  • Like 1
Link to comment
Share on other sites

@aprex nice piece of code, but unfortunetaly it does not work on 1.6.0.11 in my case, that is weird as I am triing few different methods and nothing works, even on clean install with default template. Do you have any idea why could it be?

Have you configured layer navigation module filters?

Link to comment
Share on other sites

Hi,

 

Thank you. It works. The only thing I modified was a line:

$sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));

to:

$sub=new Category(intval($subcategory['id_category']),intval($this->context->language->id));

Best regards,

Darek

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...

My solution for displaying all product from subcategories on category page for Prestashop 1.6.0.11

After hours of debug I discovered that blocklayered module can do the trick. Common idea is to show all products filtered by blocklayered. So some configuration is needed.

 

1. Open <theme_folder>/category.tpl

Find lines

       {if $products}
           <div class="content_sortPagiBar clearfix">
               <div class="sortPagiBar clearfix">
                   {include file="./product-sort.tpl"}
                   {include file="./nbr-product-page.tpl"}
               </div>
               <div class="top-pagination-content clearfix">
                   {include file="./product-compare.tpl"}
                   {include file="$tpl_dir./pagination.tpl"}
               </div>
           </div>
           {include file="./product-list.tpl" products=$products}
           <div class="content_sortPagiBar">
               <div class="bottom-pagination-content clearfix">
                   {include file="./product-compare.tpl" paginationId='bottom'}
                   {include file="./pagination.tpl" paginationId='bottom'}
               </div>
           </div>
       {/if}

end remove wrapping {if $products}{/if}, so the content needed to be displayed

 

2. Open <theme_folder>/product-list.tpl

Find lines at the bottom of file

{addJsDef comparedProductsIds=$compared_products}
{/if}

and add additional contodion to {if}

{addJsDef comparedProductsIds=$compared_products}
{else}
    <ul{if isset($id) && $id} id="{$id}"{/if} class="product_list grid row{if isset($class) && $class} {$class}{/if}">
    </ul>
{/if}

This ul.product_list needed to put content in there by ajax by blocklayered

 

3. Open <theme_folder>/js/modules/blocklayered/blocklayered.js and find initLayered() function.

function initLayered()
{
    initFilters();
    initSliders();
    initLocationChange();
    updateProductUrl();
    if (window.location.href.split('#').length == 2 && window.location.href.split('#')[1] != '')
    {
        var params = window.location.href.split('#')[1];
        reloadContent('&selected_filters='+params);
    }
}

 

Extend condition like this

   if (window.location.href.split('#').length == 2 && window.location.href.split('#')[1] != '')
   {
       var params = window.location.href.split('#')[1];
       reloadContent('&selected_filters='+params);
   } else {
       // Reload content if no filters enabled, to show all products on category page
       reloadContent(true);
   }

So when page is loaded and no filters are applied blocklayered will return all products withiout filtering on accessing that page.

 

Also you need to enable diplaying products from subcategories in module configuration.

 

 

thx you saved my day :) this is what i search for

Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...
  • 4 weeks later...

I have successfully done this in prestashop 1.6.1.0

I studied the thread and created my own version, here is my solution

 

first open controllers/front/CategoryController.php find function assignSubcategories() and replace it with this code.

protected function assignSubcategories()
{
    if ($sub_categories = $this->category->getSubCategories($this->context->language->id))
    {
            $subcategory_objects=array();
            $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));              
            foreach ($sub_categories as $subcategory)
                  {
                   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
                   $subcategory_objects[$subcategory['id_category']]=$sub;
               }
               //var_dump($subcategory_objects);
               $this->context->smarty->assign("subcategories_objects",$subcategory_objects);    
    
    
        $this->context->smarty->assign(array(
            'subcategories'          => $sub_categories,
            'subcategories_nb_total' => count($sub_categories),
            'subcategories_nb_half'  => ceil(count($sub_categories) / 2)
        ));
    }
}

2nd step is to open "your theme"/category.tpl and find this code

<div class="content_sortPagiBar clearfix">
	<div class="sortPagiBar clearfix">
		{include file="./product-sort.tpl"}
		{include file="./nbr-product-page.tpl"}
	</div>
	<div class="top-pagination-content clearfix">
		{include file="./product-compare.tpl"}
		{include file="$tpl_dir./pagination.tpl"}
	</div>
</div>

{include file="./product-list.tpl" products=$products}	


Replace it with

<div class="content_sortPagiBar clearfix">
	<div class="sortPagiBar clearfix">
		{include file="./product-sort.tpl"}
		{include file="./nbr-product-page.tpl"}
	</div>
	<div class="top-pagination-content clearfix">
		{include file="./product-compare.tpl"}
		{include file="$tpl_dir./pagination.tpl"}
	</div>
</div>

{if isset($subcategories)}			
	{foreach from=$subcategories item=subcategory}

		  {assign var="subcategory_id" value=$subcategory.id_category}
		  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
	  
			{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
	  
		{/foreach}
	
	{else}
	{include file="./product-list.tpl" products=$products}			
{/if}

3rd Step, This is not mentioned in the thread but this is the most important part.

copy your product-list.tpl and rename it as product-list-sub.tpl

<!-- Products list -->
<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>

you can add this code to product-list-sub.tpl to show the name of the category

 

and that's it, Thank you guys.

Edited by aljon ngo (see edit history)
  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...
  • 4 weeks later...

I have successfully done this in prestashop 1.6.1.0

I studied the thread and created my own version, here is my solution

 

first open controllers/front/CategoryController.php find function assignSubcategories() and replace it with this code.

protected function assignSubcategories()
{
    if ($sub_categories = $this->category->getSubCategories($this->context->language->id))
    {
            $subcategory_objects=array();
            $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));              
            foreach ($sub_categories as $subcategory)
                  {
                   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
                   $subcategory_objects[$subcategory['id_category']]=$sub;
               }
               //var_dump($subcategory_objects);
               $this->context->smarty->assign("subcategories_objects",$subcategory_objects);    
    
    
        $this->context->smarty->assign(array(
            'subcategories'          => $sub_categories,
            'subcategories_nb_total' => count($sub_categories),
            'subcategories_nb_half'  => ceil(count($sub_categories) / 2)
        ));
    }
}

2nd step is to open "your theme"/category.tpl and find this code

<div class="content_sortPagiBar clearfix">
	<div class="sortPagiBar clearfix">
		{include file="./product-sort.tpl"}
		{include file="./nbr-product-page.tpl"}
	</div>
	<div class="top-pagination-content clearfix">
		{include file="./product-compare.tpl"}
		{include file="$tpl_dir./pagination.tpl"}
	</div>
</div>

{include file="./product-list.tpl" products=$products}	


Replace it with

<div class="content_sortPagiBar clearfix">
	<div class="sortPagiBar clearfix">
		{include file="./product-sort.tpl"}
		{include file="./nbr-product-page.tpl"}
	</div>
	<div class="top-pagination-content clearfix">
		{include file="./product-compare.tpl"}
		{include file="$tpl_dir./pagination.tpl"}
	</div>
</div>

{if isset($subcategories)}			
	{foreach from=$subcategories item=subcategory}

		  {assign var="subcategory_id" value=$subcategory.id_category}
		  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
	  
			{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
	  
		{/foreach}
	
	{else}
	{include file="./product-list.tpl" products=$products}			
{/if}

3rd Step, This is not mentioned in the thread but this is the most important part.

copy your product-list.tpl and rename it as product-list-sub.tpl

<!-- Products list -->
<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>

you can add this code to product-list-sub.tpl to show the name of the category

 

and that's it, Thank you guys.

 Thank you! you really helped

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...

Hi,

 
The code given by aljon ngo is working. Thank you very much.
 
However, products are divided by categories. Do you know how i can display all products orderd by id or date added ?
 
Also, there is no pagination with this code. How can i add pagination ? 
 
in product-list.tpl, there is :
{include file="./product-list.tpl" products=$products}
But it’s not included in product-list-sub.tpl 
Edited by Cloud Nine (see edit history)
Link to comment
Share on other sites

I wanted to display all products. I’ve modified homefeatured in order to display all products from category home with pagination.
 
I know we can use block layered navigation module (see that post) but i have an issue with pagination with this module (see that post)
 
I’ve use this code :
 
(works on Prestashop 1.6.1.1)
 
I’ve added all products to home with a sql query :
INSERT IGNORE INTO ps_category_product SELECT 2, id_product, ((SELECT MAX(position) FROM ps_category_product WHERE id_category = 2) + (@inc := @inc + 1)) FROM ps_product INNER JOIN (SELECT @inc := 0) AS i

source : 

 

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

  • 3 weeks later...

BZ I just want to thank you for the easy to follow formatting.  Anyone willing to help others is gold in my book but I think too many of them think most have a similar expertise as them. I have pulled out hair trying to follow some other people.

 

You on the other hand broke it up and laid it out perfectly.

Here is my Tutorial (Tested with PrestaShop v. 1.5.2.0 AND 1.5.4.1):

open this file: controllers/front/CategoryController.php

search this lines:

if ($subCategories = $this->category->getSubCategories($this->context->language->id))
  {
Link to comment
Share on other sites

  • 3 weeks later...
  • 2 months later...

Here is my Tutorial (Tested with PrestaShop v. 1.5.2.0 AND 1.5.4.1):

 

open this file: controllers/front/CategoryController.php

 

search this lines:

if ($subCategories = $this->category->getSubCategories($this->context->language->id))
  {
insert this code after the: {

   $subcategory_objects=array();
   $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));
  
   foreach ($subCategories as $subcategory)
		  {
		   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
		   $subcategory_objects[$subcategory['id_category']]=$sub;
	   }
	   //var_dump($subcategory_objects);
	   $this->context->smarty->assign("subcategories_objects",$subcategory_objects);
save this file and upload.

 

now open the file: themes/"your_theme"/category.tpl

 

search this lines:

   <div class="content_sortPagiBar">
	{include file="$tpl_dir./pagination.tpl"}
	<div class="sortPagiBar clearfix">
	 {include file="./product-sort.tpl"}
	 {include file="./product-compare.tpl"}
	 {include file="./nbr-product-page.tpl"}
	</div>
   </div>
insert after the </div></div>:

	   	 {if isset($subcategories)}
			
				{foreach from=$subcategories item=subcategory}
	
						  {assign var="subcategory_id" value=$subcategory.id_category}
						  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
					  
		   					{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
					  
		   				{/foreach}
					
					{else}
				
						{include file="./product-list.tpl" products=$products}
				
					{/if}
save and upload.

 

now open this file: themes/"your_theme"/product-list.tpl

 

search this line:

<!-- Products list -->
after the line insert this:

<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>
search this line:

{foreach from=$products item=product name=products}
after this line add:

{if $products != ''}
now search this lines:

<a class="button lnk_view" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a>
   </div>
  </li>
after this lines add:

{else}

  No Products in this Category

{/if}
save the product-list.tpl as product-list-sub.tpl and upload the files.

 

DONE!!!

 

I hope it will works... contact me if you have problems....

 

sorry for my bad english

 

best regards

 

It worked for me but when I use blocklayered, it use product-list.tpl so everything is broken . Any idea for blocklayered to order subcategories the same way ?

Thanks

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

assuming you're working with aljon ngo's version, the product loop is inside an {if $products} which will be false if there are no product in your (home) category,

 

instant solution is to associate some product to this category, template solution is to get this loop out of the condition.

Link to comment
Share on other sites

  • 1 month later...

Hello, if you need show all products from subcategories in parent category, here's what you need to do:
 
This is my own solution to this problem.
This solution works great on versions 1.6.1 and higher.
 
Open file classes/Category.php and find function getProducts()
 
Inside at the top of the function code, write this:
 
 

		$id_parent = (int)$this->id;
		$parent = new Category((int)$id_parent);

Below in function find this code:

		/** Return only the number of products */
		if ($get_total) {
			$sql = 'SELECT COUNT(cp.`id_product`) AS total
					FROM `'._DB_PREFIX_.'product` p
					'.Shop::addSqlAssociation('product', 'p').'
					LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
					WHERE cp.`id_category` = '.(int)$this->id.
				($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
				($active ? ' AND product_shop.`active` = 1' : '').
				($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '');

			return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
		}

And replace this code, on this one:

		/** Return only the number of products */
		if ($get_total) {
			$sql = 'SELECT COUNT(cp.`id_product`) AS total
					FROM `'._DB_PREFIX_.'product` p
					'.Shop::addSqlAssociation('product', 'p').'
					LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
					LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category)
					WHERE c.nleft >= '.(int)$parent->nleft.' AND c.nright <= '.(int)$parent->nright.' AND c.active = 1
				'.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
				($active ? ' AND product_shop.`active` = 1' : '').
				($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '');

			return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
		}

You simple need add one LEFT JOIN:

LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category)

And replace WHERE in sql request on this:

WHERE c.nleft >= '.(int)$parent->nleft.' AND c.nright <= '.(int)$parent->nright.' AND c.active = 1

Below in function find this code:

		$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute,
					product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`,
					pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image,
					il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default,
					DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00",
					INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice
				FROM `'._DB_PREFIX_.'category_product` cp
				LEFT JOIN `'._DB_PREFIX_.'product` p
					ON p.`id_product` = cp.`id_product`
				'.Shop::addSqlAssociation('product', 'p').
				(Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop
				ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').'
				'.Product::sqlStock('p', 0).'
				INNER JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category AND c.nleft >= '.(int)$parent->nleft.' AND c.nright <= '.(int)$parent->nright : 'c.id_category = '.(int)$id_parent).' AND c.active = 1)
				LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
					ON (product_shop.`id_category_default` = cl.`id_category`
					AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
				LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
					ON (p.`id_product` = pl.`id_product`
					AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
				LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop
					ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.')
				LEFT JOIN `'._DB_PREFIX_.'image_lang` il
					ON (image_shop.`id_image` = il.`id_image`
					AND il.`id_lang` = '.(int)$id_lang.')
				LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
					ON m.`id_manufacturer` = p.`id_manufacturer`
				WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
					AND cp.`id_category` = '.(int)$this->id
					.($active ? ' AND product_shop.`active` = 1' : '')
					.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
					.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');

And replace on this one:

		$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute,
					product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`,
					pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image,
					il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default,
					DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00",
					INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice
				FROM `'._DB_PREFIX_.'category_product` cp
				LEFT JOIN `'._DB_PREFIX_.'product` p
					ON p.`id_product` = cp.`id_product`
				'.Shop::addSqlAssociation('product', 'p').
				(Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop
				ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').'
				'.Product::sqlStock('p', 0).'
				LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category)
				LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
					ON (product_shop.`id_category_default` = cl.`id_category`
					AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
				LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
					ON (p.`id_product` = pl.`id_product`
					AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
				LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop
					ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.')
				LEFT JOIN `'._DB_PREFIX_.'image_lang` il
					ON (image_shop.`id_image` = il.`id_image`
					AND il.`id_lang` = '.(int)$id_lang.')
				LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
					ON m.`id_manufacturer` = p.`id_manufacturer`
				WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
					AND c.nleft >= '.(int)$parent->nleft.' AND c.nright <= '.(int)$parent->nright.' AND c.active = 1
					'.($active ? ' AND product_shop.`active` = 1' : '')
					.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
					.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');

Here you need do all the same, add LEFT JOIN and change WHERE section in sql request.
 
Thats all!!!
 
PLEASE NOT: If you use blocklayered module, you dont need this fix, just turn on show product from subcategories in module settings.

 

Also i attach file, you can download this file and upload inside this folder: /override/classes/

 

Category.php

 

Dont forget delete after this file: /cache/class_index.php

If you already have this file inside override folder, you need copy getProducts function from this file and write this function inside your old file.

 

 

Link to comment
Share on other sites

  • 2 months later...

Good day everyone! I'm newbie from Prestashop. I'm using Prestashop 1.6.1.7; how can I edit category name and sub category without editing the codes, just using the admin control panel? I just want to maximize the Prestashop as it is without buying modules, due to budget constraint. Thank you and I look forward to hear from you.

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

I find this error

Fatal error: Uncaught --> Smarty: Unable to load template file 'F:\01.xamp\htdocs\prestashop/themes/pf_flipcart/./product-list-sub.tpl' in 'F:\01.xamp\htdocs\prestashop/themes/pf_flipcart/category.tpl' <-- thrown in F:\01.xamp\htdocs\prestashop\tools\smarty\sysplugins\smarty_internal_templatebase.php on line 129

Link to comment
Share on other sites

  • 2 months later...

Hi

Based on #67, my solution for Prestashop 1.6.1.7 (maybe cleaner with controller override...)

 

Create a new file CategoryController.php in override/controllers/front/

class CategoryController extends CategoryControllerCore
{
	protected function assignSubcategories()
	{
		if ($sub_categories = $this->category->getSubCategories($this->context->language->id)) {
			$subcategory_objects=array();
			$this->context->smarty->assign("andr_lang", $this->context->language->id);
			foreach ($sub_categories as $subcategory){
				$sub = new Category(intval($subcategory['id_category']), $this->context->language->id);
				$subcategory_objects[$subcategory['id_category']]=$sub;
			}

			//var_dump($subcategory_objects);
			$this->context->smarty->assign("subcategories_objects", $subcategory_objects);    
			$this->context->smarty->assign(array(
				'subcategories'          => $sub_categories,
				'subcategories_nb_total' => count($sub_categories),
				'subcategories_nb_half'  => ceil(count($sub_categories) / 2)
			));
		}
	}

}

Don't forget to delete the cache...

 

Best

Link to comment
Share on other sites

  • 3 months later...
  • 3 months later...
  • 1 month later...
  • 6 months later...
  • 1 month later...
On 30/08/2015 at 7:21 PM, aljon ngo said:

I have successfully done this in prestashop 1.6.1.0

I studied the thread and created my own version, here is my solution

 

first open controllers/front/CategoryController.php find function assignSubcategories() and replace it with this code.


protected function assignSubcategories()
{
    if ($sub_categories = $this->category->getSubCategories($this->context->language->id))
    {
            $subcategory_objects=array();
            $this->context->smarty->assign("andr_lang",intval($cookie->id_lang));              
            foreach ($sub_categories as $subcategory)
                  {
                   $sub=new Category(intval($subcategory['id_category']),intval($cookie->id_lang));
                   $subcategory_objects[$subcategory['id_category']]=$sub;
               }
               //var_dump($subcategory_objects);
               $this->context->smarty->assign("subcategories_objects",$subcategory_objects);    
    
    
        $this->context->smarty->assign(array(
            'subcategories'          => $sub_categories,
            'subcategories_nb_total' => count($sub_categories),
            'subcategories_nb_half'  => ceil(count($sub_categories) / 2)
        ));
    }
}

2nd step is to open "your theme"/category.tpl and find this code


<div class="content_sortPagiBar clearfix">
	<div class="sortPagiBar clearfix">
		{include file="./product-sort.tpl"}
		{include file="./nbr-product-page.tpl"}
	</div>
	<div class="top-pagination-content clearfix">
		{include file="./product-compare.tpl"}
		{include file="$tpl_dir./pagination.tpl"}
	</div>
</div>

{include file="./product-list.tpl" products=$products}	

Replace it with


<div class="content_sortPagiBar clearfix">
	<div class="sortPagiBar clearfix">
		{include file="./product-sort.tpl"}
		{include file="./nbr-product-page.tpl"}
	</div>
	<div class="top-pagination-content clearfix">
		{include file="./product-compare.tpl"}
		{include file="$tpl_dir./pagination.tpl"}
	</div>
</div>

{if isset($subcategories)}			
	{foreach from=$subcategories item=subcategory}

		  {assign var="subcategory_id" value=$subcategory.id_category}
		  {assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
	  
			{include file="./product-list-sub.tpl" products=$subcategory_object->getProducts('1','1','100')}
	  
		{/foreach}
	
	{else}
	{include file="./product-list.tpl" products=$products}			
{/if}

3rd Step, This is not mentioned in the thread but this is the most important part.

copy your product-list.tpl and rename it as product-list-sub.tpl


<!-- Products list -->
<h4>{$subcategory.name|escape:'htmlall':'UTF-8'}</h4>

you can add this code to product-list-sub.tpl to show the name of the category

 

and that's it, Thank you guys.

 

I have a problem with this solution in 1.6.1 : Product images are not showing, the link appears broken. Please see https://stackoverflow.com/questions/52218895/prestashop-product-images-not-showing-in-category-loop-with-friendly-urls 

Link to comment
Share on other sites

  • 1 year later...
9 hours ago, ndiaga said:

There  is  a module  that  does  the same for 1.7   but  it is  for  the  home page  listing.

Here it is:

 

Thank you so much ndiaga ! Do you think this module can also do this listing in category page ? Not only in homepage. If so I definitely want to buy it :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...