Post by nuez » Sat Jul 17, 2010 11:11 pm

Until now there are no 'PREVIOUS and NEXT' buttons for the CATALOG>PRODUCT page of OPENCART. The next and previous buttons make it easier to leaf through the products of the same category without having to go back to the 'category' page.

Here is my solution: ( example on: http://shop.ramonespantaleon.com) . comments or suggestions are welcome.

1.in model\catalog\product.php

OPENCART generates a table which connects products to categories. We use this table to get our product IDs corresponding to the category ID. Therefore I added this function to the ModelCatalogProduct class:

Code: Select all

	public function getProductsIDbyCategoryID($category_id) {
		$query = $this->db->query("SELECT `product_id` FROM  `product_to_category` WHERE  `category_id` =  '".$category_id."'");
								  
		return $query->rows;
	}  

But one more query is needed: The problem with the first function is that it includes both activated and deactivated products. We use this function to filter out the activated products:

Code: Select all

	public function getStatusByProductID($product_id) { 
	
		$query = $this->db->query("SELECT `status` FROM `product` WHERE `product_id`=".$product_id);
		return $query->rows;
	}

2. in controller\product\product.php

Having built the queries, I changed the controller. First we need to know what category we are talking about, which we can see in the 'path' variable in the URL. I changed the following code adding what´s in between '//ADDITION' and ' //END ADDITION' tags. The code is basically the same as in the category.php file.

Code: Select all

if (isset($this->request->get['path'])) {
			$path = '';
				
			// ADDITION
			$parts = explode('_', $this->request->get['path']);
			// END ADDITION

			foreach (explode('_', $this->request->get['path']) as $path_id) {
				$category_info = $this->model_catalog_category->getCategory($path_id);
				
				if (!$path) {
					$path = $path_id;
				} else {
					$path .= '_' . $path_id;
				}
				
				if ($category_info) {
					$this->document->breadcrumbs[] = array(
						'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $path),
						'text'      => $category_info['name'],
						'separator' => $this->language->get('text_separator')
					);
				}
			}

		// ADDITION
					$category_id = array_pop($parts);
		} else {
			$category_id = 0;
		}
			
		$this->data['category_id'] = $category_id;
			
		// END ADDITION
Secondly i created the hyperlinks for the next and previous button. the code goes after the MODEL is loaded and the product_id is defined. again between the ADDITION and END ADDITION tags.

Code: Select all

$this->load->model('catalog/product');
		
		if (isset($this->request->get['product_id'])) {
			$product_id = $this->request->get['product_id'];
		} else {
			$product_id = 0;
		}
		
		// ADDITTION!!!
		
		$productsByCategoryID = $this->model_catalog_product->getProductsIDbyCategoryID($category_id); // create an array with our products
		
		
		for ($i=0;$i < count($productsByCategoryID); $i++) { // remove items that are not activated
			$statusProducts = $this->model_catalog_product->getStatusByProductID($productsByCategoryID[$i]["product_id"]);
			if ($statusProducts[0]["status"] == 0) {
				unset( $productsByCategoryID[$i] );
			}
		}
		
		foreach ($productsByCategoryID as $key => $value){
       			if ($productsByCategoryID[$key]["product_id"] == $product_id) {
					$next = current ($productsByCategoryID); //move back and forward in the array to create the hyperlinks for the NEXT and PREVIOUS buttons.

					prev($productsByCategoryID);
					if (!$next) {
						end ($productsByCategoryID);
					}
					$prev = prev ($productsByCategoryID);
					
					$url = 'index.php?route=product/product';
				
					if (isset($this->request->get['path'])) {
						$url .= '&path=' . $this->request->get['path'];
					}
					    				
					if ($next){ // there is only a next button if there is a next product to see
							$url_next = $url.'&product_id='. $next["product_id"];
							$this->data['next_url'] = $url_next;
					}else {
						$this->data['next_url'] = FALSE;
					}
					if ($prev){ // same here
						$url_prev = $url.'&product_id='. $prev["product_id"];
						$this->data['previous_url'] = $url_prev;
					} else {
						$this->data['previous_url'] = FALSE;
					}
				}   
				next ($productsByCategoryID); // set the array pointer to the next product
    	}
		
		// END


[b]3. in view\theme\catalog\[i]themename[/i]\template\product\product.php[/b]

create a div for the next and previous buttons:

	[code]	<?php if ($previous_url) { ?>
        	<a href="<?php echo ($previous_url); ?>"><</a>
        <?php } ?>
        <?php if ($next_url) { ?>
        	
        	<a href="<?php echo ($next_url); ?>">></a>
        <?php } ?>

that´s it.

Newbie

Posts

Joined
Sun May 16, 2010 3:09 am

Post by crawford » Thu Aug 12, 2010 6:26 pm

Hi,

works great, thank you for supplying the code!

I get an error, but just for all products in 1 category:

'Undefined offset: 0 in .../<template>/shop/catalog/controller/product/product.php'

it's the line
if ($statusProducts[0]["status"] == 0) {

that produces this error.
Any idea how to track this problem?

Thank you,

Michael

New member

Posts

Joined
Mon Dec 28, 2009 8:10 pm

Post by konservasi » Mon Sep 13, 2010 11:53 am

this is what I looking for..thank you very much

may be if you wanna share it at extension menu, there are a lot of people will download this modification

http://galerigis.com

http://galeritiket.net


Active Member

Posts

Joined
Wed May 05, 2010 2:29 am


Post by konservasi » Mon Sep 13, 2010 1:10 pm

not works for me

when I added this

$category_id = array_pop($parts);
} else {
$category_id = 0;

there is an error

needs assistant here

http://galerigis.com

http://galeritiket.net


Active Member

Posts

Joined
Wed May 05, 2010 2:29 am


Post by starbug » Thu Jun 16, 2011 1:31 am

Hi,
just a quick question... Does this solution work with SEO URL'S turned on?

http://www.emptypaper.net/


Newbie

Posts

Joined
Mon May 30, 2011 12:25 am

Post by emmetje » Tue Oct 18, 2011 3:40 am

I get a error on product.tpl:

Notice: Undefined variable: previous_url in /***/public_html/catalog/view/theme/default/template/product/product.tpl on line 76Notice: Undefined variable: next_url in /***/public_html/catalog/view/theme/default/template/product/product.tpl on line 79

I did make the changes on controller/product/product.php. I'm using 1.5.1

Could somebody please help me out, I need it urgent. I tried the solution provided on the extentions (http://www.opencart.com/index.php?route ... order=DESC) but that's a joke really. It just add's +1 or -1 to the products id. It does not check the products category or if its active :S

User avatar
New member

Posts

Joined
Wed Jun 22, 2011 2:18 am

Post by alvinlei » Sun Dec 11, 2011 9:26 pm

I also want the previous and next buttons but I am not a programmer. Not actually know know how to edit the files.
do you want to post the solution for sale? thx a lot
my opencart version is 1.5.1.3

Newbie

Posts

Joined
Sat Dec 10, 2011 6:26 pm

Post by lbaxterl » Wed Jan 25, 2012 7:36 pm

i Can confirm this works for 1.5.1.3, but to answer someones else question it doesnt work correctly with SEO URLS turned on. It does go to the next/previous products just with the traditonal url not the SEO one.

Thanks for the code.

Newbie

Posts

Joined
Fri Jun 17, 2011 9:56 pm

Post by emmetje » Wed Jan 25, 2012 8:20 pm

lbaxterl wrote:i Can confirm this works for 1.5.1.3, but to answer someones else question it doesnt work correctly with SEO URLS turned on. It does go to the next/previous products just with the traditonal url not the SEO one.

Thanks for the code.
I didn't manage, would you be so kind to post the code? Many thanks in advance!!
emmetje wrote:I get a error on product.tpl:

Notice: Undefined variable: previous_url in /***/public_html/catalog/view/theme/default/template/product/product.tpl on line 76Notice: Undefined variable: next_url in /***/public_html/catalog/view/theme/default/template/product/product.tpl on line 79

I did make the changes on controller/product/product.php.

User avatar
New member

Posts

Joined
Wed Jun 22, 2011 2:18 am

Post by lbaxterl » Wed Jan 25, 2012 8:40 pm

The code I used is in the first post i havent modified it at all, i have noticed a bug with my mulstore setup though which i m currently working on. If you would like me to have a look over your files just pm me.

Newbie

Posts

Joined
Fri Jun 17, 2011 9:56 pm

Post by lbaxterl » Wed Jan 25, 2012 9:14 pm

Hello Just an update to solve the multistore problem and getting SEO URLS working use this guys method:

http://forum.opencart.com/viewtopic.php ... 79#p219397

thanks

Newbie

Posts

Joined
Fri Jun 17, 2011 9:56 pm

Post by emmetje » Mon Jan 30, 2012 11:23 pm

Thx for the link everything is working fine now. So happy :)

User avatar
New member

Posts

Joined
Wed Jun 22, 2011 2:18 am
Who is online

Users browsing this forum: No registered users and 10 guests