Show all categories on Prestashop product page

I need to get a list of all categories and their identifiers on the product page in Prestashop (I use v 1.6.0.9).

I tried to do something like this:

$other_categories = $something->getCategories($this->context->language->id, 1, 100); foreach($other_categories as something) { // now if the id of the category isnt "1", display name of category if($category->id != "1") { $category->name } } 

But that does not work.

$category->name gives me only the name of the current open category, not the name of each category in the list. I do not know what to put instead of something ? And it only works when I use $category->getProducts . Here you have my store (see "Related Products").

This is my third store, and I have been struggling with this problem for two days.

+5
source share
3 answers

In PS 1.6, there is a Category class, it contains some useful static methods used in your controller: getCategories(...) , getNestedCategories(...) , getSimpleCategories - all these are static (and public) that you call them Category::funcName(...)

For your purpose, I think the best option would be getNestedCategories() , which has this header:

 public static function getNestedCategories( $root_category = null, $id_lang = false, $active = true, $groups = null, $use_shop_restriction = true, $sql_filter = '', $sql_sort = '', $sql_limit = '' ) 

In your controller, you can do something like:

 $allCategories = Category::getNestedCategories(null, $this->context->language->id); $this->context->smarty->assign( 'allCategories' , $allCategories ); 

Then in your template file something like

 {foreach from=$allCategories item=mainCategory} <div class="categoryBox"> <h2>{$mainCategory.name}</h2> <p>{$mainCategory.description}</p> </div> {foreach from=$mainCategory.children item=subCategory} <div class="categoryBox"> <h3>{$subCategory.name}</h3> <p>{$subCategory.description}</p> </div> {/foreach} {/foreach} 

If you want to have only subcategories of the Home category, you can use getHomeCategories($id_lang, $active = true, $id_shop = false) :

 $allCategories = Category::getHomeCategories( $this->context->language->id ); 

Also getCategoryInformations($ids_category, $id_lang = null) is the static function getCategoryInformations($ids_category, $id_lang = null)
=> VERY useful when you have a list of specific category identifiers that you want to receive โ€” you simply pass them as an array โ€” use case:

 $myCustomCatIDs = array( 5 , 20 , 7); $myCustomCats = Category::getCategoryInformations( $myCustomCatIDs ); 
+6
source

Check out the home categories module. I tested this module with PS 1.6, it works. You can change the module binding to your needs. I made some custom modification to display subcategories. (Sorry for the bad English, not my native language)

Here is my custom PHP code for the module storing category and subcategory elements in smarty and associated with the tpl file.

 class Homecategories extends Module { private $_html = ''; private $_postErrors = array(); function __construct() { $this->name = 'homecategories'; $this->tab = 'front_office_features'; $this->version = 1.3; $this->author = 'John Stocks'; $this->need_instance = 0; parent::__construct(); // The parent construct is required for translations $this->page = basename(__FILE__, '.php'); $this->displayName = $this->l('Homepage Categories for v1.5'); $this->description = $this->l('Displays categories on your homepage'); } function install() { return (parent::install() AND $this->registerHook('home') AND $this->registerHook('header')); } public function hookHeader() { Tools::addCSS(($this->_path).'homecategories.css', 'all'); } function hookHome($params) { global $smarty, $cookie, $link; $id_customer = (int)$params['cookie']->id_customer; $id_group = $id_customer ? Customer::getDefaultGroupId($id_customer) : _PS_DEFAULT_CUSTOMER_GROUP_; $id_lang = (int)$params['cookie']->id_lang; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT c.*, cl.* FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.') LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`) WHERE level_depth > 1 And level_depth < 3 AND c.`active` = 1 AND cg.`id_group` = '.$id_group.' ORDER BY `level_depth` ASC, c.`position` ASC'); $category = new Category(1); $nb = intval(Configuration::get('HOME_categories_NBR')); global $link; $this->context->smarty->assign(array( 'categories' => $result, Category::getRootCategories(intval($params['cookie']->id_lang), true), 'link' => $link)); $this->context->smarty->assign(array( 'category' => $category, 'lang' => Language::getIsoById(intval($params['cookie']->id_lang)), )); $result2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT c.*, cl.* FROM ps_category c LEFT JOIN `ps_category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.') LEFT JOIN ps_category_group cg ON (cg.`id_category` = c.`id_category`) WHERE level_depth > 2 And level_depth < 4 AND cg.`id_group` = '.$id_group.' AND c.`active` = 1 ORDER BY `level_depth` ASC, c.`position` ASC '); global $link; $this->context->smarty->assign(array( 'subcategories' => $result2, Category::getRootCategories(intval($params['cookie']->id_lang), true), 'sublink' => $link)); $this->context->smarty->assign(array( 'category' => $subcategory, 'lang' => Language::getIsoById(intval($params['cookie']->id_lang)), )); return $this->display(__FILE__, 'homecategories.tpl'); } } 
0
source
 Try- Add in controller function- $categoryList = Category::getCategories(); and assign the variable in smarty. $this->smarty->assign(array( 'displayCategoryList' => $categoryList, )); Add in tpl file- {$displayCategoryList|@print_r} 
0
source

Source: https://habr.com/ru/post/1205579/


All Articles