Jump to content

[Solved] Display Parent Category Inside a Category


Recommended Posts

Hi, inside a category (which lists all the products), I want to set a link for the parent category.

I know to get the child categories, I use:

{foreach from=$subcategories item=subcategory}





thanks!

Link to comment
Share on other sites

you have to make changes in the category page PHP file (/category.php for 1.3x or /contollers/CategoryController.php) to load the parent category object and assign to Smarty so that you can use the theme file.

here is example for for 1.4x


   $parent = new Category($this->category->id_parent, intval($cookie->id_lang));
       self::$smarty->assign('parent',$parent);



Edit: corrected to use id_parent instead of id

then at your theme file you just use

{$parent->name}

Link to comment
Share on other sites

Thanks for the reply.

I put those 2 lines inside the process() function of CategoryController.php

But when I use {$parent->name} it just returns the string "Array".

Does the parent object already have name assigned to it?

Link to comment
Share on other sites

Are you sure you are using the exactly the same code below?
it seems that you didn't pass in the language ID so the category loaded all names of of the parent category.

$parent = new Category($this->category->id_parent, intval($cookie->id_lang));



correction made: change id to id_parent.

  • Like 1
Link to comment
Share on other sites

Yep, I used that exactly.

I just tried looping through the array and it returned the current category itself. So tried changing to category->id_parent

$parent = new Category($this->category->id_parent, intval($cookie->id_lang));



And it gave me the parent category. Not sure why it is an array though.

Thanks :)

Link to comment
Share on other sites

  • 2 weeks later...

Hi guys,

I'm hoping you can help me too as I am trying to achieve something similar...

I have 4 main categories, each of those have subcategories.

When I click on a category, the subcategories are shown - when I click on one of these subcategories - I still want those subcategories to stay. (I know the code is looking for further subcategories for the subcategory I just clicked on)

I want the subcategories to always remain on every single page for the category I click on... (except for on the page where you view the product)

Hope this makes sense...
any ideas how I can achieve this? hope you can help me!

  • Like 3
Link to comment
Share on other sites

  • 4 years later...

I know it is an old thread, but i wanted to achieve the same as Keweli in the first post, but using PS v1.6.0.14.

 

I share my solution based on Shokinro code if someone else need it:

 

Best practice is to create a file called categoryController.php and save it in ../override/controllers/front (and delete file class_index.php in the cache root folder )

I've overriden the full function as follow:

<?php
class CategoryController extends CategoryControllerCore
{
    public function init()
    {
        // Get category ID
        $id_category = (int)Tools::getValue('id_category');
        if (!$id_category || !Validate::isUnsignedId($id_category))
            $this->errors[] = Tools::displayError('Missing category ID');

        // Instantiate category
        $this->category = new Category($id_category, $this->context->language->id);
        
        // Instantiate parent category
        $parent = new Category($this->category->id_parent, $this->context->language->id);
        $this->context->smarty->assign('parent',$parent);
        
        parent::init();
        //check if the category is active and return 404 error if is disable.
        if (!$this->category->active)
        {
            header('HTTP/1.1 404 Not Found');
            header('Status: 404 Not Found');
        }
        //check if category can be accessible by current customer and return 403 if not
        if (!$this->category->checkAccess($this->context->customer->id))
        {
            header('HTTP/1.1 403 Forbidden');
            header('Status: 403 Forbidden');
            $this->errors[] = Tools::displayError('You do not have access to this category.');
            $this->customer_access = false;
        }
    }
}

then for category.tpl is quite the same as Shokinro code,

i used:

{$parent->name|escape:'html':'UTF-8'}

but i'm not a coder...so maybe there is also a better way to do it...

 

I think this is also very good for my SEO  having subcategory and related category as <h1>.... but shurely it is more readable for my costumer cause my subcategories have all the same name but different product depending on the category they're related.

 

...Tks to Shokinro

Edited by ariom (see edit history)
  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...