8
votes

first of all, I apologize for asking yet another "magento core override" question here, but I followed about 10 tutorials and read through almost all similar questions posted here, no success.

I have to override a bunch of core models and classes. The code works, because I already changed the core (in a test magento site) and it worked perfect. But every now and then a magento update is available and if we would apply the updates all of my changes would be lost. So I have to override the base code instead. I want to make my own module to put in all of the required code, because I only have to override 1 or 2 functions in every class, the rest should work like Magento intended.

My first attempt was to override the Mage_Sales_Model_Order_Pdf_Invoice class. Ok, so I made my module. The file structure is:

app/code/local/[namespace]/Sales/etc/config.xml

app/code/local/[namespace]/Sales/Helper/Data.php (This class doesn't do anything, it's just an empty class. I made it because I read somewhere that Magento sometimes doesn't recognize the module if there is no Helper class)

app/code/local/[namespace]/Sales/Model/Order/Pdf/Invoice.php

app/etc/modules/[namespace]_Sales.xml

The [namespace]_Sales.xml file looks this way:

<?xml version="1.0"?>
    <config>
        <modules>
            <[namespace]_Sales>
                <active>true</active>
                <codePool>local</codePool>
            </[namespace]_Sales>
        </modules>
    </config>

The config.xml file looks like this:

< ?xml version="1.0"?>
  <config>
    <modules>
        <[namespace]_Sales>
            <version>0.1.0</version>
        </[namespace]_Sales>
    </modules>
    <global>
    <helpers>
            <sales>
                <class>[namespace]_Sales_Helper</class>
            </sales>
        </helpers>
       <models>
          <sales>
              <rewrite>
                  <order_pdf_invoice>[namespace]_Sales_Model_Order_Pdf_Invoice</order_pdf_invoice>
              </rewrite>
          </sales>
       </models>
    </global>
</config>

And the Invoice.php file looks like this:

<?php

 /****I'm adding some different classes here*******************************/
 include_once Mage::getBaseDir('lib')."/myclass.php";
 include_once Mage::getBaseDir('lib')."/another_library.php";
 /********************************************************/

class [namespace]_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
    public function getPdf($invoices = array())
    {
         //my code
    }


}

I wanted to test this first before I go and override all the other controllers and models I have to change.

The problem is, it still uses the original model.

I think the module code and paths are correct, because magento finds my custom model. I checked by going into the backend and looked at System->configuration->advanced

I cleared the cache completely, so that's not it.

I used get_class to determine what model is returned in the controller: get_class(Mage::getModel('sales/order_pdf_invoice')), this returns Mage_Sales_Model_Order_Pdf_Invoice

I do not know where I made a mistake but I am sure I made one :(

1
Why whitespace here? < ?xml version="1.0"?>Dmytro Zavalkin
I copied the xml structure out of examples and changed it accordingly to my custom module. I don't think the white space is necessary, but it works this way too.itd

1 Answers

7
votes

There are some mistakes which I have found literally. Please correct those mistakes:-

All the file structures, which you have mentioned in the question within the "local" code pool, have a missing folder name "code" inside the "app" folder. So every file structure of your local module must be like: "app/code/local/[namespace]/Sales/...".

If this folder structure is wrong, then also your [namespace]_Sales module may not work as expected.

Secondly, The contents of the file "config.xml" is a bit wrong. The correct one will be:-

<?xml version="1.0"?>
<config>
  <modules>
    <[namespace]_Sales>
      <version>0.1.0</version>
    </[namespace]_Sales>
  </modules>

  <global>
    <helpers>
      <!--
      This node will be the unique identifier of your module,
      and it will be used every time your code requires referencing your own module.
      This shouldn't clash with other unique identifiers used in your Magento system.
      Normally all the characters are kept in small case for this,
      however, I haven't tried with the upper case.
      But it will be best to keep your unique identifier in small case only.
      -->
      <[namespace]sales>
        <class>[namespace]_Sales_Helper</class>
      </[namespace]sales>
    </helpers>

    <models>
      <!--
      If this is not provided, then Magento will not know your module's starting part of Model Class Names.
      -->
      <[namespace]sales>
        <class>[namespace]_Sales_Model</class>
      </[namespace]sales>
      <sales>
        <rewrite>
          <order_pdf_invoice>[namespace]_Sales_Model_Order_Pdf_Invoice</order_pdf_invoice>
        </rewrite>
      </sales>
    </models>
  </global>
</config>

Also I don't think you will require adding different classes here (which you have done in "[namespace]_Sales_Model_Order_Pdf_Invoice" class PHP page). This is because Magento loads all the definitions of related libraries automatically (some examples of library classes are of "Varien" and "Zend"). You will just need to make an object of those library classes and you will be able to use the methods fully.

Hope it helps.