development,

PrestaShop 1.7 Development

Pickles Lane Pickles Lane Follow Mar 16, 2021 · 2 mins read
Share this

Adding subcategories to the category page in PrestaShop 1.7

Have you been troubled by the absence of subcategories, in PrestaShop 1.7's category pages? Let's take them back in a few, simple steps!

Long time PrestaShop users have always been able to display subcategories in category pages, whatever theme they used. However, for some reason PrestaShop 1.7 doesn't seem to have any subcategory showing on page with the classic (standard) theme, making it harder for users to navigate through the catalog, if the merchant chose not to use the side category block or layered navigation. Taking them back is fortunately easy enough, and can be done by almost just cloning the code from the 1.6 default-bootstrap template. The files we need to edit are category.tpl and theme.css , so let's get to it!

Adding the subcategories loop to PrestaShop 1.7's category template

In the classic theme, let's open up category.tpl , located in themes\classic\templates\catalog\listing, it should look like the following:

What we want to to is append the subcategory loop inside the block, just before it closes, so after the div containing the category title. The $subgcategories variable is already assigned to the template, much like in PrestaShop 1.6, and it's ready to be used:

The code we just added is pretty much useless at this stage, as it just prints out each subcategory's name, but let's first see if it works. Save and refresh any category having children, and subcategories should pop up like this:

Nice! Let's not make our effort useful by linking them to the proper pages

Explanation: I copied and pasted the code from 1.6 for the category title, simply wrapping the name in an h5 tag, then an anchor pointing to the proper page. To get the url, we can still use the $link class, and its getCategoryLink method. I also left in the truncate modifier for the name, which cuts it to the length of 25 characters, and lastly escaped it.

At this point they are at least useful! But why don't we go ahead and add the image as well? Let's do it right before the h5:

Explanation: once more, we are using the link class's method to get the correct url pointing to the subcategory; then, we simply add an img tag, pointing directly to the category images folder, as each of them has an image named with the corresponding ID. Careful though! We have no way to check if the category has an image or not, and since the default method this theme uses is broken (heh.), you might as well get away without images. At this point you should have something like this, depending on your images' size:

It needs some styling, so let's see what we can do with css!

Styling subcategories in PrestaShop 1.7

Open up theme.css , located in themes\classic\assets\css. At the very end, let's append our css code:

I preferred staying on a simple styling here, and of course you will need to adapt the width depending on your category image size. Also, you might want to add media queries to make sure it displays well at smaller resolutions. Save and refresh, we are done!

For more about PrestaShop development visit dh42

Pickles Lane
Written by Pickles Lane Follow
Blogger, fashionista, love to explore new ideas and write on my morning coffee!