Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override/Extend Magento Abstract class?

I only need to add a method to the Mage_Core_Model_Abstract class.

For that, in my config.php I have

<models>
     <mymodule>
         <class>Mynamespace_Mymodule_Model</class>
     </mymodule>
     <core>
         <rewrite>
             <mage_core_model_abstract>Mynamespace_Mymodule_Model_Abstract</mage_core_model_abstract>
         </rewrite>
     </core>
</models>

It doesn't really work..

I know that there is a way of "overriding" by copying all the folder structure including the class file into my module, but I would like to know if there is a way to make it more fancy, within my module - something similar as above..

like image 967
Michael Avatar asked Apr 01 '13 07:04

Michael


People also ask

Can we override a abstract class in magento2?

Yes. You can write plugins for abstract classes and plugins should always be preferred over preferences if possible. Preferences are useful if you want to replace an implementation.

Is overriding possible in abstract class?

A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.

Can abstract method be overridden?

Abstract methods cannot be overridden by a concrete subclass.

How do I override a class in magento 2?

You have to put the path of the class override the target class in the "type" attribute and set sortOrder for you plugin. Magento 2 will run your plugin based on sortOrder. We will use After listener Plugin for example. You have to pay attention to your plugin class, it does not extend the class you want to override.


1 Answers

You can only rewrite classes that you instantiate directly, from config.xml.

The various values in config.xml are used when creating an object through a factory (e.g. Mage::getModel($class), Mage::getSingleton($class), Mage::helper($class), etc). The value of the $class argument is used to translate catalog/product into Mage_Catalog_Model_Product.

This means it's impossible to rewrite (in the Magento sense) classes that are used as superclasses, including abstract classes (by definition).

If you want to redefine any class that's used as a superclass, then you'll need to place a file in the correct place further up the include path. In this case, you'd need to make a file in app/code/local/Mage/Core/Model/Abstract.php.

like image 108
Nick Avatar answered Sep 24 '22 05:09

Nick