2012-01-22 121 views
8

首先,我为此要求另一个“magento核心覆盖”问题表示歉意,但是我跟随了大约10个教程,并通读了几乎所有类似的问题,但都没有成功。我不得不重写一堆核心模型和类。代码的作品,因为我已经改变了核心(在一个测试magento网站),它的工作完美。但是每隔一段时间都会有一个magento更新,如果我们应用更新,则所有更改都将丢失。 所以我必须改写基本代码。 我想让自己的模块放入所有必需的代码,因为我只需要在每个类中重写1或2个函数,其余的应该像Magento预期的那样工作。magento无法覆盖核心模型

我的第一次尝试是重写Mage_Sales_Model_Order_Pdf_Invoice类。 好吧,所以我做了我的模块。文件结构是:

应用程序/代码/本地/ [命名空间] /Sales/etc/config.xml

应用程序/代码/本地/ [命名空间] /Sales/Helper/Data.php (这个类并没有做任何事情,它只是一个空的类。我做到了,因为我读的地方,Magento的,有时如果没有辅助类,不能识别模块)

应用程序/代码/本地/[namespace]/Sales/Model/Order/Pdf/Invoice.php

应用的/ etc /模块/ [命名空间] _Sales.xml

的[名称空间] _Sales.xml文件看起来是这样的:

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

config.xml文件看起来像这样:

< ?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> 

而且Invoice.php文件看起来像这样:

<?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 
    } 


} 

我想先测试一下,然后覆盖所有其他需要更改的控制器和模型。

问题是,它仍然使用原始模型。

我认为模块代码和路径是正确的,因为magento找到我的自定义模型。我检查了进入后端,看着系统 - >配置 - >高级

我完全清除缓存,所以不是这样。

我用get_class确定返回什么型号的控制器:get_class(法师:: getModel( '销售/ order_pdf_invoice')),这将返回Mage_Sales_Model_Order_Pdf_Invoice

我不知道我在哪里做一个错误,但我相信我做了一个:(

+1

为什么在这里留空? '<?xml version =“1.0”?>' – Zyava

+0

我将xml结构复制出示例,并相应地将其更改为我的自定义模块。我不认为白色空间是必要的,但它也适用于这种方式。 – itd

回答

7

有一些错误,我已经发现字面上。请更正这些错误: -

local”代码池中的问题中提到的所有文件结构在“app”文件夹中缺少文件夹名称“code”。因此,本地模块的每个文件结构必须如下所示:app/code/local/[namespace]/Sales/...

如果此文件夹结构错误,那么您的[namespace]_Sales模块可能无法正常工作。

其次,文件“​​3210”的内容有点不对。正确的是: -

<?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> 

此外,我不认为你需要在这里添加不同类(你在“[namespace]_Sales_Model_Order_Pdf_Invoice”级PHP页面已完成)。这是因为Magento会自动加载相关库的所有定义(库类的一些示例为“Varien”和“Zend”)。您只需要创建这些库类的对象,就可以完全使用这些方法。

希望它有帮助。

+1

是的,我写了这个问题有点快和错误输入。我的所有文件当然都在app/code/local文件夹中,而不是app/local。 虽然我会看看你的其他建议,谢谢你的帮助:) – itd

+1

@itd - 最受欢迎 –

+0

谢谢,它的工作。正如你所说,config.xml文件有错误。 – itd