Jump to content

How to show the full breadcrumb to the Default Category on EVERY product page


TWDesign
 Share

Recommended Posts

The default behavior of PrestaShop re breadcrumbs seems a little screwy to me.

 

Lets say I am on the "CATEGORY A" page and use the search box feature.

Lets say the product I am looking for is somewhere in "CATEGORY A".

If I click the search result it will take me to the product page but the breadcrumb

will only show something like >>category A>>product 123

 

BUT.... what if the product is actually buried one or two deep in the sub-categories.

Even having the sub-category correctly set as "Default Category" doesn't seem to change this behavior.

 

What I really want to see is something like >>category A>>sub-category X>>product 123

 

It seems screwy because if I happen to be in "CATEGORY B" at the time I do the search,

it WILL return the full breadcrumb trail down to the Default Category.

 

How can I change this behavior.

 

(btw - looks like another Module opportunity for somebody clever).

Share this post


Link to post
Share on other sites

  • 1 month later...
  • 4 months later...

Morning

I have a question more about this topic ... I dont´t know if I could resolve my problem with your module (HA!*!*). I explain my case below ...

 

I have a shop with a lot of products and 15 cats, and sometimes a product can be into several cats. I´ve got to have unique metas and urls but I don´t know how make the same for the breadcrumb.

 

Ex:

 

Product A = Cat 1 - Sub cat 2

Product A = Cat 3 - Sub cat 3

Product A = Home - Sales

 

I need 3 breadcrumb:

 

- Home > Cat 1 > Subcat2 > Product A

- Home > Cat 3 > Subcat3 > Product A

- Home > Sales > Product A

 

Prestashop only shows the first breadcrumb because It always takes the category default (Subcat 2).

 

Does anyone know how solve the problem with the breadcrumb??

 

Thx a lot and regards.

Share this post


Link to post
Share on other sites

In the Product.php Class, where get the product path, remove the second parameter and you'll get the full path, but only if the product has default category. Sorry i'm on mobile now and i can't post the code. Tell me if you resolve

Share this post


Link to post
Share on other sites

Hi Bluego78,

Now, I have uploaded all products in my catalog with unique url and metas (Ex: www.xxxxxxx.com/catalog/product-name) and this product is into 3 subcategories (Ex; SubA, SubB, Offer) and I need:

 

- Keep my unique url: www.xxxxxxx.com/catalog/product-name

 

- Breadcrumb 1: Home > Cat 1 > SubA > Product name

- Breadcrumb 2: Home > Cat 2 > SubB > Product name

- Breadcrumb 3: Home > Offer > Product name

 

> Is it possible with the change in te Product.php class

 

Thx a lot for your answer!

Share this post


Link to post
Share on other sites

Hi Bluego78,

Now, I have uploaded all products in my catalog with unique url and metas (Ex: www.xxxxxxx.com/catalog/product-name) and this product is into 3 subcategories (Ex; SubA, SubB, Offer) and I need:

 

- Keep my unique url: www.xxxxxxx.com/catalog/product-name

 

- Breadcrumb 1: Home > Cat 1 > SubA > Product name

- Breadcrumb 2: Home > Cat 2 > SubB > Product name

- Breadcrumb 3: Home > Offer > Product name

 

> Is it possible with the change in te Product.php class

 

Thx a lot for your answer!

 

Hi, sorry, i've checked now that i'm in office... the change is to made to ProductController.php not in Product.php class.

 

In the function assignCategory()

 

Tools::getPath let you get the path...

 

which version of prestashop are you using?

  • Like 1

Share this post


Link to post
Share on other sites

I´m using 1.4.8.2 version

Thx

 

Oh.. i've done! But i use 1.5.2 but i think you can adapt my script, it works :-)

 

In ProductController.php modify the assignCategory() method like this:

 


