添加以下这个Helper类:
<?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;
}
}
在产品页的模板文件中直接调用这个类里的函数:
<?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; ?>
这样就很简单的在产品详细页面添加了指向上一个产品和下一个产品的链接。
PS:我自己之前写过一个上一个和下一个产品链接的Block:http://blog.csdn.net/shuishui8310/article/details/6136578,这个是根据产品ID的顺序来取的,合理性不如inchoo的根据产品在当前分类下的位置(Position)来取,所以推荐本篇文章提供的方式来实现上一个和下一个产品,有兴趣的可以对比下代码。
PS:inchoo的代码我还没测试过,但看了下写的很合理,所以推荐下
转自:http://inchoo.net/ecommerce/magento/simple-helper-class-for-previous-next-products-in-magento/