Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a Magento function in app/code/core/Mage/Core/functions.php

I need to override a function in this file:

app/code/core/Mage/Core/functions.php

The problem is that this is so core that there’s no class associated to it, probably because Core isn’t even a module. Does anybody know how to override a function in the file without a class?

Any help would be appreciated.

like image 204
Adam Moss Avatar asked May 12 '11 12:05

Adam Moss


2 Answers

As mentioned by @tweakmag the disadvantages of creating a folder structure and copying the entire Model or controller for a single function override, most important being,

"Future upgrades could introduce new features that would not be available unless it is remembered to copy across the new version of that file and implement the changes again."

Thus a solution can be, to extend the core class (Model or controller) and just write the method you want to override, instead of copying all the methods.

For example, if you want to say, override a method getProductCollection in Mage_Catalog_Model_Category, here will be the steps:

  1. Create a Custom Namespace/Module folder with etc folder in it
  2. Register the module in app/etc folder by creating Namespace_Module.xml
  3. setup the config.xml in Namespace/Module/etc/ :

    <?xml version="1.0"?>
      <config>
         <modules>
           <Namespace_Module>
             <version>1.0</version>
           </Namespace_Module>
          </modules>
          <global>
           <models>
            <catalog>
             <rewrite>
               <category>Namespace_Module_Model_Category</category>
             </rewrite>
            </catalog>
          </models>
         </global>
      </config>
    
  4. create Model folder in Namespace_Module and a Php File Category.php

  5. Now the main difference here is we extend the Magento's core class instead of writing all the methods

    <?php
     /**
      * Catalog category model
     */
      class Namespace_Module_Model_Category extends Mage_Catalog_Model_Category
       {
       public function getProductCollection()
        {
         // Include your custom code here!
         $collection = Mage::getResourceModel('catalog/product_collection')
         ->setStoreId($this->getStoreId())
         ->addCategoryFilter($this);
         return $collection;
       }
     }

The getProductCollection method is overridden so it'll be called instead of the method defined in the core model class.

Also an important point is:

When you override any methods in models, you should make sure that the data type of the return value of that method matches with the data type of the base class method. Since model methods are called from several core modules, we should make sure that it doesn't break other features!

This link gives a step by step approach to it

like image 96
Ajay Bisht Avatar answered Oct 13 '22 18:10

Ajay Bisht


Copying the file to app/code/local/Mage/Core/functions.php should not be used because of the following reasons:

  1. The entire file has to be copied over making it harder for us to identify what changes have been made.

  2. Future upgrades could introduce new features that would not be available unless it is remembered to copy across the new version of that file and implement the changes again.

  3. Future upgrades could address bugs with core that we would miss unless it is remembered to copy across the new version of that file and implement the changes again.

  4. In respect to points 2 & 3 each upgrade could change the way things work that means revisiting what changes we need to make. In some cases this will be true for overriden methods as well but at least we can easily identify where those changes effect us.

  5. What do you do if another person wants to use the same technique? Being able to identify what is core code and what is ours becomes more and more complex.

  6. Keeping our code together as a “module” becomes more difficult as by copying in the core file means that we have effectively locked it into being “guaranteed” to run on the version of the software that we have copied the original code from. It also means reusing this work is a lot more difficult to do.

  7. Identifying why the code was changed it much harder as it is outside our namespacing, ie all development related to “Example_Module” is in the namespace:

/app/code/core/local/Example/Module

whereas code copied to app/code/core/local/Mage only indicates that we have made a change to support an unknown feature etc.

Also Magento occasionally release patches which fixes bugs – these will only patch files inside core leaving your copied file without the patch.

What I would suggest instead is that you write your own function to do what you want and override the function to call your new function instead.

like image 36
tweakmag Avatar answered Oct 13 '22 19:10

tweakmag