0

I'm struggling to figure out the correct XML structure for extending magento classes which are deeper in the folder hierarchy than say Core/Store.php

For instance, how can I extend the following:

app/code/core/Mage/Checkout/Model/Cart/Api.php
app/code/core/Mage/Checkout/Model/Api/Resource/Customer.php

I've tried something along the lines of this:

<?xml version="1.0"?>
<config>
    <modules>
        <company_Checkout>
            <version>0.0.1</version>
        </company_Checkout>
    </modules>
    <global>
        <models>
            <checkout_api>
                <rewrite>
                    <resources_customer>company_Checkout_Model_Api_Resource_Customer</resources_customer>
                </rewrite>
            </checkout_api>
        </models>
    </global>
</config>

But haven't really gotten anywhere. Any ideas what the XML structure should be? And what would be a good resource to check out in order to better understand the extension logic in magento? Is what I'm trying to do even possible?

EDIT

I got the Customer.php rewrite working with the following config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Checkout>
            <version>0.0.1</version>
        </Company_Checkout>
    </modules>
    <global>
        <models>
            <checkout>
                <rewrite>
                    <cart_api>Company_Checkout_Model_Cart_Api</cart_api>
                    <api_resource_customer>Company_Checkout_Model_Api_Resource_Customer</api_resource_customer>
                </rewrite>
            </checkout>
        </models>
    </global>
</config>

However, the Api.php rewrite does not seem to work. I'm sure I'm missing something obvious but can't see it. Below is thew new Api.php. The folder structure for it is app/code/local/Company/Checkout/Model/Cart/Api.php

<?php

class Company_Checkout_Model_Cart_Api extends Mage_Checkout_Model_Api_Resource {
    /**
     * Create new quote for shopping cart
     *
     * @param int|string $store
     * @return int
     */
    public function create($store = null)
    {
        $storeId = $this->_getStoreId($store);

        Mage::log("**REWRITE**");

        try {
            /*@var $quote Mage_Sales_Model_Quote*/
            $quote = Mage::getModel('sales/quote');
            $quote->setStoreId($storeId)
                    ->setIsActive(false)
                    ->setIsMultiShipping(false)
                    ->save();
        } catch (Mage_Core_Exception $e) {
            $this->_fault('create_quote_fault', $e->getMessage());
        }
        return (int) $quote->getId();
    }
}

EDIT Using magento-ce 1.9.0.1

3
  • "company" is it uppercase or lowercase "c"? Commented Nov 6, 2014 at 14:39
  • In real world scenario it is an uppercase letter. I just replaced the company name with a general placeholder. Does the case really matter?
    – Sand
    Commented Nov 6, 2014 at 14:40
  • Should alway be uppercase see inchoo.net/magento/… Commented Nov 6, 2014 at 14:44

2 Answers 2

4

If you want to rewrite something that is not a resource model you simply translate the class name into xml nodes like this:

 |- skip this
 |
Mage_Checkout_Model_Cart_Api
          |     |      |
    |-----|-----|      |
<models>  |            |
   <checkout>      |---|
       <rewrite>   |
           <cart_api>Your_Class_Name_Here</cart_api>
       </rewrite>
    </checkout>
</models>

So if you are rewriting a model the top tag is <models>. For blocks is <blocks> and so on.

Then you add the module name as a tag <checkout> in this case.
Then <rewrite> tag. This is fixed.
Then the rest of the class name with underscores and in lowercase. cart_api in this case.

For resource models is a bit different.

Mage_Catalog_Model_Resource_Product translates to

<models>
    <catalog_resource>
       <rewrite>
            <product>Your_Class_Name_Here</product>
       </rewrite>
    </checkout_resource>
</models>

The difference is that the tag below the models is not the module name <catalog>. It is <{module}_resource> and the tag inside <rewrite> your put the rest of the class name after Model_Resource in lowercase. product in my example.

But keep in mind that not all the class names that have Resource in them are resource models. Only those that have Resource imediately after Model.
In your example Mage_Checkout_Model_Api_Resource_Customer is not a resource model so you can extend it by following the first example.

<models>
   <checkout>
       <rewrite>
           <api_resource_customer>Your_Class_Name_Here</api_resource_customer>
       </rewrite>
    </checkout>
</models>
3
  • Thank you for the explanation. I did get the Customer.php rewrite working with the following: <api_resource_customer>Company_Checkout_Model_Api_Resource_Customer</api_resource_customer> However the Api.php rewrite does not come in to effect with this <cart_api>Company_Checkout_Model_Cart_Api</cart_api>. Both are inside the same rewrite tag, is this legal?
    – Sand
    Commented Nov 6, 2014 at 15:12
  • Strange. It should work like that. And yes...you can put both rewrites inside the same <rewrite> tag.
    – Marius
    Commented Nov 6, 2014 at 15:16
  • Added some updates to my original question, in the hopes it might help pinpoint the problem with the Api.php.
    – Sand
    Commented Nov 6, 2014 at 20:32
0

Try this

 <global>
        <models>
            <checkout>
                <rewrite>
        <cart_api>company_Checkout_Model_Cart_Api</cart_api>
                    <api_resource_customer>company_Checkout_Model_Api_Resource_Customer</api_resource_customer>
                </rewrite>
            </checkout_api>
        </models>
    </global>

StuctureL:

<global>
        <models>
            <modelprefix> <!-- model prefix model of checkout is Mage_Checkout -->
                <rewrite>
            <!-- Cart/Api.php rest class of cart_api  -->
            <cart_api>company_Checkout_Model_Cart_Api</cart_api>
                    <api_resource_customer>company_Checkout_Model_Api_Resource_Customer</api_resource_customer>
                </rewrite>
            </modelprefix>
        </models>
    </global>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.