Jump to content

Parent categories on product page


dawb

Recommended Posts

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

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

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

  • 2 months later...

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

  • 8 months later...
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

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