One of the common requirements in Magento is to have prev/next product on Product Information page. Development logic there is simple, fetch current product id and take the one before and after that one and now you have previous and next products Remember that this will work per category only, since you will get list of products in current category.
So how will next code work?
If you dump next line of code in your catalog/product/view.phtml:
Mage::registry('current_category')->getProductsPosition();
You will get array with keys of product id’s and product positions as values of that current category.
I’m not going to put full module here since you can use this helper in any other module, you just need to call it with right name in your template file.
Here comes the helper class:
<?php
/**
* INCHOO's FREE EXTENSION DISCLAIMER
*
* Please do not edit or add to this file if you wish to upgrade Magento
* or this extension to newer versions in the future.
*
* Inchoo developers (Inchooer's) give their best to conform to
* "non-obtrusive, best Magento practices" style of coding.
* However, Inchoo does not guarantee functional accuracy of specific
* extension behavior. Additionally we take no responsibility for any
* possible issue(s) resulting from extension usage.
*
* We reserve the full right not to provide any kind of support for our free extensions.
*
* You are encouraged to report a bug, if you spot any,
* via sending an email to bugreport@inchoo.net. However we do not guaranty
* fix will be released in any reasonable time, if ever,
* or that it will actually fix any issue resulting from it.
*
* Thank you for your understanding.
*/
/**
* @category Inchoo
* @package Inchoo_Catalog
* @author Vedran Subotic <vedran@inchoo.net>
* @copyright Inchoo <http://inchoo.net>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Inchoo_Catalog_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getPreviousProduct()
{
$prodId = Mage::registry('current_product')->getId();
$catArray = Mage::registry('current_category');
if($catArray){
$catArray = $catArray->getProductsPosition();
$keys = array_flip(array_keys($catArray));
$values = array_keys($catArray);
$productId = $values[$keys[$prodId]-1];
$product = Mage::getModel('catalog/product');
if($productId){
$product->load($productId);
return $product->getProductUrl();
}
return false;
}
return false;
}
public function getNextProduct()
{
$prodId = Mage::registry('current_product')->getId();
$catArray = Mage::registry('current_category');
if($catArray){
$catArray = $catArray->getProductsPosition();
$keys = array_flip(array_keys($catArray));
$values = array_keys($catArray);
$productId = $values[$keys[$prodId]-1];
$product = Mage::getModel('catalog/product');
if($productId){
$product->load($productId);
return $product->getProductUrl();
}
return false;
}
return false;
}
}
All you need is to generate new links on product view page:
<?php $_prev = $this->helper('inchoo_catalog')->getPreviousProduct(); ?>
<?php $_next = $this->helper('inchoo_catalog')->getNextProduct(); ?>
<?php if($_prev): ?><a class="product-prev" href="<?php echo $_prev;?>"><?php echo $this->__('< Previous')?></a><?php endif; ?>
<?php if($_next): ?><a class="product-next" href="<?php echo $_next;?>"><?php echo $this->__('Next >')?></a><?php endif; ?>
Note: this will not work for all cases in layered navigation, since layered navigation doesn’t work that way and may consist of product collection from different categories.
Enjoy coding!
The post Simple Helper class for Previous/Next Products in Magento appeared first on Inchoo.