Post by dimko » Wed Dec 21, 2011 11:49 pm

As it is, if I open this product, for e.g.:

Code: Select all

http://demo.opencart.com/index.php?route=product/product&path=20_27&product_id=41
the left menu is active to the category/subcategory where the product belongs to, but when I click on the related product, I get this URL for that related product:

Code: Select all

http://demo.opencart.com/index.php?route=product/product&product_id=42
Is there any way I can get the Related Product's link to be as the parent product's link, so the left menu still shows the active category/subcategory?

Thanks in advance.

P.S. I'm not using products within multiple categories, and all related products belong to the same category/subcategory with the parent product.
Last edited by dimko on Thu Dec 22, 2011 4:53 am, edited 1 time in total.

Using OpenCart v1.5.1.3


Active Member

Posts

Joined
Sun Sep 25, 2011 2:10 am

Post by uksitebuilder » Thu Dec 22, 2011 4:15 am

You might get away with the following:

edit; catalog/controller/product/product.php

find

Code: Select all

'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
change to

Code: Select all

'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id'] . (isset($this->request->get['path']) ? '&path=' . $this->request->get['path'] : '')),
Note this will only work as long as all related products shown for the product are in the same category as the product being viewed.

I hope I understood you correctly.

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by dimko » Thu Dec 22, 2011 4:53 am

Yeap, you understood me very well, and this works like a charm :) Thanks a lot buddy ;)

Using OpenCart v1.5.1.3


Active Member

Posts

Joined
Sun Sep 25, 2011 2:10 am

Post by hex » Wed Mar 14, 2012 7:50 am

Note this will only work as long as all related products shown for the product are in the same category as the product being viewed.
Is there a way for this to work the way it probably should work, i.e. display full path including category and any sub-cats?

hex
Newbie

Posts

Joined
Fri Jun 03, 2011 4:44 pm

Post by uksitebuilder » Tue Mar 20, 2012 12:58 am

A method could be coded to do this, but would rely on the product only being added to one sub category or man category (not both and not multiple) Which is the reason I guess it works the way it does now.

I have such a method which is used in one of my extensions

edit: catalog/model/catalog/product.php

find

Code: Select all

}
?>
add before

Code: Select all

	public function getCategoryPath($product_id) {
		$category = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
		
		if($category->num_rows){
			$path = array();
			foreach($category->rows as $paths){
				$path[0] = $paths['category_id'];
			}
			
			$parent = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$path[0] . "'");

			$pid = $parent->row['parent_id'];
			
			$p = 1;
			if($pid>0){
				do{
					$path[$p] = $pid;
					
					$parent2 = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$pid . "'");
					$pid = $parent2->row['parent_id'];
					$p++;
				}while($pid>0);
			}
		
			$path = array_reverse($path);
			
			$fullpath = '';
			
			foreach($path as $val){
				$fullpath .= '_'.$val;
			}
		
			return ltrim($fullpath, '_');
		}else{
			return '0';
		}
	}			
Now, in whichever controller you wish that lists products (i.e. it already loads the model we just edited), then put the product link as follows

Code: Select all

$this->url->link('product/product', 'path=' . $this->model_catalog_product->getCategoryPath($product['product_id']) . '&product_id=' . $product['product_id'])
You may need to replace $product['product_id'] with whatever is used in the controller for the product_id

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom
Who is online

Users browsing this forum: No registered users and 13 guests