I am trying to set up next and previous links for products in the same category.
based on http://forum.opencart.com/viewtopic.php?t=17355
I am currently running 1.5.1.3.
The original concept is based on 1.4.x or 1.3.x, so there are some things that need to be changed, I would really appreciate if you could be able to help fix the code for 1.5.x
The modification is done as follows:
1. In /catalog/model/catalog/product.php in ModelCatalogProduct class
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
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 getProductsIDbyCategoryID($category_id) {
$query = $this->db->query("SELECT `product_id` FROM `product_to_category` WHERE `category_id` = '".$category_id."'");
return $query->rows;
}
public function getStatusByProductID($product_id) {
$query = $this->db->query("SELECT `status` FROM `product` WHERE `product_id`=".$product_id);
return $query->rows;
}
ORIGINAL CODE FOR 1.3.x or 1.4.x included.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
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
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
<?php if ($previous_url) { ?>
<a href="<?php echo ($previous_url); ?>"><</a>
<?php } ?>
<?php if ($next_url) { ?>
<a href="<?php echo ($next_url); ?>">></a>
<?php } ?>
Thanks in advance!

Also, I commit to pay for a coffee/beer at your favorite place via paypal to your account.