如何覆盖 Magento 社区模型?

How do I override a Magento community model?

我要覆盖的 class 在 app\code\community\Dhl\Intraship\Model\Gateway.php 中。所以我将 class 放在 app\code\local\MyCompany\Intraship\Model\Gateway.php 的本地模块中,并相应地更改了 class 名称。

现在我必须在我的 config.xml 文件中添加什么才能使其正常工作?

谢谢!

models下的节点应该与app\code\community\Dhl\Intraship\etc\config.xml中的相同:

<?xml version="1.0"?>
<config>
    <modules>
        <Dhl_Intraship>
            <version>13.07.04</version>
        </Dhl_Intraship>
    </modules>
    <!-- some more code here -->
    <global>
        <models>
            <intraship> <!-- this is the node you have to look at -->
                <class>Dhl_Intraship_Model</class>
                <resourceModel>intraship_mysql4</resourceModel>
            </intraship>
            <!-- some more code here -->
        </models>
    <!-- some more code here -->
    <global>
</config>

并且 rewrite 下的节点必须与您要在文件夹 Model 下重写的文件的路径相匹配:因此在您的情况下它只是 gateway。但是如果你要重写 app\code\community\Dhl\Intraship\Model\Path\To\Some\Model.php 节点将是 path_to_some_model

所以它看起来应该是这样的:

<?xml version="1.0"?>
<config>
    <modules>
        <mycompany_intraship>
            <version>0.1.0</version>
        </mycompany_intraship >
    </modules>
    <global>
       <models>
          <intraship>
              <rewrite>
                  <gateway>MyCompany_Intraship_Model_Gateway</gateway>
              </rewrite>
          </intraship>
       </models>
    </global>
</config>