Jump to content

get current category id in module php file


nockbcn

Recommended Posts


Hi everyone, I'm trying to write a custom module in which I want to assign the current category, where it's being shown by the moment, to an array.

As I understand, context class won't be appropiate in this case, I've also tried Tools::getValue('id_category')  but nothing happends.

 

Could you bring some help? what I'm doing wrong?

Thanks in advance !

Link to comment
Share on other sites

Nothing wrong I can see.. if you're on a category, then Tools::getValue('id_category') should get you it

 

Is there any reason why the context class is innapropriate? Should also allow you to access it in theory, via either:

$this->context->category->id_category

Or if that's not there, you can cal it in via:

Context::getContext()->category->id_category

If you're still having no luck after all that, is it definitely on a category?.. or could the module be trying to run the code before or after some core code that needs to be ran? Can't think of much else that would stop you accessing it

Link to comment
Share on other sites

Hi FullCircles, thanks for replying :D
I've tried these codes like that

$cat = Tools::getValue('id_category');

$cat = $this->context->category->id_category;

$cat = Context::getContext()->category->id_category;

placing them above the hook DisplayRightcolumnproduct,

public function hookDisplayRightColumnProduct($params)
{
global $smarty;

then I assign the variable to smarty 

$this->context->smarty->assign('categoria', $cat);

could be the problem that I'm placing the module in a hook  that is inside  products? should I ask for product parent category id instead?

 

Thanks again!

Link to comment
Share on other sites

 

 

could be the problem that I'm placing the module in a hook  that is inside  products?

 

What exactly do you mean with this? On what page will the module be used exactly? Product detail page?

If indeed on product detail page, you can probably use {$category->name} or something , as {$category} should be available here already (defined in controllers/front/ProductController.php, before calling Product.php)

 

If that's not where you need it, please elaborate where exactly the module will be hooked on, which PrestaShop page.

 

pascal

Link to comment
Share on other sites

Hi PascalIVG the module is hooked in DisplayRightColumnProduct and is shown in product details as you say, but I want to save the id to $cat  in order to use it in sql querys in the php file.

like this

$sql = 'SELECT public_name FROM `'._DB_PREFIX_.'attribute_group_lang` WHERE id_lang = 1  AND relative = $cat';
$att_list = Db::getInstance()->executeS($sql);

 I've added a custom field to atribute_group_lang, named realtive, which I use to conect the categories with the attributes.

 My idea is to save to an array the attributes related to each category, an then in tpl create the selectors 

<tr><td>{$att_list[0].public_name}</td><td>{html_options name=selector options=$options}</td></tr>

thanks!!

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

You could try referencing id_category_default from the product class using the various ways provided, if your module has access to that.

I don't know if it will or not though.. frankly, the category references provided should be working, so it sounds like something odd is going on with it

 

Also not too sure how you're linking the attribute to a category, in theory that attribute could be used for many products and categories, it's not a 1 to 1 relationship.

Link to comment
Share on other sites

This is how you can get id category default or array id category list from existing product object

public function hookDisplayRightColumnProduct($params)
{
    // Define product object
    $product = $params['product'];
    
    // Get the default id category from defined product object
    // Depend to id shop
    $id_category_default = $product->getDefaultCategory();
    
    // Get array of id categories from the defined object product
    // NOT depend to id shop
    $categories = $product->getCategories();
    
    // Alternatively you can also Get array of id categories
    // from the defined object product using its id_product
    // NOT depend to id shop
    $categories_alt = Product::getProductCategories($product->id);
	
}
  • Like 1
Link to comment
Share on other sites

 

This is how you can get id category default or array id category list from existing product object

public function hookDisplayRightColumnProduct($params)
{
    // Define product object
    $product = $params['product'];
    
    // Get the default id category from defined product object
    // Depend to id shop
    $id_category_default = $product->getDefaultCategory();
    
    // Get array of id categories from the defined object product
    // NOT depend to id shop
    $categories = $product->getCategories();
    
    // Alternatively you can also Get array of id categories
    // from the defined object product using its id_product
    // NOT depend to id shop
    $categories_alt = Product::getProductCategories($product->id);
	
}

Thanks a lot! it's solved! all those codes works perfectly, 

now I can continue working, thanks again :D

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...