Magento Rewrite Block

When develop a site base on Magento. Everyone have to rewrite some block to customize the information which will display on template page. 

 

Before start to make a sample, Some magento term will be introduce:

Namespace: The name of folder under the /app/code/core OR /app/code/local.

 

For example rewrite the product/view block display normal information on the product detail page.

1.  Disable the cache 

2.  Create a new folder under /app/code/local/ named "Red"(Namespace), add new module structure:

/app/code/local/Catalog

            /Catalog/Block

            /Catalog/etc/config.xml

3.  Changed the config.xml as following:

<?xml version="1.0"?>

<config>

    <modules>

        < Red _Catalog>

<version>0.1.1</version>

</ Red _Catalog>

</modules>

</ config >

4.  Add module config file like /app/etc/modules/Red_All.xml as following:

<?xml version="1.0"?>

<config>

<modules>

        < Red _Catalog>

            <active>true</active>

            <codePool>local</codePoo l>

    </ Red _Catalog>

</ modules >

</config>

5.  Go to admin panel, Enable the cache and then Refresh it.     System->configuration->Advanced->Advanced ->Disable modules output

You will see a module named Red_Catalog Enable. Good lucky! Then Disable the cache.

6.  Create new product/view block class: 

Create a new file: / app/code/local/Catalog/Block/Product/View.php look like this:

<?php

class Red_Catalog_Block_Product_view extends Mage_Catalog_Block_Product_view

{

     public function getMoreInfor()

     {

          return "The new promotion is on our site!";

     }

}

7. Change the /app/code/local/Catalog/etc/config.xml like this: 

<?xml version="1.0"?>

<config>

    <modules>

        < Red _Catalog>

<version>0.1.1</version>

</ Red _Catalog>

</modules>

    <global>

<blocks>

<catalog>

<product_view> Red _Catalog_Block_Product_View</product_view>  

            </catalog>

        </blocks>

    </global>

</ config >

NOTE:  <product_view> Red _Catalog_Block_Product_View</product_view>  can't have any space. Except the space before the starting of this line. And can't divide this into many lines;

  8. Change the template file under the theme you choosed as following:

  9. Enabled cache. Go to the product detail page:

     Under the head: you will see one line text with red color like this:

     The new promotion is on our site!

    So far: the catalog product view rewriting is finished, Good Lucky.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值