Totti Posted June 5, 2013 Share Posted June 5, 2013 Good morning, I would like to put on the detail page of the product which contains a link to the category. How can I do ? Link to comment Share on other sites More sharing options...
NemoPS Posted June 5, 2013 Share Posted June 5, 2013 Hi, It depends on what you mean by category. If you mean the default one, the category id is already assigned to the product, and you should be able to acccess it with $product->id_category_default. Then you can simply call $link->getCategoryLink with the id to get that link Link to comment Share on other sites More sharing options...
vekia Posted June 5, 2013 Share Posted June 5, 2013 @Totti let us know what you exactly expect, if you want to use default category - follow nemo suggestion, it works. If you want to display ALL categories, there is necessary to add a piece of the code Link to comment Share on other sites More sharing options...
Totti Posted June 7, 2013 Author Share Posted June 7, 2013 I try to explain it in English. I want on the product page, there are links that lead back to the product categories. Link to comment Share on other sites More sharing options...
vekia Posted June 7, 2013 Share Posted June 7, 2013 I try to explain it in English. I want on the product page, there are links that lead back to the product categories. many thanks for explanation. Additional (and last) question: you want to display links only for default category, or for all categories to which the product belongs ? 1 Link to comment Share on other sites More sharing options...
Totti Posted June 7, 2013 Author Share Posted June 7, 2013 If possibile ALL category. Link to comment Share on other sites More sharing options...
NemoPS Posted June 7, 2013 Share Posted June 7, 2013 Well in that case I suggest you use a module Hook ite somewhere in the product page (perhaps product footer) then, inside the module grab the product id by using Tools::getValue('id_product') Now with that id, make a query and select all categories (ids, names, rewrites) from category product where id_product equals than one It should work Link to comment Share on other sites More sharing options...
vekia Posted June 7, 2013 Share Posted June 7, 2013 dear totti here is solution for you: all categories links on each product page {foreach from=Product::getProductCategoriesFull(Tools::getValue('id_product')) item=cat} <a href="{$link->getCategoryLink({$cat.id_category})}" title="{$cat.name}">{$cat.name}</a> {/foreach} use this in product.tpl file no core modification, no worries 2 Link to comment Share on other sites More sharing options...
NemoPS Posted June 7, 2013 Share Posted June 7, 2013 Good solution Vekia, but wouldn't it be better to keep php logic out of templates? It might slow down the tpls Link to comment Share on other sites More sharing options...
vekia Posted June 7, 2013 Share Posted June 7, 2013 Hmmm I think in this case it does not significantly affect the loading of the page. Product::getProductCategoriesFull uses only one simple query with 2 joins. I think that there is nothing to worry about. The difference in the charging page certainly is, but it's a matter of milliseconds 1 Link to comment Share on other sites More sharing options...
Totti Posted June 10, 2013 Author Share Posted June 10, 2013 With excellent solution and perfectly customizable. To change the location where it appears, I always have to change ".tpl" right ? Link to comment Share on other sites More sharing options...
vekia Posted June 10, 2013 Share Posted June 10, 2013 yes, .tpl file + sometimes css styles for object that you want to move to new position Link to comment Share on other sites More sharing options...
Totti Posted June 11, 2013 Author Share Posted June 11, 2013 If you want to put a separate css file where I have to declare ? Link to comment Share on other sites More sharing options...
NemoPS Posted June 11, 2013 Share Posted June 11, 2013 I'll never stress it hardly enough use a module, and the module api Link to comment Share on other sites More sharing options...
vekia Posted June 11, 2013 Share Posted June 11, 2013 I think that many css files is a bad idea. One small separated css file (no matter how big it is) = new apache connection to download it to browser while browsing website. In my opinion - you should put own css styles to the global.css file. Link to comment Share on other sites More sharing options...
NemoPS Posted June 11, 2013 Share Posted June 11, 2013 Yeah it's tru that many css files are bad, but if he needs to develop something reusable which is not a theme, the only way to go it a module. And in any case, compression would squeeze them into one file Link to comment Share on other sites More sharing options...
schibulski Posted January 30, 2016 Share Posted January 30, 2016 Hello! I still use this great function in prestashop 1.6.1.4 very well! But after adding a few more categories i figured out that the Categories are sorted by the category ID. Is it possible to change the code so that the list is sorted by category name? Link to comment Share on other sites More sharing options...
NemoPS Posted February 1, 2016 Share Posted February 1, 2016 What method are you using to retrieve them?If you use getCategories, you can add a 5th parameter as 'cl.name' public static function getCategories($id_lang = false, $active = true, $order = true, $sql_filter = '', $sql_sort = '', $sql_limit = '') Link to comment Share on other sites More sharing options...
schibulski Posted February 1, 2016 Share Posted February 1, 2016 Hello Nemo, thanks for your answer. I use the "getCategoriesFull" method as seen in the post from vekia above: public static function getProductCategoriesFull($id_product = '', $id_lang = null) { if (!$id_lang) { $id_lang = Context::getContext()->language->id; } $ret = array(); $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT cp.`id_category`, cl.`name`, cl.`link_rewrite` FROM `'._DB_PREFIX_.'category_product` cp LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.id_category = cp.id_category) LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cp.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').') '.Shop::addSqlAssociation('category', 'c').' WHERE cp.`id_product` = '.(int)$id_product.' AND cl.`id_lang` = '.(int)$id_lang ); foreach ($row as $val) { $ret[$val['id_category']] = $val; } return $ret; } I think i have to insert an "ORDER BY xxx ASC" somewhere. I tried everything, but i can´t figure out WHAT to use instead of "xxx" and WHERE to insert the ORDER BY snippet. I´m really a php an sql noob :-( Link to comment Share on other sites More sharing options...
NemoPS Posted February 3, 2016 Share Posted February 3, 2016 ORDER BY cl.name ASC Link to comment Share on other sites More sharing options...
Recommended Posts