Jump to content

How check if a product belongs to a specific category


FluffyCat

Recommended Posts

Hi

 

I want to make a simple check to see if a product id belongs to a special category from smarty.

 

I found this function: Product::idIsOnCategoryId

 

which seems to do exactly what I want but it always return false.

 

I am calling it like this:

 

{assign var=catarray value=[84]}
{if Product::idIsOnCategoryId(414, $catarray)}OK{else}NOT OK{/if}
 
If I print the categories for product 414 using {Product::getProductCategories(414)|@print_r}  then it correctly prints the array like this:
 ( [0] => 2 [1] => 38 [2] => 78 [3] => 79 [4] => 84 [5] => 152 [6] => 153 [7] => 160 )
 
  So product 424 is indeed in category 84 but the call to idIsOnCategoryId is not working as I expect. I think I am passing the array of categories the correct way but still no luck.
 
Can anyone advise if I am doing this correctly or if there is another better or more efficient way to make a quick check if a product belongs to a category.
 
Thanks in advance for any advice, this can not be that complex.
Link to comment
Share on other sites

Thanks for the reply. It is strange since I have managed to call other static functions from the tpl file without problems.

 

I would prefer not to alter any php code, I mean, it must be possible to do the simple check if a product belongs to a specific category frmo the tpl file. This can not be that dificult right?

 

Other solution proposals are welcome, it does not have to be using this function.

Link to comment
Share on other sites

Smarty templates are generally for data representation.

It can be rarely use for calling php functions from .tpl file.

 

Also one thing if you are checking/calling this function from product.tpl then it works successfully.

But if you are calling from category.tpl or product-list.tpl then it won't be possible.

 

All you need to do is to render Product object within .tpl file.

like in categoryController.php as 

public function initContent()
{
'category' => $this->category
}
Link to comment
Share on other sites

  • 3 weeks later...

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