php - magento 无法覆盖核心模型

标签 php magento

首先,我为在这里提出另一个“magento 核心覆盖”问题而道歉,但我遵循了大约 10 个教程并通读了此处发布的几乎所有类似问题,但没有成功。

我必须重写一堆核心模型和类。代码有效,因为我已经更改了核心(在测试 magento 站点中)并且它运行完美。但是时不时会有 magento 更新可用,如果我们应用更新,我的所有更改都会丢失。 所以我必须改写基本代码。 我想制作我自己的模块来放入所有必需的代码,因为我只需要在每个类中重写 1 或 2 个函数,其余的应该像 Magento 预期的那样工作。

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

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

app/code/local/[命名空间]/Sales/Helper/Data.php (这个类什么都不做,它只是一个空类。我之所以这样做是因为我在某处读到如果没有 Helper 类,Magento 有时无法识别模块)

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

app/etc/modules/[命名空间]_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
    }


}

我想先测试一下,然后再覆盖我必须更改的所有其他 Controller 和模型。

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

我认为模块代码和路径是正确的,因为 magento 找到了我的自定义模型。我通过进入后端进行检查并查看了System->configuration->advanced

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

我使用 get_class 来确定在 Controller 中返回什么模型:get_class(Mage::getModel('sales/order_pdf_invoice')),这返回 Mage_Sales_Model_Order_Pdf_Invoice/p>

我不知道我在哪里犯了错误,但我确定我犯了一个错误:(

最佳答案

有些错误是我从字面上发现的。请更正这些错误:-

您在“local”代码池中的问题中提到的所有文件结构,在“”中都缺少文件夹名称“code” >app”文件夹。所以你的本地模块的每个文件结构都必须像: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 无法覆盖核心模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8961737/

相关文章:

mysql - 通过在彼此内嵌套子查询来解决 MySQL 中的 61 个表 JOIN 限制

php - Magento如何从一家商店的价格中去除小数

php - 具有鼠标悬停和移出功能的按钮

php - 在 Yii 的另一个 Controller 中调用函数

php - 从 Doctrine2 获取值数组的正确方法

php - 在mysql中整数8.5存储为8.0

magento - 如何在magento中为 Controller 方法运行cronjob

php - Laravel 5.4 中的快速身份验证和配置

magento - 有没有一种时髦的方法来计算 Magento 商店中的所有产品? getProduct() 想要一个 id

Magento,进入 SSL 页面后不要链接到 SSL