0
votes

My magento install has a free shipping shopping cart rule, as well as coupon rules. When a coupon is entered, the free shipping goes away. This is undesirable, but a known condition with Magento: Magento Free shipping and Coupon discount Note: If someone knows how to correct THAT issue without overriding, that would be preferable.

I've worked on this for about 3 hours. I've searched SO, Google, and others, and cannot find the solution.

Question:
I am attempting to write a module that corrects the behavior based on this post:
Magento Issue with Cart Price

The class I am attempting to override is Mage_SalesRule_Model_Resource_Rule_Collection
Using Alan Storm's free tool, I've found that the uri is salesrule/resource_rule_collection,
And doing some manual investigation, that the class is located here:
app/code/core/Mage/SalesRule/Model/Resource/Rule/Collection.php
There is ALSO a class here that extends the class from the above file:
app/code/core/Mage/SalesRule/Model/Mysql4/Rule/Collection.php
(which one of these should I be overriding?)

For the life of me, I cannot find the proper config.xml syntax to override the desired class.

My config currently looks like so:

<config>
    <modules>
        <My_Couponrule>
            <version>1.0.0</version>
        </My_Couponrule>
    </modules>
    <global>
        <models>
            <salesrule>
                <rewrite>
                    <resource_rule_collection>My_Couponrule_Model_Resource_Rule_Collection</resource_rule_collection>
                </rewrite>
            </salesrule>
        </models>
    </global>

However, I have carefully tried every combination I can deduce might be appropriate and none of them have worked:

<salesrule_resource_rule><rewrite><collection>
<salesrule_resource><rewrite><rule_collection>
<salesrule_resource><rewrite><collection>  
<salesrule><rewrite><resource_collection>
<salesrule><rewrite><resource_rule_collection>
<salesrule><rewrite><rule_collection>
<salesrule_mysql4><rewrite><rule_collection>
<salesrule_mysql4_rule><rewrite><collection>

Notes:
1. I know the config file is being loaded, I have checked in System->Configuration->Advanced
2. I further know the config file is being loaded because I've got the loaded classes being output to the browser, and I've tried overrideing OTHER classes and seen it output the correct class successfully
3. I know that each change I made was loaded through another rewrite that I added (and modified each time) just to verify the change was properly uploaded.
3. Yes, I was very careful about ensuring the tags were closed with the proper matching closing tag
4. Caching is disabled, so the file not being loaded is not the issue

Please help. I don't have much hair left...

1

1 Answers

11
votes

Tested with Magento 1.6.1, should apply to all versions (although naming conventions for resource models have changed in recent versions, so act accordingly).

First, two lessons which should help you understand the configuration.

Lesson 1

Magento has models. Models have resource models. Resource models do the database/datastore querying that loads a model object.

Model collections are a third type of thing. However, because model collections query the database, it was decided that a model collection would also be a model resource. That's why you use

Mage::getResourceModel('salesrule/resource_rule_collection');

to instantiate a collection. When you use

Mage::getModel('catalog/product')->getCollection();

Magento is actually calling

Mage::getResourceModel('catalog/product_collection');

behind the scenes.

Lesson 2

You can only rewrite models, helpers, and blocks. You can't directly rewrite resource models. Fortunately, we're not out of luck. In another confusing bit of abstraction, Magento's resource models are actually models themselves. When you call

Mage::getResourceModel('salesrule/resource_rule_collection');

Magento ultimately calls

Mage::getModel('salesrule_resource/resource_rule_collection')

to instantiate the resource model class. In case you're not seeing it, the resource model group salesrule is transformed into the model group salesrule_resource. The value salesrule_resource is derived by looking for a <resourceModel> node under the <salesrule> models node.

<salesrule>
    <class>Mage_SalesRule_Model</class>
    <resourceModel>salesrule_resource</resourceModel>
</salesrule>

So, what this means is you need to rewrite the model class salesrule_resource/resource_rule_collection. The following XML should work, replacing Packagename_Modulename_Model_Your_Class_Here with your class name.

<config>
    <global>
        <models>
            <salesrule_resource>
                <rewrite>     
                    <rule_collection>Packagename_Modulename_Model_Your_Class_Here</rule_collection>
                </rewrite>
            </salesrule_resource>
        </models> 
    </global>
</config>