Redefining Abstract in Magento

I am trying to override abstract text in Magento, but I do not like the information in my config.xml below, you will see my folder structure for the abstract that I am trying to override, and part of my config.xml specific to this file.

Original catalog:

app/code/core/Mage/Rule/Model/Abstract.php

My directory:

app/code/local/EGeeked/Coupons/Rule/Model/Abstract.php

My config.xml:

 <models> <EGeeked_Coupons> <class>EGeeked_Coupons_Rule_Model_Abstract</class> </EGeeked_Coupons> <rule> <rewrite> <model_abstract>EGeeked_Coupons_Rule_Model_Abstract</model_abstract> </rewrite> </rule> </models> 

My extends in Abstract.php

 abstract class EGeeked_Coupons_Rule_Model_Abstract extends Mage_Core_Model_Abstract 
+4
source share
2 answers

First, a terminology lesson. Then explain why you cannot do this. Thirdly, an alternative solution that is not perfect, but can lead you to where you need to go.

Terminology

From the point of view of your question, you are not trying to "redefine" the class, you are trying to rewrite the class. Class rewriting is where you add Magento configuration nodes to report it

Create this class instead of this class

"override" is where you copy the class from the Magento core to the local code pool. In other words, copy

 app/code/core/Mage/Rule/Model/Abstract.php 

to

 app/code/local/Mage/Rule/Model/Abstract.php 

An override is where you tell Magento to "use this class file instead of another class file." Sounds like, but different from rewriting. Rewriting is considered best practice because it is less damaging and less likely to cause problems with updates and extension compatibility.

You cannot do it

You cannot rewrite an abstract class. The rewrite system works because Magento uses the factory template to create models, blocks and helpers

 $class = Mage::getModel('catalog/product'); 

What is rewriting in pseudo-code

 function getModel($model) { if("can I find a rewrite configuration for $model") { //instantiate the model with the rewrite } else { //instantiate the normal model } } 

Because an abstract class is never created, it can never be rewritten.

Decision

From what I can tell, there are only three classes that inherit this abstract class in a standard Magento installation.

 catalogrule/rule Mage_CatalogRule_Model_Rule rule/rule Rule/Model/Rule.php salesrule/rule SalesRule/Model/Rule.php 

You can add a rewrite for each of these classes separately, ideally putting your new logic in a common helper class. You will need to handle extensions or custom code in a similar way, but this is one possible way forward.

Another alternative is to use a traditional class override and copy

 app/code/core/Mage/Rule/Model/Abstract.php 

to

 app/code/local/Mage/Rule/Model/Abstract.php 

This will allow you to change one Abstract class, but you will need to merge any changes from the updated versions into this class - and you can cause system problems when (and not if) you forget to do it.

Good luck

+15
source

It is true that you cannot override an abstract class from the Magician, but you can override its specific method using a custom extension from the local average instead of putting the whole Abstract.php in the same local place. It can be redefined from the local one using its own module.

For example, if I need to change the reordering action in Mage, which is written in:

 app\code\core\Mage\Sales\Controller\Abstract.php 

then I can override its controller from my module as shown below in my config.xml :

 <config> <modules> <Custom_Sales> <version>1.0.0</version> </Custom_Sales> </modules> <frontend> <routers> <sales> <args> <modules> <custom_sales before="Mage_Sales">Custom_Sales</custom_sales> </modules> </args> </sales> </routers> </frontend> </config> 

After that add the controller to the module:

 app\code\local\Custom\Sales\controllers\OrderController.php 

in the following way:

 require_once(Mage::getModuleDir('controllers','Mage_Sales').DS.'OrderController.php'); class Custom_Sales_OrderController extends Mage_Sales_OrderController { public function reorderAction() { "Add required changes !!"; } } 

Everything is set to run the abstract method from the local module. Similarly, you can override the abstract model method.

+1
source

All Articles