Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Abstract in Magento

Tags:

php

magento

I am trying to override an abstract in Magento but it does not like the info in my config.xml below you will see my folder structure for the abstract I am trying to override and the portion of my config.xml specific to this file.

Original Directory:

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 the Abstract.php

abstract class EGeeked_Coupons_Rule_Model_Abstract extends Mage_Core_Model_Abstract
like image 895
Jordan Avatar asked Apr 11 '13 21:04

Jordan


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.

Can you override an abstract method?

you can override both abstract and normal methods inside an abstract class. only methods declared as final cannot be overridden.

Which method we Cannot be override abstract?

If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods.


1 Answers

First, a terminology lesson. Next, explaining why you can't do that. Third, an alternate solution that's not ideal but may get you where you need to go.

Terminology

From the looks of your question, you're not trying to "override" a class, you're trying to rewrite the class. A class rewrite is where you add configuration nodes to Magento in order to tell it

Instantiate this class instead of that class

An "override" is where you copy a class from the Magento core into 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 "use this class file instead of the other class file". Similar, but different from a rewrite. Rewrites are considered better practice because they're less destructive, and less likely to cause problems with upgrades and extension compatibility.

You can't do that

You can't rewrite an abstract class. The rewrite system works because Magento uses a factory pattern to instantiate model, blocks, and helpers

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

What a rewrite does is, 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
    }
}

Since an abstract class is never instantiated, it can never be rewritten.

Solutions

From what I can tell, there's only three classes that inherit from 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 could add a rewrite for each of these classes individually, ideally putting your new logic in a shared helper class. You'd need to handle extensions or custom code in a similar way, but it's one possible path forward.

The other 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 let you modify a single Abstract class, but you'll need to merge any changes from upgraded versions into this class — and you may cause system problems when (not if) you forget to do that.

Good luck!

like image 135
Alan Storm Avatar answered Sep 20 '22 18:09

Alan Storm