0
votes

I have a problem with overriding the core file. Basic path is app/code/core/Mage/Catalog/Block/Product/List.php

My module structure:

CompanyName/
  ModuleName/
    Block/
      Product/
        List.php
    etc/
      config.xml

My files: app/etc/modules/CompanyName_ModuleName.xml

<config>
    <modules>
        <CompanyName_ModuleName>
            <active>true</active>
            <codepool>local</codepool>
        </CompanyName_ModuleName>
    </modules>
</config>

../CompanyName/ModuleName/etc/config.xml

<config>
    <modules>
        <CompanyName_ModuleName>
            <version>1.0</version>
        </CompanyName_ModuleName>
    </modules>
    <global>
        <blocks>
            <CompanyName_ModuleName>
                <class>CompanyName_ModuleName_Block</class>
            </CompanyName_ModuleName>
            <catalog>
                <rewrite>
                    <product_list>CompanyName_ModuleName_Block_Product_List</product_list>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

and ../CompanyName/ModuleName/Block/Product/List.php

class CompanyName_ModuleName_Block_Product_List extends Mage_Catalog_Block_Product_List
{
    public function _getProductCollection()
    {
      (...)
    }
}

I don't know why it doesn't work. I can't see the mistake.

1
enable the logs form backend and then check the system.log , what error is showing ? - Vishal Sharma
i think you have to follow the CompanyName as Companyname and ModuleName as Modulename.... some times it also create the problem .... - Vishal Sharma

1 Answers

2
votes

There is a typo in your case. In your CompanyName_ModuleName.xml, it should be

<codePool>local</codePool>

Note the camel-letter P in codePool. Due to this typo, magento most probably didn't activated your module. That is why nothing happens through your module

Also for rewrite a block, you don't want define block section. This is enough in config.xml under global node

     <blocks>
        <catalog>
            <rewrite>
                <product_list>CompanyName_ModuleName_Block_Product_List</product_list>
            </rewrite>
        </catalog>
    </blocks>

Please note your module should reside in local codepool. Please ensure that also.

Always try to avoid unwanted camel-letters in Namespace and in modulename. It is not wrong actually. But instead of CompanyName_ModuleName, you can use Companyname_Modulename. It would helps you to avoid unwanted typos.

It is a small recommendation from my part

Eventhough you dont need the following part in your config.xml, I would like to mention one thing here. If your module has its own block inside it, except rewriting blocks, you should have this code in your config.xml

<global>
     <companyname_modulename> <!-- unique identifier; block reference -->
                <class>CompanyName_ModuleName_Block</class>
    <companyname_modulename>
     ....
</global>

Later you will going to refer your blocks using the unique identifier companyname_modulename. The rule for specifying this reference node is "It should be unique". By convention, module name is used for this. So in this case, convention says, you have to use modulename as block reference. But some time, there is a chance to make conflict with other custom extensions, if we use only module name as the reference.So in order to ensure this one is unique, I strongly recommend namespace_modulename format. Note that I used small letters. In your case you used Camel-letters also. It is not a good practice.

But the above thing is not relevant in your case. Since your module does not hold any blocks of your module itself, but only a rewrite block. So the above mentioned part of code is not necessary for you. This is because, in rewrite section, we are explicitly tells magento that where should it look for that rewrite block. In your case, you are specifying it like CompanyName_ModuleName_Block_Product_List.

Hope that make sense.