提供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 <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/

目录
相关文章
|
分布式计算 DataWorks 算法
DataWorks常见问题之更改odps表生命周期失败如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
|
12月前
|
Java 开发者
在Java编程中,正确的命名规范不仅能提升代码的可读性和可维护性,还能有效避免命名冲突。
【10月更文挑战第13天】在Java编程中,正确的命名规范不仅能提升代码的可读性和可维护性,还能有效避免命名冲突。本文将带你深入了解Java命名规则,包括标识符的基本规则、变量和方法的命名方式、常量的命名习惯以及如何避免关键字冲突,通过实例解析,助你写出更规范、优雅的代码。
342 3
|
12月前
|
存储 机器学习/深度学习 测试技术
[大语言模型-论文精读] 以《黑神话:悟空》为研究案例探讨VLMs能否玩动作角色扮演游戏?
[大语言模型-论文精读] 以《黑神话:悟空》为研究案例探讨VLMs能否玩动作角色扮演游戏?
307 0
|
12月前
|
网络协议 文件存储 Windows
Windows Server 2019 FTP服务器搭建
Windows Server 2019 FTP服务器搭建
362 0
|
存储 分布式计算 Hadoop
Hadoop中RecordReader的用途详解
【8月更文挑战第31天】
186 0
|
存储 数据挖掘 OLAP
阿里云 EMR Serverless StarRocks OLAP 数据分析场景解析
阿里云 E-MapReduce Serverless StarRocks 版是阿里云提供的 Serverless StarRocks 全托管服务,提供高性能、全场景、极速统一的数据分析体验,具备开箱即用、弹性扩展、监控管理、慢 SQL 诊断分析等全生命周期能力。内核 100% 兼容 StarRocks,性能比传统 OLAP 引擎提升 3-5 倍,助力企业高效构建大数据应用。本篇文章对阿里云EMR Serverless StarRocks OLAP 数据分析场景进行解析、存算分离架构升级以及 Trino 兼容,无缝替换介绍。
19458 12
|
JavaScript 前端开发
1天搞定SpringBoot+Vue全栈开发 (5)Vue框架快速上手
1天搞定SpringBoot+Vue全栈开发 (5)Vue框架快速上手
|
自然语言处理 编译器 程序员
【Qt底层之 元对象的编译】Qt 元对象系统及其编译流程解析
【Qt底层之 元对象的编译】Qt 元对象系统及其编译流程解析
762 4
|
机器学习/深度学习 数据采集 算法
【Python机器学习专栏】使用Scikit-learn进行数据编码
【4月更文挑战第30天】本文介绍了Python Scikit-learn库在机器学习数据预处理中的作用,尤其是数据编码。数据编码将原始数据转化为算法可理解的格式,包括标签编码(适用于有序分类变量)、独热编码(适用于无序分类变量)和文本编码(如词袋模型、TF-IDF)。Scikit-learn提供LabelEncoder和OneHotEncoder类实现这些编码。示例展示了如何对数据进行标签编码和独热编码,强调了正确选择编码方法的重要性。
381 0
|
存储 人工智能 边缘计算
云计算大势所趋:从基础到未来,探析发展趋势
云计算大势所趋:从基础到未来,探析发展趋势