提供Magento上一個和下一個產品的簡單幫助(Helper)類
添加以下這個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 <https://inchoo.net> * @license https://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 href="<?php echo $_prev;?>"><?php echo $this->__('< Previous')?></a><?php endif; ?> <?php if($_next): ?><a href="<?php echo $_next;?>"><?php echo $this->__('Next >')?></a><?php endif; ?>
這樣就很簡單的在產品詳細頁麵添加了指向上一個產品和下一個產品的鏈接。
PS:我自己之前寫過一個上一個和下一個產品鏈接的Block:https://blog.csdn.net/shuishui8310/article/details/6136578,這個是根據產品ID的順序來取的,合理性不如inchoo的根據產品在當前分類下的位置(Position)來取,所以推薦本篇文章提供的方式來實現上一個和下一個產品,有興趣的可以對比下代碼。
PS:inchoo的代碼我還沒測試過,但看了下寫的很合理,所以推薦下
轉自:https://inchoo.net/ecommerce/magento/simple-helper-class-for-previous-next-products-in-magento/
最後更新:2017-04-02 06:52:15