php – magento无法覆盖核心模型

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – magento无法覆盖核心模型脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我为在这里提出另一个“magento核心覆盖”问题而道歉,但我遵循了大约10个教程并阅读了几乎所有类似的问题,但没有成功.

我必须覆盖一堆核心模型和类.代码有效,因为我已经改变了核心(在测试magento站点中)并且它工作得很完美.但是偶尔会有一个magento更新,如果我们应用更新,我的所有更改都将丢失.
所以我必须改写基本代码.
我想创建自己的模块来输入所有必需的代码,因为我只需要在每个类中覆盖1或2个函数,其余的应该像Magento一样工作.

我的第一次尝试是覆盖Mage_Sales_Model_Order_PDF_Invoice类.
好的,所以我制作了我的模块.文件结构是:

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

应用程序/代码/本地/ [命名空间] /Sales/HelPEr/Data.PHP
(这个类没有做任何事情,它只是一个空类.我之所以这样做是因为我读到某个地方,如果没有Helper类,Magento有时候无法识别模块)

应用程序/代码/本地/ [命名空间] /Sales/Model/Order/Pdf/Invoice.PHP

应用程序的/ etc /模块/ [命名空间] _Sales.xML

[namespace] _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找到了我的自定义模型.我通过进入后端检查并查看了System-> configuration-> advanced

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

我使用get_class来确定控制器中返回的模型:get_class(Mage :: getModel(‘sales / order_pdf_invoice’)),这将返回Mage_Sales_Model_Order_Pdf_Invoice

我不知道我犯了什么错,但我确信我做了一个:(

我从字面上发现了一些错误.请纠正这些错误: –

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

如果此文件夹结构错误,那么您的[namespace] _Sales模块也可能无法按预期工作.

其次,文件“config.xml”的内容有点不对.正确的将是: –

<?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”).您只需要创建这些库类的对象,您就可以完全使用这些方法.

希望能帮助到你.

脚本宝典总结

以上是脚本宝典为你收集整理的php – magento无法覆盖核心模型全部内容,希望文章能够帮你解决php – magento无法覆盖核心模型所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。