Post by billynoah » Fri Jun 07, 2013 2:08 pm

Greetings! I am new to Opencart and this is my first contribution post. I am using OpenCart Version 1.5.5.1.

I wanted the dropdown menu under each top level category to display all of it's products instead of it's subcategories. Accomplished by changing the file: catalog/controller/common/header.php as follows (lines 102-119 are commented out and replaced):

Code: Select all

<?php   
class ControllerCommonHeader extends Controller {
	protected function index() {
		$this->data['title'] = $this->document->getTitle();
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$server = $this->config->get('config_ssl');
		} else {
			$server = $this->config->get('config_url');
		}

		$this->data['base'] = $server;
		$this->data['description'] = $this->document->getDescription();
		$this->data['keywords'] = $this->document->getKeywords();
		$this->data['links'] = $this->document->getLinks();	 
		$this->data['styles'] = $this->document->getStyles();
		$this->data['scripts'] = $this->document->getScripts();
		$this->data['lang'] = $this->language->get('code');
		$this->data['direction'] = $this->language->get('direction');
		$this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
		$this->data['name'] = $this->config->get('config_name');
		
		if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) {
			$this->data['icon'] = $server . 'image/' . $this->config->get('config_icon');
		} else {
			$this->data['icon'] = '';
		}
		
		if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
			$this->data['logo'] = $server . 'image/' . $this->config->get('config_logo');
		} else {
			$this->data['logo'] = '';
		}		
		
		$this->language->load('common/header');
		
		$this->data['text_home'] = $this->language->get('text_home');
		$this->data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
		$this->data['text_shopping_cart'] = $this->language->get('text_shopping_cart');
    	$this->data['text_search'] = $this->language->get('text_search');
		$this->data['text_welcome'] = sprintf($this->language->get('text_welcome'), $this->url->link('account/login', '', 'SSL'), $this->url->link('account/register', '', 'SSL'));
		$this->data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL'));
		$this->data['text_account'] = $this->language->get('text_account');
    	$this->data['text_checkout'] = $this->language->get('text_checkout');
				
		$this->data['home'] = $this->url->link('common/home');
		$this->data['wishlist'] = $this->url->link('account/wishlist', '', 'SSL');
		$this->data['logged'] = $this->customer->isLogged();
		$this->data['account'] = $this->url->link('account/account', '', 'SSL');
		$this->data['shopping_cart'] = $this->url->link('checkout/cart');
		$this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
		
		// Daniel's robot detector
		$status = true;
		
		if (isset($this->request->server['HTTP_USER_AGENT'])) {
			$robots = explode("\n", trim($this->config->get('config_robots')));

			foreach ($robots as $robot) {
				if ($robot && strpos($this->request->server['HTTP_USER_AGENT'], trim($robot)) !== false) {
					$status = false;

					break;
				}
			}
		}
		
		// A dirty hack to try to set a cookie for the multi-store feature
		$this->load->model('setting/store');
		
		$this->data['stores'] = array();
		
		if ($this->config->get('config_shared') && $status) {
			$this->data['stores'][] = $server . 'catalog/view/javascript/crossdomain.php?session_id=' . $this->session->getId();
			
			$stores = $this->model_setting_store->getStores();
					
			foreach ($stores as $store) {
				$this->data['stores'][] = $store['url'] . 'catalog/view/javascript/crossdomain.php?session_id=' . $this->session->getId();
			}
		}
				
		// Search		
		if (isset($this->request->get['search'])) {
			$this->data['search'] = $this->request->get['search'];
		} else {
			$this->data['search'] = '';
		}
		
		// Menu
		$this->load->model('catalog/category');
		
		$this->load->model('catalog/product');
		
		$this->data['categories'] = array();
					
		$categories = $this->model_catalog_category->getCategories(0);
		
		foreach ($categories as $category) {
			if ($category['top']) {
				// Level 2
		    /*  $children_data = array();
				
				$children = $this->model_catalog_category->getCategories($category['category_id']);
				
				foreach ($children as $child) {
					$data = array(
						'filter_category_id'  => $child['category_id'],
						'filter_sub_category' => true
					);
					
					$product_total = $this->model_catalog_product->getTotalProducts($data);
									
					$children_data[] = array(
						'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
						'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                
                    );						
                 */
                $dataProduct = array(
						'filter_category_id'  => $category['category_id'],
						'filter_sub_category' => true
					);
	            $children = $this->model_catalog_product->getProducts($dataProduct);
                $children_data = array();
				foreach ($children as $child ) {
					$children_data [] = array(
							'name'  =>  $child['name'],
							'href'  => $this->url->link('product/product', 'product_id=' . $child['product_id']),
						);

                }
				
				// Level 1
				$this->data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
				);
			}
		}
		
		$this->children = array(
			'module/language',
			'module/currency',
			'module/cart'
		);
				
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/header.tpl';
		} else {
			$this->template = 'default/template/common/header.tpl';
		}
		
    	$this->render();
	} 	
}
?>

