FluffyCat Posted January 19, 2015 Share Posted January 19, 2015 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 More sharing options...
sandipchandela Posted January 20, 2015 Share Posted January 20, 2015 It seems your method is incorrect. You are calling php method/function from smarty template using PHP syntax that resulted in error situation. Better to call same method in Controller file and then renders data into .tpl file and then display result what you want in tpl file. Link to comment Share on other sites More sharing options...
FluffyCat Posted January 22, 2015 Author Share Posted January 22, 2015 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 More sharing options...
sandipchandela Posted January 23, 2015 Share Posted January 23, 2015 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 More sharing options...
FluffyCat Posted February 9, 2015 Author Share Posted February 9, 2015 Ok, but how do I create this object then in the .tpl file :-) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now