2012-01-22 120 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