threeopus3 Posted May 11, 2013 Share Posted May 11, 2013 (edited) I believe that this is a new function in Prestashop 1.5, there is very little documentation on it, and I am bashing my way through Prestashop to get things to work. This code exits is the product class (product.php) /** * getProductCategories return an array of categories which this product belongs to * * @return array of categories */ public static function getProductCategories($id_product = '') { $ret = array(); $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT `id_category` FROM `'._DB_PREFIX_.'category_product` WHERE `id_product` = '.(int)$id_product );if ($row) foreach ($row as $val) $ret[] = $val['id_category']; return $ret; } How can I call this from my product.tpl template? I know that I'll end up with an array, and don't know how to call it or how to blow apart the array to get the values once I have called it. I think that from the product.tpl template I would use this: $product->getProductCategories($product->id_product); But that is as far as I can get. Thanks for any help. I know that I'm not the only person who will find any help with this really useful. Thanks. Edited May 11, 2013 by threeopus3 (see edit history) 1 Link to comment Share on other sites More sharing options...
PrestaMonster.com Posted May 11, 2013 Share Posted May 11, 2013 Prestashop is using MVC model so it's not good to call that function from template. You can assign smarty variable in ProductController.php such as $this->context->smarty->assign('product_categories', Product::getProductCategories($this->product->id)); Link to comment Share on other sites More sharing options...
threeopus3 Posted May 11, 2013 Author Share Posted May 11, 2013 (edited) Beauty. Thanks. I changed that code a bit to match the format that I see being used on the ProductController.php. I inserted this: 'categories' => $this->product->getProductCategories($this->product->id), On my product.tpl page, I have added this code: {foreach from=$categories item=category} <p>I am a category</p> {/foreach} I know it is rudimentary, but I can see that it is sort of working. I have a product that is associated with four categories, and I see that <p>I am a category</p> is echoed four times. This is good. Getting closer. But, I can't figure out how to access that array of categories. I've tried: {foreach from=$categories item=category} {if $category.name|escape:'htmlall':'UTF-8' == 'On Sale'} <p>ON SALE</p> {/if} {/foreach} This doesn't work, Though that syntax does work with the features array, so I thought it would. Do you have any idea how I can access the categories array information? Thanks again. Edited May 11, 2013 by threeopus3 (see edit history) Link to comment Share on other sites More sharing options...
yaniv14 Posted May 11, 2013 Share Posted May 11, 2013 I am pretty suck with Smarty, but maybe you can try: $category->name Link to comment Share on other sites More sharing options...
threeopus3 Posted May 11, 2013 Author Share Posted May 11, 2013 Thanks Yaniv. I did try that but it does not work. Link to comment Share on other sites More sharing options...
yaniv14 Posted May 11, 2013 Share Posted May 11, 2013 if you just printing $categoy.name, are you getting a list of all your categories? do you really have a category names 'On Sale'? Link to comment Share on other sites More sharing options...
PrestaMonster.com Posted May 11, 2013 Share Posted May 11, 2013 Hi, With static function getProductCategories(), you are only able to get id_category. If you want to get full information about these categories, you should use this function instead getProductCategoriesFull(). Link to comment Share on other sites More sharing options...
threeopus3 Posted May 11, 2013 Author Share Posted May 11, 2013 If I try printing $category.name, I get a list of single digit numbers (5,5,6,6,1). I don't know what they are. They are not category ids. The array in my controller file is called $my_categories. It seems like there is an array that already exists, called $categories. If I print it, I can get a list of the ids for every category in my store by calling "$category.id_category". But I can't call "$category.id_category" for my product-specific array. I think I need to sleep on this and try again tomorrow. Link to comment Share on other sites More sharing options...
threeopus3 Posted May 11, 2013 Author Share Posted May 11, 2013 Thanks PrestaMonster. I'll settle for the IDs. That would still be good, but I cannot get those. Argh. Link to comment Share on other sites More sharing options...
PrestaMonster.com Posted May 11, 2013 Share Posted May 11, 2013 I've checked and it works fine in this case. In ProductController.php, I add 'belonged_categories' => $this->product->getProductCategoriesFull($this->product->id) Then in product.tpl, I add {foreach from=$belonged_categories item=category} Category ID#: {$category.id_category}<br /> Name: {$category.name}<br /> Friendly URL: {$category.link_rewrite}<br /> {/foreach} You need to assign special variable name in ProductController.php, due to 'categories' is so common. 1 Link to comment Share on other sites More sharing options...
threeopus3 Posted May 11, 2013 Author Share Posted May 11, 2013 Prestamonster, thanks you so much!!! That works. It must have been, as you say, that I was using a variable that was too common. Thank again! So this is how to add category info to the product page. I will call this solved. Link to comment Share on other sites More sharing options...
PrestaMonster.com Posted May 11, 2013 Share Posted May 11, 2013 You are welcome. And can you mark this topic as [sOLVED]. Link to comment Share on other sites More sharing options...
dramdram Posted June 9, 2013 Share Posted June 9, 2013 Hello, But I try adding both codes but it instantly crashes ... Could you give me more detail on how exactly get all that working. wich files and where exactly ? thank you very much I've checked and it works fine in this case. In ProductController.php, I add 'belonged_categories' => $this->product->getProductCategoriesFull($this->product->id) Then in product.tpl, I add {foreach from=$belonged_categories item=category} Category ID#: {$category.id_category}<br /> Name: {$category.name}<br /> Friendly URL: {$category.link_rewrite}<br /> {/foreach} You need to assign special variable name in ProductController.php, due to 'categories' is so common. Link to comment Share on other sites More sharing options...
vekia Posted June 9, 2013 Share Posted June 9, 2013 Hello, But I try adding both codes but it instantly crashes ... Could you give me more detail on how exactly get all that working. wich files and where exactly ? thank you very much you want to display ALL product categories on product page? Am i right? 1 Link to comment Share on other sites More sharing options...
Recommended Posts