Jump to content

[Solved] Show parent category image for subcategory


Recommended Posts

I'm using the basic code to display the category image as a div background in header.tpl:

{if $category->id_image}
<div style="background: url({$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category_default')|escape:'html'}) top center no-repeat;">
{/if}

This works well with the top categories, but I would like the top category image to be displayed also when viewing subcategories, since they don't have any images of their own. Is there any way to do this? Preferably a solution that doesn't involve manually adding the top category image to all subcategories.  ;)

 

Thanks in advance!

Edited by yay (see edit history)
Link to comment
Share on other sites

1st, you should assign a smarty for parent category object

 

Override CategoryController.php (don't forget to delete /cache/class_index.php)

class CategoryController extends CategoryControllerCore
{
    public function initContent()
    {
        parent::initContent();
        
        // Load parent category object
        $parent_category = new Category((int)$this->category->id_parent);
        if(Validate::isLoadedObject($parent_category))
            $this->context->smarty->assign('parent_category' , $parent_category);
    }
}

And then you can use the necessary variable of parent category object to get the image Link

In your theme file (category.tpl)

<img src="{$link->getCatImageLink($parent_category->link_rewrite, $parent_category->id_image, 'category_default')|escape:'html'}" alt="{$parent_category->name|escape:'htmlall':'UTF-8'}" title="{$parent_category->name|escape:'htmlall':'UTF-8'}" id="categoryImage" width="{$categorySize.width}" height="{$categorySize.height}" />
  • Like 1
Link to comment
Share on other sites

Thanks for the reply!
 

I got the parent category image to show in the subcategory, however the image disappeared in the parent category. Found a solution to that, but since I'm totally new to Prestashop it's probably anything but correct.  :)

		{if $category->level_depth == 2}
		<!-- Ordinary category image for parent categories -->
		<img src="{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category_default')|escape:'html'}" />
		{else}
		<!-- Use CategoryController to fetch parent category image for subcategory -->
		<img src="{$link->getCatImageLink($parent_category->link_rewrite, $parent_category->id_image, 'category_default')|escape:'html'}" />
		{/if}
Edited by yay (see edit history)
Link to comment
Share on other sites

I'm not sure if i'm understand, you've said "...however the image disappeared in the parent category..." what is mean ?
But I'm glad you can solve the problem :rolleyes:

 

You can use id_image variable to check the availability of catagory image, or better use php function file_exists(PATH_TO_FILE) to check the image file existence

Link to comment
Share on other sites

  • 3 years later...
  • 3 years later...

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