浏览文章

文章信息

Magento1 覆盖block类 | Magento1 how to overwriter block class 10699

1、创建模块

关于怎么创建模块请到:

如何创建模块:https://bbs.aiweline.com/thread-121041.htm

2、覆盖Block类

假如我们要覆盖:Mage_Checkout_Block_Cart_Sidebar

第一步:

修改app/code/local/Aiweline/VideoProduct/config.xml文件,告诉系统我们要干的事

<?xml version="1.0"?>
<config>
    <modules>
        <Aiweline_VideoProduct>
            <version>0.1.0</version>
            <depends>
                <Smartwave_Porto/>
            </depends>
        </Aiweline_VideoProduct>
    </modules>
    <global>
        <models>
            <welinevp>
                <class>Aiweline_VideoProduct_Model</class>
                <resourceModel>welinevp_resource</resourceModel>
            </welinevp>
            <welinevp_resource>
                <class>Aiweline_VideoProduct_Model_Resource</class>
            </welinevp_resource>
        </models>
        <blocks>
            <welinevp>
                <class>Aiweline_VideoProduct_Block</class>
            </welinevp>
            <!--这是我们要覆盖的 Block START -->
            <checkout>
                <rewrite>
                    <cart_sidebar>Aiweline_VideoProduct_Block_Cart_Sidebar</cart_sidebar>
                </rewrite>
            </checkout>
            <!--这是我们要覆盖的 Block END -->
        </blocks>
        <helpers>
            <welinevp>
                <class>Aiweline_VideoProduct_Helper</class>
            </welinevp>
        </helpers>
        <!--升级资源-->
        <resources>
            <welinevp_setup>
                <setup>
                    <module>Aiweline_VideoProduct</module>
                    <class>Mage_Core_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </welinevp_setup>
        </resources>
    </global>
</config>

解释:

<?xml version="1.0"?>
<config>
     ...
    <global>
        ...
        <blocks>
            ...
            <checkout>
                 <rewrite>
                     <cart_sidebar>Aiweline_VideoProduct_Block_Cart_Sidebar</cart_sidebar>
                 </rewrite>
             </checkout>
             ...
         </blocks>
       ...
    </global>
</config>

checkout:是Magento模块的资源标志(标志要操作Mage_Checkout模块),在如何创建模块的文章中我讲过。

rewrite:操作是覆盖

cart_sidebar:这个是要覆盖的类,这个类全部小写,且去除mage_checkout_block后剩下的部分。全部class名是无意义的,因为<checkout>已经告知系统是Mage_Checkout模块,而<blocks>告知系统是Block目录,你只要告诉Block目录下的class路径位置就可以了。Magento1的路径搜索(目录名和类名一致,类名用“_”连接)会找到这个类的。

第二步:创建:app/code/local/Aiweline/VideoProduct/Block/Cart/Sidebar.php

<?php
class Aiweline_VideoProduct_Block_Cart_Sidebar extends Mage_Checkout_Block_Cart_Sidebar
{
    function getItemHtml(Mage_Sales_Model_Quote_Item $item)
    {
        p($item->getData('welinevp_video'));
        return parent::getItemHtml($item);
    }
}

提示:p函数是我写的调试函数,用于验证是否生效。


3、验证

重新编译

项目目录下执行

php -f shell/compiler.php -- disable
php -f shell/compiler.php -- clear
php -r 'require "app/Mage.php"; Mage::app()->getCacheInstance()->flush();'

提示:禁用编译->启用编译->清理缓存

然后访问url查看即可!


完毕!喜欢的注册发帖支持!


原创