dawb Posted November 21, 2011 Share Posted November 21, 2011 Hi, I am looking to display the categories that a product belongs to on the product page and to arrange them in groups by the parent of each category. For example: Top Category - Sub Category - Sub Category Top Category - Sub Category - Sub Category Has anyone seen any modules that do something similar to this? I have seen this on other sites but can not find anything similar already in the forums. Thanks for your help. Link to comment Share on other sites More sharing options...
dawb Posted November 23, 2011 Author Share Posted November 23, 2011 The following code placed in the ProductController.php file will acheive this. public function getCategories(){ $id_product = (int)Tools::getValue('id_product'); $categories = Db::getInstance()->ExecuteS('SELECT CatProd.`id_product` AS ProductID, CatProd.`id_category` AS CategoryID, CatLang.`name` AS CategoryName, CatLang.`link_rewrite` AS CategoryLink, Cat.`id_parent` AS ParentID, ParLang.`name` AS ParentName FROM `ps_category_product` AS CatProd LEFT JOIN `'._DB_PREFIX_.'category_lang` AS CatLang ON CatLang.`id_category` = CatProd.`id_category` LEFT JOIN `'._DB_PREFIX_.'category` AS Cat ON Cat.`id_category` = CatProd.`id_category` LEFT JOIN `'._DB_PREFIX_.'category_lang` AS ParLang ON Cat.`id_parent` = ParLang.`id_category` WHERE CatProd.`id_product` = ' . $id_product . ' AND Cat.`id_parent` != 1 GROUP BY CatLang.`id_category`'); if (sizeof($categories)){ $array = array(); foreach($categories AS $category){ if(!array_key_exists($category['ParentName'], $array)) $array[$category['ParentName']] = array(); $array[$category['ParentName']][] = $category; } $html = '<div id="productCats">'; foreach($array as $parent) { $html .= '<p>' . $parent[0]['ParentName'] . ':'; foreach($parent as $category) { $html .= " <a href='" . $category[CategoryLink] . "'>" . $category[CategoryName] . "</a>, "; } $html .= '</p>'; } return $html .= '</div>'; } } Link to comment Share on other sites More sharing options...
Carolina Custom Designs Posted November 23, 2011 Share Posted November 23, 2011 Do you have a working example of this? I would like to see what this accomplishes. Thanks! Marty Shue Link to comment Share on other sites More sharing options...
dawb Posted November 23, 2011 Author Share Posted November 23, 2011 Not currently sorry as am still working on the site. But it displays a menu/list of links in the following way: main category 1: sub category, sub category, sub category main category 2: sub category, sub category Link to comment Share on other sites More sharing options...
banense Posted February 14, 2012 Share Posted February 14, 2012 Hi i need make the same thing.. anybody can help us ?????.. Link to comment Share on other sites More sharing options...
PrestaMagic Posted February 15, 2012 Share Posted February 15, 2012 We did something similar. We wanted to add the parent category's image to the subcategory pages, so the visitor would have more context for those subcat pages. We ended up using the $category->id_parent to get the parent ID for each subcategory, then built an image using that ID. Each subcategory has a id_parent that you can access. Try adding {debug} to the top of category.tpl and you can see the available smarty variables. Link to comment Share on other sites More sharing options...
TiBaker Posted November 9, 2012 Share Posted November 9, 2012 The following code placed in the ProductController.php file will acheive this. public function getCategories(){ $id_product = (int)Tools::getValue('id_product'); $categories = Db::getInstance()->ExecuteS('SELECT CatProd.`id_product` AS ProductID, CatProd.`id_category` AS CategoryID, CatLang.`name` AS CategoryName, CatLang.`link_rewrite` AS CategoryLink, Cat.`id_parent` AS ParentID, ParLang.`name` AS ParentName FROM `ps_category_product` AS CatProd LEFT JOIN `'._DB_PREFIX_.'category_lang` AS CatLang ON CatLang.`id_category` = CatProd.`id_category` LEFT JOIN `'._DB_PREFIX_.'category` AS Cat ON Cat.`id_category` = CatProd.`id_category` LEFT JOIN `'._DB_PREFIX_.'category_lang` AS ParLang ON Cat.`id_parent` = ParLang.`id_category` WHERE CatProd.`id_product` = ' . $id_product . ' AND Cat.`id_parent` != 1 GROUP BY CatLang.`id_category`'); if (sizeof($categories)){ $array = array(); foreach($categories AS $category){ if(!array_key_exists($category['ParentName'], $array)) $array[$category['ParentName']] = array(); $array[$category['ParentName']][] = $category; } $html = ' '; foreach($array as $parent) { $html .= ' ' . $parent[0]['ParentName'] . ':'; foreach($parent as $category) { $html .= " [url=""%20.%20$category[CategoryLink]%20.%20"]" . $category[CategoryName] . "[/url], "; } $html .= ''; } return $html .= ' '; } } Hello, I would like to know how to test this function ... How do appear in a file .tpf 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