3

I have been trying to override Magento resource collection class. I have found few solutions but it seems can't get it to work.
The class is located in:

App/code/core/Mage/Reports/Model/Resource/Order/Collection.php

This is the code to register module:

<?xml version="1.0"?> 
    <config>  
        <modules>  
            <XYZ_Reports>
                <active>true</active>  
                <codepool>local</codepool>  
            </XYZ_Reports>
        </modules>  
    </config> 

And XML config file to override collection:

<?xml version="1.0"?>
<config>
    <modules>
        <XYZ_Reports>
            <version>0.1.0</version>
        </XYZ_Reports>
    </modules>
    <global>
        <models>
            <reports>
                <rewrite>
                    <resource_order_collection>
                        XYZ_Reports_Model_Resource_Order_Collection
                    </resource_order_collection>
                </rewrite>
            </reports>
        </models>
    </global>    
</config>

This is a one version of config file. I have used different approaches including this one suggested by Damir Korpar - Inchoo:

<?xml version="1.0"?>
<config>
    <modules>
        <XYZ_Reports>
            <version>0.1.0</version>
        </XYZ_Reports>
    </modules>
    <global>
        <models>
            <reports_resource>
                <rewrite>
                    <order_collection>
                        XYZ_Reports_Model_Resource_Order_Collection
                    </order_collection>
                </rewrite>
            </reports_resource>
        </models>
    </global>    
</config>

The new file located in:

app/local/XYZ/Reports/Model/Resource/Order/Collection.php

And finally extended class:

<?php

class XYZ_Reports_Model_Resource_Order_Collection extends Mage_Reports_Model_Resource_Order_Collection
{

    public function joinCustomerName($alias = 'name')
    {
        die('Overridden class'); 
    }
}

3 Answers 3

4

change your config.xml to

    <?xml version="1.0"?>
    <config>
        <modules>
            <XYZ_Reports>
                <version>0.1.0</version>
            </XYZ_Reports>
        </modules>
        <global>
            <models>
                <reports_resource>
                    <rewrite>
                        <order_collection>XYZ_Reports_Model_Resource_Order_Collection</order_collection>
                    </rewrite>
                </reports_resource>
            </models>
        </global>
    </config>
5
  • As far as I know, XML parser discard the whitespace. But I have changed the code as you suggested and still can't get it to work.
    – BenGhabili
    May 12, 2016 at 8:32
  • 1
    in Magento this whitespace like above can cause issues. please make sure the model is succesfully overriden, create script in your magento root as following: include "app/Mage.php"; Mage::app(); echo get_class(Mage::getResourceModel('reports/order_collection')); exit;
    – Ilja
    May 12, 2016 at 8:53
  • Still echo outs: Mage_Reports_Model_Resource_Order_Collection
    – BenGhabili
    May 12, 2016 at 9:01
  • please check edited answer, i have tried it on my machine and it worked. whitespaces not allowed in magento. you can check it in your test script. if it helped you please accept the answer good luck
    – Ilja
    May 12, 2016 at 9:24
  • thanks @Anonymous. I have found the problem. I have a typo in defining module. <codepool> and I should have used <codePool>
    – BenGhabili
    May 12, 2016 at 9:59
0

To rewrite class Mage_Reports_Model_Resource_Order_Collection in your custom module, you can try below code
Code in your config.xml

....
        <global>
            <models>
                <yourmodule>
                    <class>Namespace_Yourmodule_Model</class>
                </yourmodule>

                <reports_resource>
                    <rewrite>                    
            <order_collection>Namespace_Yourmodule_Model_Reports_Resource_Order_Collection</order_collection>
                    </rewrite>
                </reports_resource>
            </models>
        </global>
    ....

Your rewrite class

<?php
class Namespace_Yourmodule_Model_Reports_Resource_Order_Collection extends Mage_Reports_Model_Resource_Order_Collection {
/*Your custom code/Methods */
}
1
  • I have seen this suggested in few places but I can't figured out what I should put for the model class name in Namespace_Yourmodule_Model
    – BenGhabili
    May 12, 2016 at 9:40
0

I have found the problem if someone have similar issue. I have typo in defining module. <codepool> should be camel-case. <codePool>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.