1

我只是想构建一个扩展,如果启用它,它将用我自己的模板文件覆盖原始的 cart/shipping.phtml 文件。

当我单击启用时,它不会启用扩展。如果我手动更改我的布局块主题,我知道扩展实际上可以工作 b/c。但是,我不想那样做。你能看看我的代码,让我知道我做错了什么吗?我假设这与我的块文件不正确有关。PS,如果您发现出了什么问题以及如何解决它,您是否还可以告诉我如何为扩展设置 CSS 文件以及是否已启用它?

这是我所有的文件:)

等/config.xml

<?xml version="1.0"?>
<config>    
<modules>
<Module_Name><version>1.0.0</version></Module_Name>
</modules>

<global>
        <blocks>
             <modulename>
                  <class>Module_Name_Block</class>
             </modulename>
        </blocks>

<helpers>
     <modulename>
      <class>Module_Name_Helper</class>
     </modulename>
</helpers>      
</global>


<modulename>
<settings>
<enable>1</enable>
</settings>
</modulename>

<frontend>
<layout>
    <updates>
        <modulename>
            <file><!-- shipping.xml --></file>
        </modulename>
    </updates>
</layout>
<routers>
    <modulename>
        <use>standard</use>
        <args>
            <module>Module_Name</module>
            <frontName>modulename</frontName>
        </args>
    </modulename>
</routers>  
</frontend>


<adminhtml>
<acl>
    <resources>
        <admin>
            <children>
                <system>
                    <children>
                        <config>
                            <children>
                                <modulename>
                                    <title>Shipping Extension</title>
                                </modulename>
                            </children>
                        </config>
                    </children>
                </system>
            </children>
        </admin>
    </resources>
</acl>
</adminhtml>

</config>

等/system.xml

<?xml version="1.0"?>
<config>
<tabs>
<module translate="label">
    <label>Custom Extensions</label>
    <sort_order>100</sort_order>
</module>
</tabs>

<sections>  
        <modulename translate="label">
    <label>Shipping</label>
    <tab>module</tab>
    <frontend_type>text</frontend_type>
    <sort_order>1000</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>


        <groups>            

            <settings translate="label">
            <label>Settings</label>
            <frontend_type>text</frontend_type>
            <sort_order>1</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>

                <fields>
                    <enable translate="label">
                    <label>Enable</label>
                    <comment>
                    <![CDATA[Enable or Disable this extension.]]>
                    </comment>
                    <frontend_type>select</frontend_type>
                    <source_model>adminhtml/system_config_source_yesno</source_model>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>                    
                    </enable>           
                </fields>

            </settings>
        </groups>
    </modulename >
</sections>     
</config>

助手/Data.php

<?php
class Module_Name_Helper_Data extends Mage_Core_Helper_Abstract
{   

}

块/购物车/Shipping.php

 <?php

 class Module_Name_Block_Cart_Shipping extends Mage_Checkout_Block_Cart_Shipping
{
protected function _beforeToHtml()
{
if(Mage::getStoreConfig('modulename/settings/enable'))

$this->setTemplate('module/name/shipping.phtml');

return $this;
}

}
4

1 回答 1

4

Mage::getStoreConfigFlag()为了检查布尔配置数据,使用[link]更合适。在这种情况下,有一个钩子可以纯粹在布局 XML 中执行此操作,而无需重写块类。

为您的模块配置自定义布局更新文件,并在该文件中简单地执行以下操作:

<?xml version="1.0"?>
<layout>
    <checkout_cart_index>
        <action method="setTemplate" block="checkout.cart.shipping" ifconfig="dropdownshipping/settings/enable">
            <template>beckin/dropdownshipping/drop_down_shipping.phtml</template>
        </action>
        <action method="addCss" block="head" ifconfig="dropdownshipping/settings/enable">
            <template>css/beckin/dropdownshipping.css</template>
        </action>
    </checkout_cart_index>
</layout>

只要您的模块还配置<depends />Mage_Checkout此布局,XML 更新就会合并到核心指令之后,从而覆盖核心模板。

采用您所采取的方法的唯一原因是在渲染之前彻底强制将模板设置为模块的模板 - 从而覆盖任何潜在的冲突布局 XML 指令 - 假设没有缓存命中,这种行为是……有争议的。

于 2012-12-16T15:58:42.110 回答