'Prestashop 1.7 - Get breadcrumb categories in custom module

First post on StackOverflow :)

So my issue is that I want to get the current product page category in order to use it in a custom module.

I know that I can get the product categories from my DataBase but it's not what I want.

I thought that I can get the category in the breadcrumb but I try with no success.

Can someone help me please ?

Edit 1: I don't want my product category, I already have it. The problem is that my product have more than one category with the same level depth so I need the category that is display in the breadcrumb.

Edit 2: I discovered that in the .tpl file of my breadcrumb, I've an array with the categories displayed in the bredcrumb. Did you know how I can get them from my custom module .php file ?

SOLUTION : So it's not exactly what I wanted but it gonna make the job. I access to the breadcrumb categories array in my custom module with this line :

$this->context->smarty->GetTemplateVars('breadcrumb');

Source: https://stackoverflow.com/a/40260317/18297483



Solution 1:[1]

I can't fully understand what is your problem but i think if you are in php file /module file and you have the id_product that you are working with, then you just

$p = new Product($id_product); and then you can get the category id

(the direct category parent)

with

$id_category = $p->id_category_default;

another solution if you want the full category object might be like this .

$category = new Category($p->id_category_default,$this->context->language->id);

Solution 2:[2]

I think you can use the getCategories() function in the product class, like this:

$p = new Product($id_product);
$categories = $p->getCategories();

Then you can loop on each category one-by-one to get any information example 'name'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zied Dams
Solution 2 Jeremy Caney