Post by SupraTwinTurbo » Fri Jul 29, 2011 11:04 am

Hi, I would like to only show a module (Category or Manufacturer) if the user is browsing that main Category or Manufacturer. My top navigation bar will have all the Main Categories. As a example, if I have the following Category structure:

Category01
-SubCategory01a
-SubCategory01b
-SubCategory01c

Category02
-SubCategory02a
-SubCategory02b
-SubCategory02c

and the user clicks on the "Category01" link in the top navigation, only this will be shown in the sidebar:

Category01
-SubCategory01a
-SubCategory01b
-SubCategory01c

Is there a module or php logical code to do this?

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by fido-x » Fri Jul 29, 2011 11:22 am

In "catalog/controller/module/category.php", change the following (at line 31) from:

Code: Select all

$categories = $this->model_catalog_category->getCategories(0); 
to:

Code: Select all

$categories = $this->model_catalog_category->getCategories($parts[0]); 

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by SupraTwinTurbo » Fri Jul 29, 2011 11:37 am

Hi Fido-X,

Thanks for the code, but I do not see that entry. Forgot to mention I am using OC 1.4.9.4.

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by fido-x » Fri Jul 29, 2011 11:46 am

SupraTwinTurbo wrote:... Forgot to mention I am using OC 1.4.9.4.
You should have said that in the first place!

In "catalog/controller/module/category.php", find the following (around line 20):

Code: Select all

$this->data['category'] = $this->getCategories(0); 
and replace with:

Code: Select all

if (isset($this->request->get['path'])) {
    $this->data['category'] = $this->getCategories($this->path[0]);
} else {
    $this->data['category'] = $this->getCategories(0);
} 

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by SupraTwinTurbo » Fri Jul 29, 2011 12:58 pm

Awesome that worked!

Do you know if it is possible to change the title of the module depending on which main category is showing? So if someone clicked on Category01, then the title would be "Shop By Category01"?

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by fido-x » Fri Jul 29, 2011 1:08 pm

That's a bit more complex, but not impossible.

In "catalog/controller/module/category.php", comment or delete line 9:

Code: Select all

$this->data['heading_title'] = $this->language->get('heading_title'); 
Then, replace the block of code provided above with:

Code: Select all

if (isset($this->request->get['path'])) {
    $category_info = $this->model_catalog_category->getCategory($this->path[0]);
    $this->data['heading_title'] = $category_info['name'];
    $this->data['category'] = $this->getCategories($this->path[0]);
} else {
    $this->data['heading_title'] = $this->language->get('heading_title');
    $this->data['category'] = $this->getCategories(0);
} 

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by SupraTwinTurbo » Fri Jul 29, 2011 1:52 pm

Just tried it, but getting this error now when I visit the site:

Code: Select all

Fatal error: Call to a member function getCategories() on a non-object in /home/mysite/public_html/catalog/controller/module/category.php on line 49

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by fido-x » Fri Jul 29, 2011 2:06 pm

Looks like you put the block of code provided in the wrong place. It needs to be AFTER the category model is loaded.

This is what your "catalog/controller/module/category.php" should look like with the code changes I've suggested:

Code: Select all

<?php  
class ControllerModuleCategory extends Controller {
    protected $category_id = 0;
    protected $path = array();
    
    protected function index() {
        $this->language->load('module/category');

        //$this->data['heading_title'] = $this->language->get('heading_title');
        
        $this->load->model('catalog/category');
        $this->load->model('tool/seo_url');
        
        if (isset($this->request->get['path'])) {
            $this->path = explode('_', $this->request->get['path']);
            
            $this->category_id = end($this->path);
        }
        
        if (isset($this->request->get['path'])) {
            $category_info = $this->model_catalog_category->getCategory($this->path[0]);
            $this->data['heading_title'] = $category_info['name'];
            $this->data['category'] = $this->getCategories($this->path[0]);
        } else {
            $this->data['heading_title'] = $this->language->get('heading_title');
            $this->data['category'] = $this->getCategories(0);
        }
                                                
        $this->id = 'category';

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/category.tpl';
        } else {
            $this->template = 'default/template/module/category.tpl';
        }
        
        $this->render();
      }
    
    protected function getCategories($parent_id, $current_path = '') {
        $category_id = array_shift($this->path);
        
        $output = '';
        
        $results = $this->model_catalog_category->getCategories($parent_id);
        
        if ($results) { 
            $output .= '<ul>';
        }
        
        foreach ($results as $result) {    
            if (!$current_path) {
                $new_path = $result['category_id'];
            } else {
                $new_path = $current_path . '_' . $result['category_id'];
            }
            
            $output .= '<li>';
            
            $children = '';
            
            if ($category_id == $result['category_id']) {
                $children = $this->getCategories($result['category_id'], $new_path);
            }
            
            if ($this->category_id == $result['category_id']) {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '"><b>' . $result['name'] . '</b></a>';
            } else {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '">' . $result['name'] . '</a>';
            }
            
            $output .= $children;
        
            $output .= '</li>'; 
        }
 
        if ($results) {
            $output .= '</ul>';
        }
        
        return $output;
    }        
}
?>

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by SupraTwinTurbo » Fri Jul 29, 2011 2:28 pm

Thanks that worked!

I just have one more issue with this modification. Is it possible that if a subcategory is clicked, the sidebar still shows the main category? For example, if the user clicks on Subcategory01a, the sidebar will still show:

Category01
-SubCategory01a
-SubCategory01b
-SubCategory01c

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by fido-x » Fri Jul 29, 2011 3:06 pm

And what do you want the heading to display if a sub-sub-category (or a sub-sub-sub-category or sub-sub-sub, etc.) is selected?

What you're asking for is some kind of function/query that will get the parent of a parent of a parent, etc. I'm not saying it can't be done, but it's not an easy task.

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by SupraTwinTurbo » Fri Jul 29, 2011 4:59 pm

Hi Fido-X
fido-x wrote:And what do you want the heading to display if a sub-sub-category (or a sub-sub-sub-category or sub-sub-sub, etc.) is selected?

What you're asking for is some kind of function/query that will get the parent of a parent of a parent, etc. I'm not saying it can't be done, but it's not an easy task.
Just showing the main (top-level) category would be enough. So if the user clicked on a sub-sub-...category, "Category01" will show.

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by fido-x » Fri Jul 29, 2011 5:22 pm

SupraTwinTurbo wrote:Just showing the main (top-level) category would be enough. So if the user clicked on a sub-sub-...category, "Category01" will show.
Like I said:
fido-x wrote:What you're asking for is some kind of function/query that will get the parent of a parent of a parent, etc. I'm not saying it can't be done, but it's not an easy task.

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by tresj » Fri Jan 25, 2013 8:53 am

Hi,

Its a long time ago but I'm curious if you found the solution?
Because the code posted worked fine.
Hope to hear something.

Greetings
Angelique

Newbie

Posts

Joined
Fri Jan 25, 2013 8:49 am
Who is online

Users browsing this forum: No registered users and 14 guests