Get Product Category Name In Module using Prestashop 1.6

Hanny picture Hanny · Dec 17, 2015 · Viewed 12.4k times · Source

I created my own module for prestashop (very basic currently).

I want to add some customization to products (similar to Attribute Wizard Pro ) Ultimate Goal: I would like for my module to display a little form on the product page depending on which category the product is in (slightly varied forms for each category) - and the results of that form would be saved when the product is purchased.

I want to put the form in the RightColumnProduct - I can do that by accessing it in this hook and calling the TPL I created.

public function hookDisplayRightColumnProduct()
{
    /* Place your code here. */
    /*Get the Current Category name to see which TPL to show*/


    return $this->display(__FILE__,'views/hooks/mytpl.tpl');
}

What I need to do is access the category name for the current product, but that is proving difficult to do.

I've tried various solutions with no success.

Answer

Krystian Podemski picture Krystian Podemski · Dec 17, 2015

This snippet will show you name of the default product category.

$product = $this->context->controller->getProduct(); $category = new Category((int)$product->id_category_default, (int)$this->context->language->id); echo $category->name;