Prestashop - Main Categories on Every Template

1.7k views Asked by At

I'm using Prestashop for an eCommerce site. In the 'product.tpl' I put the following code, which I (eventually) found on a website, and it worked perfectly for my needs.

{assign var='category' value=Product::getProductCategoriesFull($product->id, $cookie->id_lang)}

{foreach from=$categories key='categoryId' item='category' name='category'}
    <a href="#">{$category['name']}</a>
 {/foreach}

This puts the eleven main categories on the product page for me. Works great. Now I need to put those same categories on the page that actually lists categories and subcategories, which I'm using 'category.tpl' for. That code doesn't work for that template, just no content is loaded at all.

The main categories part is actually supposed to be seen from every page on the website, as part of the header really. But I'm finding Prestashop to be problematic with includes, which would be the norm with PHP.

I'm trying to not use all the 'overrides' that I keep finding. It's far too much code that I don't understand. Can the code I have be slightly modified to work in the category template file (and any other template file on the website)?

1

There are 1 answers

3
gskema On

You code snippet is either wrong, or you typed it incorrectly:

{assign var='category'

Creates a variable $category. However, it is immediatelly overwritten by from=$categories item='category' -> looks creates it's own $category.

I think you meant {assign var='categories'. Anyway, the code may still be working because $categories variables is already avaiable in product page (every product has some categories).

Also, you first statement grabs categories for the specified product $product->id. Again, $product is avaiable in the product page only (logical).

If you want to grab all categories independently of the page, try using:

{assign var='all_categories' value=Category::getCategories(Context::getContext()->language->id)}
<pre>{$all_categories|@print_r}</pre>