Magento cannot override base model

First of all, I apologize for asking another question about redirecting the magento core, but I followed about 10 tutorials and read almost all of the similar questions posted here without success.

I need to redefine a bunch of basic models and classes. The code works because I already changed the kernel (on the test magento site) and it worked fine. But from time to time, a magento update is available, and if we apply the updates, all my changes will be lost. Therefore, I have to redefine the base code. I want to make my own module for entering all the necessary code, because I only need to redefine 1 or 2 functions in each class, the rest should work as Magento expected.

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

Applications / code / local / [names] /Sales/etc/config.xml

Application / code / local / [names] /Sales/Helper/Data.php (This class does nothing, it's just an empty class. I did this because I read somewhere that Magento sometimes does not recognize a module, if there is no Helper class)

Application / code / local / [names] /Sales/Model/Order/Pdf/Invoice.php

<strong> application / etc / modules / [names] _Sales.xml

The [namespace] _Sales.xml file is as follows:

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

The config.xml file is as follows:

 < ?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 moving on and overriding all the other controllers and models that I need to change.

The problem is that she is still using the original model.

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

I completely cleared the cache so it wasnโ€™t.

I used get_class to determine which model returns to 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'm sure I did one: (

+8
php magento
source share
1 answer

There are some errors that I literally found. Correct these errors: -

All file structures that you mentioned in the question in the " local " code pool have the name of the " code " folder in the " app " folder. Therefore, each file structure of your local module should be like this: " app/code/local/[namespace]/Sales/... " .

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

Secondly, the contents of the " config.xml " file are slightly incorrect. 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 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 need to add different classes here (what you did on the PHP page of the class [namespace]_Sales_Model_Order_Pdf_Invoice "class). This is because Magento automatically loads all the definitions of linked libraries (some examples of library classes have" Varien "and " Zend "). You just need to create an object of these library classes, and you can make full use of the methods.

Hope this helps.

+7
source share

All Articles