Jump to content

[Solved] Editing Categories Block to show sub-cats of current cat!


adammontague

Recommended Posts

1. Go to your modules folder and make a copy/duplicate of your blockcategories folder and rename it with blockcategories2 for example.
2. Enter in the blockcategories2 folder and rename the blockcategories.tpl and blockcategories.php to blockcategories2.tpl and blockcategories2.php.
3. Open up your blockcategories2.php and replace all blockcategories words to blockcategories2.

4. in blockcategories2.php replace hookLeftColumn method with:

   public function hookLeftColumn($params)
   {
       global $smarty, $cookie;

       $id_customer = (int)($params['cookie']->id_customer);
       $id_group = $id_customer ? Customer::getDefaultGroupId($id_customer) : _PS_DEFAULT_CUSTOMER_GROUP_;
       $id_product = (int)(Tools::getValue('id_product', 0));
       $id_category = (int)(Tools::getValue('id_category', 0));
       $id_lang = (int)($params['cookie']->id_lang);
       $smartyCacheId = 'blockcategories2|'.$id_group.'_'.$id_lang.'_'.$id_product.'_'.$id_category;

 if($id_category)
   $category = new CategoryCore($id_category,$id_lang);
 else
   $category = Category::getRootCategory ($id_lang);
       $id_category = $category->id;

       Tools::enableCache();
       if (!$this->isCached('blockcategories2.tpl', $smartyCacheId))
       {
           $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');

           if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
               SELECT c.id_parent, c.id_category,c.nleft,c.nright, cl.name, cl.description, cl.link_rewrite
               FROM `'._DB_PREFIX_.'category` c
               LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.')
               LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`)
               WHERE (c.`active` = 1 OR c.`id_category` = 1)
               '.((int)($maxdepth) != 0 ? ' AND `level_depth` <= '.(int)($maxdepth) : '').'
               AND cg.`id_group` = '.$id_group.'
   AND c.nleft >= '.$category->nleft.'
   AND c.nright <= '.$category->nright.'
               ORDER BY `level_depth` ASC, c.`position` ASC')
           )
               return;
           $resultParents = array();
           $resultIds = array();

           foreach ($result as &$row)
           {
               $resultParents[$row['id_parent']][] = &$row;
               $resultIds[$row['id_category']] = &$row;
           }
           $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH'),$id_category);

           unset($resultParents);
           unset($resultIds);
           $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);

           if (Tools::isSubmit('id_category'))
           {
               $cookie->last_visited_category = $id_category;
               $smarty->assign('currentCategoryId', $cookie->last_visited_category);
           }
           if (Tools::isSubmit('id_product'))
           {
               if (!isset($cookie->last_visited_category) OR !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $cookie->last_visited_category))))
               {
                   $product = new Product($id_product);
                   if (isset($product) AND Validate::isLoadedObject($product))
                       $cookie->last_visited_category = (int)($product->id_category_default);
               }
               $smarty->assign('currentCategoryId', (int)($cookie->last_visited_category));
           }
           $smarty->assign('blockCategTree', $blockCategTree);

           if (file_exists(_PS_THEME_DIR_.'modules/blockcategories2/blockcategories2.tpl'))
               $smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories2/category-tree-branch.tpl');
           else
               $smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories2/category-tree-branch.tpl');
           $smarty->assign('isDhtml', $isDhtml);
       }
       $smarty->cache_lifetime = 31536000; // 1 Year
       $display = $this->display(__FILE__, 'blockcategories2.tpl', $smartyCacheId);
       Tools::restoreCacheSettings();
       return $display;
   }



Credits: steps 1, 2 and 3 come from Dharani's Blog

  • Like 2
Link to comment
Share on other sites

  • 3 months later...

Hi,

 

I would like to do exactly what Adammontague wanted to do. So I have followed all your explanation, but I do not know how to activate my second module "blockcategories2.tpl ".

I tried trough the Back Office, but I don't succeded to find how to install this second module : there is no "add new module" button...

I'm sure it's very simple...

 

Thanks in advance !!

Link to comment
Share on other sites

  • 5 weeks later...

Hi All,

 

I too am in need of the same solution i need to display only active or Current category and its subcategory in its respective category page not all the categories , please brief me the steps to do the changes in code in respective files more importantly i am using PRESTASHOP VERSION 1.4.1 please help me in this regard thanks in Advance.

 

Anil

Link to comment
Share on other sites

  • 3 months later...
  • 1 year later...

Cracker! Works in 1.5.3.1

I too had trouble setting up a "blockcategories2" module. I was going to research it but I need this done.

 

So I changed the code in my blockcategories.php (yeah I guess I probably shouldn't but it works) changing in the code any reference of "blockcategory2" to "blockcategory" in the code.

 

I also want to undisplay the block in the last category. Time for research.

 

Thanks heaps apug!

Link to comment
Share on other sites

I have tried this solution on 1.5.3.1 and it works, thanks a lot to apug. I'd like to have a slightly different behaviour, when i'm inside a subcategory (lvl2) if it doesn't have lower levels i don't see anything in the category block, and it's not useful to go back to parent level , Is it possible to change the code in a way that when in lvl2 subcategory it still shows also the lvl 1 parent category?

Thanks

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

I have tried this solution on 1.5.3.1 and it works, thanks a lot to apug. I'd like to have a slightly different behaviour, when i'm inside a subcategory (lvl2) if it doesn't have lower levels i don't see anything in the category block, and it's not useful to go back to parent level , Is it possible to change the code in a way that when in lvl2 subcategory it still shows also the lvl 1 parent category?

Thanks

 

Good Sugestion... Please Apug ... Help Us.

  • Like 1
Link to comment
Share on other sites

  • 5 weeks later...
  • 7 months 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...