0

How to overwrite core model ? (what's wrong with below code?)

Trying to overwrite: \app\code\core\Mage\Catalog\Model\Category.php

//\app\code\local\Trening\Catalog\etc\config.xml
<?xml version="1.0"?>
<config>
    <global>
        <models>
            <catalog>
                <rewrite>
                    <category>Trening_Catalog_Model_Category</category>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

//\app\code\local\Trening\Catalog\Model\Category.php
class Trening_Catalog_Model_Category extends Mage_Catalog_Model_Category{
    public function getChildren()
    {
        echo 'overwrite';
    }
}

//\app\etc\modules\Trening_Catalog.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Trening_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </Trening_Catalog>
    </modules>
</config>

and below return original method:

$catal = Mage::getModel("Catalog/Category")->load(2);
var_dump($catal->getChildren());

1 Answer 1

1

You need to call model like this:

$catal = Mage::getModel("catalog/category")->load(2);
var_dump($catal->getChildren());

and change getChildren() method like this:

public function getChildren()
{
    return 'overwrite';
}

If you are still not getting the rewritten output, then you need to check following things:

  • Make sure you removed all cache.
  • Make sure any other modules are not rewriting the same class.

Other than these, I cannot see other issues with your module.

1
  • 1
    yes, catalog/category instead of Catalog/Category did a job. Thank You. Jan 13, 2017 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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