hope this helps somebody... i was sure there had to be some free mods out there that already do this but i couldn't find one.

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm

Post by robocop1953 » Wed Jul 31, 2013 10:00 pm

Hey great! Thanks for that, was looking to improve the drop down myself to enable customers to view products directly without having to hunt and peck through various sub-categories! This should do it, thanks again. ;D

Active Member

Posts

Joined
Wed Jul 17, 2013 2:18 am

Post by billynoah » Tue Aug 27, 2013 2:01 am

you're welcome, this is especially useful for stores with either no sub categories, or very few products.

i've rewritten this several times at this point in order to achieve the final goal of displaying 3 & 4 level menus, with products as the final tier. the code i have is tailored to integrate several other 3rd party vqmods so it may not be that universal, but you can pm me if this interests you.

billy

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm

Post by alan_82 » Sun Nov 10, 2013 7:54 am

This is just what iv'e been looking for! Thanks

Do you know if there is a way to include the category name in the product link url, so dropwon product links are like:

Code: Select all

site.com/category/product
instead of

Code: Select all

site.com/product
Thanks

Newbie

Posts

Joined
Mon Jul 30, 2012 1:35 am

Post by billynoah » Mon Nov 11, 2013 4:56 am

you can definitely do this, but a problem arises if a product belongs to more than one category, or is within subcategories. ultimately, how you structure your url is up to you.

you can query to product-to-category table using the model/catalog/product -> getCategories(product_id) function which will return an array of category ids for your product. then just code your url accordingly.

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm

Post by bevang » Tue Jan 07, 2014 11:59 am

Nevermind, just realised I had to replace the whole code ::) :clown:

Thank you so much for this.

I can't speak for 1.5.5, but for 1.5.6 it does throw a small error stating that "error" variable on line 119 in the header.tpl in not recognised. To make this work for 1.5.6 without the error, add the following code in line 11 of your new catalog/controller/common/header.php file

Code: Select all

        if (isset($this->session->data['error']) && !empty($this->session->data['error'])) {
            $this->data['error'] = $this->session->data['error'];
            
            unset($this->session->data['error']);
        } else {
            $this->data['error'] = '';
        }

Newbie

Posts

Joined
Sun Nov 17, 2013 7:46 pm

Post by bevang » Tue Jan 07, 2014 12:00 pm

Can this be used in conjunction with your other piece of code to detect products or subcategories? No big deal if it can't.

I would also like to do what alan_82 said with the category friendly SEOs. Do you have a piece of code for that? Won't be a problem with multiple categories so I'll try and figure it out if you don't.

Thanks again!

Newbie

Posts

Joined
Sun Nov 17, 2013 7:46 pm

Post by billynoah » Wed Jan 08, 2014 12:30 am

bevang wrote:Nevermind, just realised I had to replace the whole code ::) :clown:
No, you misunderstood. You're not meant to replace the entire controller. This is the reason you are getting the missing variable error as well. You should only replace the lines which are commented out with lines 120 - 130 of the code I posted. In 1.5.6 you would comment out lines 110 - 125 and before the closing brace add:

Code: Select all

           $dataProduct = array(
                      'filter_category_id'  => $category['category_id'],
                      'filter_sub_category' => true
                   );
                   $children = $this->model_catalog_product->getProducts($dataProduct);
                    $children_data = array();
                foreach ($children as $child ) {
                   $children_data [] = array(
                         'name'  =>  $child['name'],
                         'href'  => $this->url->link('product/product', 'product_id=' . $child['product_id']),
                      );
bevang wrote:Can this be used in conjunction with your other piece of code to detect products or subcategories? No big deal if it can't.
Which code are you referring to? Link?
bevang wrote:I would also like to do what alan_82 said with the category friendly SEOs. Do you have a piece of code for that? Won't be a problem with multiple categories so I'll try and figure it out if you don't.
Yes I wrote some code for this a while back.

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm

Post by masoodw1 » Wed Jan 08, 2014 12:49 am

I registered in this forums just to thank you :ok: ;D

Newbie

Posts

Joined
Wed Jan 08, 2014 12:46 am

Post by billynoah » Thu Jan 09, 2014 1:06 pm

you're welcome :-)

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm
Who is online

Users browsing this forum: No registered users and 48 guests