Magento 1.9 Block Override Not Working – Fix Guide

blocksfrontendmagento-1.9overrides

i try rewrite my block on app/local/lesson7/test/block but its not working
my modules xml (lesson7.xml)

<?xml version="1.0"?>
<config>
    <modules>
        <Lesson7_Test>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Lesson7_Test>
    </modules>
</config>

my layout on app/design/frontend/tutorial/default/layout/lesson7.xml

<?xml version="1.0"?>
<layout version="0.1.0">
  <default>
      <reference name="content">
      </reference>
  </default>
  <lesson7_index_index>
       <reference name="content">
            <block type="lesson7/monblock" name="afficher_monbloc" template="lesson7/afficher.phtml" />
       </reference>
  </lesson7_index_index>
</layout>

my template on app/design/frontend/tutorial/default/template/lesson7/afficher.phtml

<?php

    echo $this->methodblock();

?>

my config on app/local/etc/lesson7/test/etc/config.xml

<?xml version="1.0"?>
  <config>
     <modules>
        <Lesson7_Test>
          <version>1.0.0</version>
        </Lesson7_Test>
     </modules>
 <frontend>
   <routers>
      <lesson7>
          <use>standard</use>
          <args>
             <module>Lesson7_Test</module>
             <frontName>lesson7</frontName>
          </args>
       </lesson7>
   </routers>
   <layout>
     <updates>
          <lesson7>
               <file>lesson7.xml</file>
           </lesson7>
      </updates>
  </layout>
</frontend>
    <global>
      <blocks>
          <catalog>
              <rewrite>
                  <product_view>
                      Lesson7_Test_Block_Product_View
                  </product_view>
              </rewrite>
          </catalog>
        <lesson7>
          <class>Lesson7_Test_Block</class>
        </lesson7>
      </blocks>
    </global>
</config>

my block on app/local/lesson7/test/block/Monblock.php

<?php
class Lesson7_Test_Block_Monblock extends Mage_Core_Block_Template
{
     public function methodblock()
     {
         return 'Lesson 7 Block !!!' ;
     }
}

and my rewrite app/etc/lesson7/test/block/product/View.php

<?php
class Lesson7_Test_Block_Product_View extends Mage_Core_Block_Product_View
{
    public function afficherLesInfo()
    {
        return 'afficher les info !!!';
    }
}

UPDATE
is it possible his error was in my new block? is it true rewrite of models/resource can only be summoned by Mage such as Mage:: getModel (), Mage:: getResourceModel (), Mage:: Helper (), Mage:: getSingletonBlock () ?
please your help, thank you 🙂

Best Answer

Your extending block is wrong. There is no Mage_Core_Block_Product_View.php class

Replace this below content: Lesson7/Test/Block/Product/View.php (you should have all folder/file's first letter capital)

<?php
class Lesson7_Test_Block_Product_View extends Mage_Catalog_Block_Product_View
{
    public function afficherLesInfo()
    {
        return 'afficher les info !!!';
    }
}

I also doubt you are not placing your file in right place.

Check where is your View.php file? It should be in app/code/[codepool : community or local]/Lesson7/Test/Block/Product folder.

All your module files/folders should be inside this folder:

app/code/[codepool : community or local]
            |_Lesson7
                 |_Test
                    |_ Block
                    |_ etc

Hope this helps.

[UPDATE]

If you want to test then go to design/your_theme/template/catalog/product/view.phtml and have this code: <?php echo $this->afficherLesInfo();?>. And now go to any simple product and see if this appears. If it does then you have successfully rewritten the block.