2

我正在使用 Magento,我想重写 Mage_Catalog_Block_Product_Price,但社区 codePool 模块已经重写了该块。我知道在我的 xml 中,我需要

<config>
    <global>
        <blocks>
            <WHAT_GOES_HERE?>
                <rewrite>
                    <product_price>Display_Price_Block_Product_Price</product_price>
                </rewrite>
            </WHAT_GOES_HERE?>
        </blocks>

但是我不确定应该在类组节点中放置什么(我已将其标记为 WHAT_GOES_HERE?)社区模块没有声明 <[classgroup]>,所以我不确定应该在我的节点中放置什么失踪。

提前感谢您的帮助。

4

1 回答 1

4

All of the configuration XML is merged into a single DOM structure in a way that means colliding xpaths will have their text values overridden. An example:

This community module is currently rewriting the resolved classname via configuration:

<config>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product_price>Community_Module_Block_Example_Price</product_price>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

The framework invokes block classes using Mage_Core_Model_Layout::createBlock(), with the argument in this case being catalog/product_price (note how those two strings map to the configuration). The community module is changing the classname which is calculated from this information, and pointing it to the classname specified i.e. Community_Module_Block_Example_Price. That classname is then fed to PHP new to invoke a class.

What you need to do is copy this exact configuration to your module config - specifying your block classname - and (important!) make sure that the following occur:

  1. your configuration is loaded after the community module's configuration, and
  2. that your class extends from the community module's class.

How do you make sure that your module configuration is loaded after the community module? Just open the community module's declaration file in app/etc/modules, note the node name under the <modules> node (in the example above, would almost certainly be Community_Module), and then in your module declaration file (also in app/etc/modules), add this to your module declaration:

<depends>
    <Community_Module /> <!-- e.g. -->
</depends>

HTH

于 2012-04-27T16:11:50.020 回答