protected function assignCategory()
{
      // Assign category to the template
      if ($this->category !== false && Validate::isLoadedObject($this->category))
      {
         $all_product_subs = Product::getProductCategoriesFull($this->product->id, $this->context->language->id);
         if(isset($all_product_subs) && count($all_product_subs)>0)
         {
             foreach($all_product_subs as $subcat)
             $all_product_subs_path[] = Tools::getPath($subcat['id_category'], '', true);
         } 

         $this->context->smarty->assign(array(
         'path' => Tools::getPath($this->category->id, '', true), 
         //THIS CONTAINS ALL PRODUCT SUBCATEGORIES PATH
         'all_product_subs'=>$all_product_subs_path,
         'category' => $this->category,
         'subCategories' => $this->category->getSubCategories($this->context->language->id, true),
         'id_category_current' => (int)$this->category->id,
         'id_category_parent' => (int)$this->category->id_parent,
         'return_category_name' => Tools::safeOutput($this->category->name)
         ));
     }
     else
     $this->context->smarty->assign('path', Tools::getPath((int)$this->product->id_category_default, $this->product->name));

     $this->context->smarty->assign('categories', Category::getHomeCategories($this->context->language->id));
     $this->context->smarty->assign(array('HOOK_PRODUCT_FOOTER' => Hook::exec('displayFooterProduct', array('product' => $this->product, 'category' => $this->category))));
}

 

 

So you'll get all categories path in "all_product_subs", then in your breadcrumb.tpl add:

 

{if isset($all_product_subs)}
     {foreach from=$all_product_subs item=prod_subcategory}
        <br />
        <a href="{$base_dir}" title="{l s='return to'} {l s='Home'}">{l s='Home'}</a>
        <span class="navigation-pipe">{$navigationPipe|escape:html:'UTF-8'}</span>
           {if !$prod_subcategory|strpos:'span'}<span class="navigation_page">{$prod_subcategory}</span>
           {else}
           {$prod_subcategory}
           {/if}
       {/foreach}
    {/if}

 

this works fine :-)

  • Like 2

Share this post


Link to post
Share on other sites

Ok let me know.

 

Maybe it's better if you put also

 

$all_product_subs_path = array(); //<-- ADD THIS LINE

$all_product_subs = Product::getProductCategoriesFull($this->product->id, $this->context->language->id);

if(isset($all_product_subs) && count($all_product_subs)>0)

