keweli Posted July 2, 2011 Share Posted July 2, 2011 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 More sharing options...
shokinro Posted July 2, 2011 Share Posted July 2, 2011 if you have category object loaded, you can just use $category->id_parent to get the parent category.at category page, it seems that $id_category_parent is also available to use in theme file, it is already to use. Link to comment Share on other sites More sharing options...
keweli Posted July 2, 2011 Author Share Posted July 2, 2011 Thanks, the ID can supply me the link. Is it possible to get the name of the parent? Link to comment Share on other sites More sharing options...
shokinro Posted July 2, 2011 Share Posted July 2, 2011 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 idthen at your theme file you just use {$parent->name} Link to comment Share on other sites More sharing options...
keweli Posted July 2, 2011 Author Share Posted July 2, 2011 Thanks for the reply.I put those 2 lines inside the process() function of CategoryController.phpBut 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 More sharing options...
shokinro Posted July 2, 2011 Share Posted July 2, 2011 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. 1 Link to comment Share on other sites More sharing options...
keweli Posted July 2, 2011 Author Share Posted July 2, 2011 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 More sharing options...
shokinro Posted July 2, 2011 Share Posted July 2, 2011 sorry, it was my mistake, we should use id_parent instead of id when load the parent category.so your code is correct. I have also corrected the code in previous posts. Link to comment Share on other sites More sharing options...
keweli Posted July 2, 2011 Author Share Posted July 2, 2011 Well problem solved. Doesn't bother me to use an array.Thanks again Link to comment Share on other sites More sharing options...
shokinro Posted July 2, 2011 Share Posted July 2, 2011 good to know your problem solved. thanks for feedback. Link to comment Share on other sites More sharing options...
idm1 Posted July 14, 2011 Share Posted July 14, 2011 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! 3 Link to comment Share on other sites More sharing options...
ariom Posted August 2, 2015 Share Posted August 2, 2015 (edited) 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 August 2, 2015 by ariom (see edit history) 1 Link to comment Share on other sites More sharing options...
Recommended Posts