Prestashop 1.6: Breadcrumb to display the category that product is currently in (not its default category)

Update 20190608: Due to SEO concern, the friendly url of product is configured to not show subcategories, i.e. domain.com/product-name-1234.html so the following solution does not work as it uses the subcategories in the friendly url as reference

Source: https://www.prestashop.com/forums/topic/167513-how-to-show-the-full-breadcrumb-to-the-default-category-on-every-product-page/?do=findComment&comment=2684341

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))));
}

This entry was posted in ecommerce and tagged , . Bookmark the permalink.