|
Usibility wise I think it would be great to allow the user to page back and forward through the products in the order they displayed on the product list page/category page (sort by order as well).
I gave it a go and came up with the following solution - would appreciate it if anyone could better the code:
In the list template I created a session variable that holds all the product url’s of the products listed:
.../templates/catalog/product/list.phtml after the 1st else statement around line #36
<?php $prodPgNavArr = array(); $i=0; foreach ($_productCollection as $_product) { $prodPgNavArr[$i] = $_product->getProductUrl(); $i++; } // put the array in a session variable $_SESSION['prodPgNav']=$prodPgNavArr;
?>
and retrieve these on the product page and conditionally display ‘previous’ and ‘next’ links:
.../templates/catalog/product/view/media.phtml at the top:
<?php
// get current product URl $currentPath = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // $prodPgNavArr = $_SESSION['prodPgNav']; // loop through the session array to find index of current product in product list/array foreach($prodPgNavArr as $key=>$value) { if($currentPath == $value) { $prevIndex = $key - 1; $nextIndex = $key + 1; } } if (isset($prevIndex) && $prevIndex!=-1) { echo "<div>“ <a href='$prodPgNavArr[$prevIndex]'>previous</a></div>"; } if (isset($nextIndex) && $nextIndex<sizeof($prodPgNavArr)) { echo "<div>” <a href='$prodPgNavArr[$nextIndex]'>next</a></div>"; } ?>
Okay this works, but only lists the first 8 products from my product listing page, because that’s what I set my listing limit per page at. Instead I would appreciate advice on how to retrieve ALL the products in the listing.
I hope this makes sense. If there’s a better (object orientated) way of solving this, please let me know.
|