Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display all categories on product page Prestashop

I need to get list of all categories and their ids on product page in Prestashop (I am using 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, this is not working.

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

It is my third shop and I am struggling with this problem for two days.

like image 464
David Jindra Avatar asked Oct 27 '14 06:10

David Jindra


People also ask

How to display subcategories images on category page in PrestaShop?

After upgrading PrestaShop 1.6, are you missing the absence of subcategories images on the category page in PrestaShop 1.7 classic theme or other themes? Let’s take them back in a few simple steps! 1. To display subcategories on category page To change category.tpl file, Which is responsible for category page.

How do I create a product category?

When creating a product category, you should focus on one thing: the products in this category must be comparable through their attributes (not their features). This is not only useful for your customers, but it is also a necessity for PrestaShop's product-comparison feature. Categories are managed in the "Categories" page of the "Catalog" menu.

How do I view sub-categories?

Categories are managed in the "Categories" page of the "Catalog" menu. This page displays a table with the currently existing categories, with the main information displayed. In order to display sub-categories, click on the parent category or select "View" in the action menu.

How do I edit the home category?

To edit the "Home" category (or any currently selected category), click on the "Edit" button in the button bar when the table displays the root categories. To create a new category (or a sub-category of an existing category), click on the "Add New" button from any level of categories. Translate your categories!


1 Answers

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

For your purpose I thing 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 could 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 would like to have only subcategories of Home category, you can use getHomeCategories($id_lang, $active = true, $id_shop = false):

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

Also handy one is static function getCategoryInformations($ids_category, $id_lang = null)
=> VERY useful when you have a list of some particular ids of categories you want to get - you just pass them as array - example of usage:

$myCustomCatIDs = array( 5 , 20 , 7);
$myCustomCats = Category::getCategoryInformations( $myCustomCatIDs );
like image 165
jave.web Avatar answered Sep 22 '22 17:09

jave.web