{

foreach($all_product_subs as $subcat)

...

 

and put the template code in the product.tpl not in breadcrumb.tpl, it's better :-)

Bye

Share this post


Link to post
Share on other sites

I've implement the method to get only the "finals" category, because if the product is associated to (example):

 

computer -> hardware -> monitor

and

office -> display -> lcd

 

 

you'll get:

 

computer

computer -> hardware

computer -> hardware ->monitor

office

office -> display

office -> display -> lcd

 

but if you need only monitor and lcd modify the script as this:

 

  $all_product_subs = Product::getProductCategoriesFull($this->product->id, $this->context->language->id);
  $all_product_subs_path = array();
  if(isset($all_product_subs) && count($all_product_subs)>0)
  {
  foreach($all_product_subs as $subcat)
  {
	 $sub_category = new Category($subcat['id_category'], $this->context->language->id);
	 $sub_category_children = $sub_category->getSubCategories($this->context->language->id, $active = true);
	 if(count($sub_category_children)<=0)
	 if($subcat['id_category']!=$this->product->id_category_default)//this remove the default category from the list
	 if(Tools::getPath($subcat['id_category'], '', true)!='')
	 $all_product_subs_path[] = Tools::getPath($subcat['id_category'], '', true);
  }
  }

 

so you'll get:

computer -> hardware -> monitor

office -> display -> lcd

Edited by bluego78 (see edit history)

Share this post


Link to post
Share on other sites

  • 2 weeks later...

Just wanted to extend some thanks - this saved me untold hours of trying to reverse engineer the workings of the breadcrumb/category hierarchy system. I love the potential of Prestashop but the documentation is so woefully lacking that it's almost hard to recommend to other developers Too late for me now that I'm already 6 weeks into a 3 week project...

 

Just some background to my problem for anyone else that may have encountered the same thing.

 

My products had all been uploaded through the Store Manager system's CSV import. This process will correctly place products in nested categories, but won't automatically associate those items with the relevant parent categories. The end result means the breadcrumbs would not display the complete path on a product page (at least it had this effect in my case).

 

Your override to the ProductController worked a charm.

 

Thanks again!!

Share this post


Link to post
Share on other sites

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

All the categories and subcategories have link but the product name don´t have link and this when is very important to improve SEO

 

Home > Summer > Shoes > Nike evo

 

How can put backlink in product name of breadcrumb?

Edited by seog (see edit history)

Share this post


Link to post
Share on other sites

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

on my 1.5 install, I've overrided ProductController like this :

 

/override/controllers/front/ProductController.php

class ProductController extends ProductControllerCore
{

    public function initContent()
    {
         parent::initContent();
         if (Validate::isLoadedObject($this->product)) 
         {
             $id_category = $this->product->id_category_default;
             $fullpath = Tools::getPath($id_category , $this->product->name, true);
             $this->context->smarty->assign('path', $fullpath);
         }
     }

}
 

Share this post


Link to post
Share on other sites

  • 2 weeks later...

 

on my 1.5 install, I've overrided ProductController like this :

 

/override/controllers/front/ProductController.php

class ProductController extends ProductControllerCore
{

    public function initContent()
    {
         parent::initContent();
         if (Validate::isLoadedObject($this->product)) 
         {
             $id_category = $this->product->id_category_default;
             $fullpath = Tools::getPath($id_category , $this->product->name, true);
             $this->context->smarty->assign('path', $fullpath);
         }
     }

}

 

 

In my version 1.5.4.1 not work - in breadcrumb still: home category->default category->product name - it's not full path of category to this product :(

Edited by And63 (see edit history)

Share this post


Link to post
Share on other sites

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

Hey there guys, I have the exact same problem for PS1.6

 

1 product is in cat. men, women and home. breadcrumb only displays the default category, never the category you come from. Problematic as women end up in men and other way around. If default cat is home then the breadcrumb is useluss to have at all..

 

Help much appreciated!

 

Thanks

 

Tim

Share this post


Link to post
Share on other sites

  • 1 year later...

Hey there,

I used parts of scripts above adaped them for v 1.6. I am not good at Prestashop and someone may find this solution ugly but it works fine for me. Either you click on latest product at homepage or use the search field,  your breadcrumb navigation shows a full path to the deepest category (assuming they are all checked - I am not sure). Searching from the menu remains unchanged.

Function  assignCategory(), preferably in override/controllers/front/ProductController.php

    protected function assignCategory()
    {
        // Assign category to the template
        if ($this->category !== false && Validate::isLoadedObject($this->category) && $this->category->inShop() && $this->category->isAssociatedToShop()) {
            $path = Tools::getPath($this->category->id, $this->product->name, true);
			$all_product_subs = Product::getProductCategoriesFull($this->product->id, $this->context->language->id);
			
         	if(isset($all_product_subs) && count($all_product_subs)>0)
         	{
             	foreach($all_product_subs as $subcat)
			 	$all_product_subs_path = array(); //reset array, only last one is used
             	$all_product_subs_path[] = Tools::getPath($subcat['id_category'], '', true);
         	} 
		 
        } elseif (Category::inShopStatic($this->product->id_category_default, $this->context->shop)) {
            $this->category = new Category((int)$this->product->id_category_default, (int)$this->context->language->id);
            if (Validate::isLoadedObject($this->category) && $this->category->active && $this->category->isAssociatedToShop()) {
                $path = Tools::getPath((int)$this->product->id_category_default, $this->product->name);
            }
        }
        if (!isset($path) || !$path) {
            $path = Tools::getPath((int)$this->context->shop->id_category, $this->product->name);
        }

        if (Validate::isLoadedObject($this->category)) {		

         // various assignements before Hook::exec
           $this->context->smarty->assign(array(
         'path' => Tools::getPath($this->category->id, '', true), 
         //THIS CONTAINS ALL PRODUCT SUBCATEGORIES PATH
         'all_product_subs'=>$all_product_subs_path,
         'category' => $this->category,
         'subCategories' => $this->category->getSubCategories($this->context->language->id, true),
         'id_category_current' => (int)$this->category->id,
         'id_category_parent' => (int)$this->category->id_parent,
         'return_category_name' => Tools::safeOutput($this->category->name)
         ));
		 
        }
        $this->context->smarty->assign(array('HOOK_PRODUCT_FOOTER' => Hook::exec('displayFooterProduct', array('product' => $this->product, 'category' => $this->category))));
    }

themes/(default-bootstrap)/breadcrumb.tpl

<!-- Breadcrumb -->
{if isset($smarty.capture.path)}{assign var='path' value=$smarty.capture.path}{/if}
<div class="breadcrumb clearfix">
	<a class="home" href="{if isset($force_ssl) && $force_ssl}{$base_dir_ssl}{else}{$base_dir}{/if}" title="{l s='Return to Home'}"><i class="icon-home"></i></a>
	{if isset($all_product_subs)}    
    
     {foreach from=$all_product_subs item=prod_subcategory}
        <span class="navigation-pipe">{$navigationPipe|escape:html:'UTF-8'}</span>
   
		{if $prod_subcategory|strpos:'span'}
			<span class="navigation_page">{$prod_subcategory|@replace:'<a ': '<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" '|@replace:'data-gg="">': '><span itemprop="title">'|@replace:'</a>': '</span></a></span>'}<span class="navigation-pipe">></span>{$product->name}</span>
		{else}
			{$prod_subcategory}
		{/if}          
       {/foreach}  
         
    {else}
    
         
		<span class="navigation-pipe"{if isset($category) && isset($category->id_category) && $category->id_category == (int)Configuration::get('PS_ROOT_CATEGORY')} style="display:none;"{/if}>{$navigationPipe|escape:'html':'UTF-8'}</span>
		{if $path|strpos:'span' !== false}
			<span class="navigation_page">{$path|@replace:'<a ': '<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" '|@replace:'data-gg="">': '><span itemprop="title">'|@replace:'</a>': '</span></a></span>'}</span>
		{else}
			{$path}
		{/if}
	{/if}
</div>
{if isset($smarty.get.search_query) && isset($smarty.get.results) && $smarty.get.results > 1 && isset($smarty.server.HTTP_REFERER)}
<div class="pull-right">
	<strong>
		{capture}{if isset($smarty.get.HTTP_REFERER) && $smarty.get.HTTP_REFERER}{$smarty.get.HTTP_REFERER}{elseif isset($smarty.server.HTTP_REFERER) && $smarty.server.HTTP_REFERER}{$smarty.server.HTTP_REFERER}{/if}{/capture}
		<a href="{$smarty.capture.default|escape:'html':'UTF-8'|secureReferrer|regex_replace:'/[\?|&]content_only=1/':''}" name="back">
			<i class="icon-chevron-left left"></i> {l s='Back to Search results for "%s" (%d other results)' sprintf=[$smarty.get.search_query,$smarty.get.results]}
		</a>
	</strong>
</div>
{/if}
<!-- /Breadcrumb -->

 

Edited by ferdinand64
note refinement (see edit history)

Share this post


Link to post
Share on other sites

  • 6 months later...
On 2016-11-23 at 9:37 AM, TimTamTom said:

Hey there guys, I have the exact same problem for PS1.6

 

1 product is in cat. men, women and home. breadcrumb only displays the default category, never the category you come from. Problematic as women end up in men and other way around. If default cat is home then the breadcrumb is useluss to have at all..

 

Help much appreciated!

 

Thanks

 

Tim

Someone have fix this problem? 

 

Share this post


Link to post
Share on other sites

  • 1 year later...

I Got one solutions (Ps 1.7).

If you want to show all child category with product name. then you need to select radio button in last category. this will work fine. check screen shot.

Best Regards

Maneesh Tiwari

 

image.png.2213564e696b4db5f03f31fc487d8874.pngimage.thumb.png.f0c17e62933d42bdfb1718a7f461ee6d.png

Share this post


Link to post
Share on other sites

  • 2 months later...
On 3/3/2020 at 7:25 AM, Maneesh said:

I Got one solutions (Ps 1.7).

If you want to show all child category with product name. then you need to select radio button in last category. this will work fine. check screen shot.

Best Regards

Maneesh Tiwari

 

image.png.2213564e696b4db5f03f31fc487d8874.pngimage.thumb.png.f0c17e62933d42bdfb1718a7f461ee6d.png

Tried so but it doesn't show the categories, just Home > Product name.

I'm on Prestashop v1.7.6.4

Share this post


Link to post
Share on other sites

  • 5 months later...
  • 10 months later...

Hi all,

I have the same issue with my PrestaShop 1.7.6.3

By example, you can access to: https://www.racealtered-import.com/shop/fr/2131-turbo
In this category
"Impreza 2.0R/WRX/STI 2001-2007 (GDA/GGA/GDB/GGB)", you can select this product "Boost contrôleur électronique BLITZ SBC i-Color Type-R - 2.5bar".
This product is also in another category, and if you click on it, you will see his default category in breadcrumbs: "Impreza GT/STI 1993-2000 (GC8-GF8)"
The current category is not saving.

I found a post in this forum:

Somone spoke about issue regarding "HTTP referer" in php.ini
I checked it on my side, but the value is the one by default: session.referer_checkno value

If someone has found something...! 😄
Thank you!

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

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