Magento Rewrite Block

来源:互联网 发布:网络语言黑洞的意思 编辑:程序博客网 时间:2024/05/04 16:37

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</codePool>

    </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.