Saturday , June 24 2017
Home >> All tutorial >> Magento developers’ guide – Part 8: Rewrite/modify a Magento block

Magento developers’ guide – Part 8: Rewrite/modify a Magento block

Today, Magento Tutorial will show you how to change a basic block of Magento. When you do this kind of modification, it is better to keep in mind one of the basic rules Magento development:

– You can not edit in the files in/app/code/core.

Instead, we will overload this Block, create a class that will extend this core Block and rewrite only the part you want to change. Then, in order to use it, declare your file in our config.xml file (as usual).

1. Start by re-creating a module in your folder Pfay and call it MyProduct

So create the file:

/app/code/local/Pfay/MyProduct

With etc/config.xml and do not forget to declare the Pfay_All.xml (see lesson 2 ).

2. Create your « modified » Block

What interests us is to rewrite the block Mage_Catalog_Block_Product_View.  We want to rewrite the method « showInfos » of this Block.

So we will create in /app/code/local/Pfay/MyProduct/Block/Product/file view.php.

Which will contain:

  1. Class Pfay_MonProduit_Block_Product_View extends Mage_Catalog_Block_Product_View
  2. {
  3.    public function afficherLesInfos()
  4.    {
  5.       return ‘les infos complementaires’;
  6.    }
  7. }

3. Tell Magento to take into accounts the new version of this block

Edit the config.xml of your module MyProduct and put in it:

  1. <?xml version=“1.0”?>
  2. <config>
  3.  <modules>
  4.     <Pfay_MonProduit>
  5.         <version>1.0.0</version>
  6.     </Pfay_MonProduit>
  7.  </modules>
  8.  <global>
  9.    <blocks>
  10.     <catalog>
  11.          <rewrite>
  12.               <product_view>
  13.                     Pfay_MonProduit_Block_Catalog_Product_View
  14.                </product_view>
  15.              </rewrite>
  16.       </catalog>
  17.   </blocks>
  18.  </global>
  19. </config>

With these tags, we say we will configure a block of Magento’s core called « catalog » and we will rewrite () the « product_view » block of this module. Now it will take our new block to replace the base.


Ready to foster your knowledge with MAGENTO 2 BASIC GUIDE

The new Order Success combo with Magento 2 Inventory Management, Magento 2 Order Fulfillment, Magento 2 Dropship, Magento 2 Barcode Management and Magento 2 Purchase Management

Is your site still a mess and Magento extensions are not enough to save your site? Save your site with Magento Web Development Services now

magento services


About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *