简体   繁体   English

自定义Magento扩展启用和禁用不工作

[英]Custom Magento Extension Enable and Disable not working

I am simply trying to build an extension that if it is enabled it will override the original cart/shipping.phtml file with my own template file.. 我只是想构建一个扩展,如果它被启用它将覆盖原始的cart / shipping.phtml文件与我自己的模板文件..

When I click on Enable it does not enable the extension. 当我单击“启用”时,它不会启用扩展。 I know the extension actually works b/c if I manually change my layout block theme it works. 我知道如果手动更改我的布局块主题,扩展实际上是b / c工作。 However, I don't want to do that. 但是,我不想这样做。 Can you please have a look at my code and let me know what I am doing wrong? 你能看看我的代码,让我知道我做错了什么吗? I am assuming it has something to do with my block file not being right. 我假设它与我的块文件不正确有关。 PS if you see what is wrong and how to fix it could you also tell me how to set a CSS file for the extension as well if it is enabled? PS如果你看到有什么问题以及如何修复它你也可以告诉我如何为扩展设置一个CSS文件,如果它已启用?

Here are all of my files :) 这是我的所有文件:)

etc/config.xml 等/ 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>

etc/system.xml 等/的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>

Helper/Data.php 助手/ Data.php

<?php
class Module_Name_Helper_Data extends Mage_Core_Helper_Abstract
{   

}

Block/Cart/Shipping.php 块/车/ 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;
}

}

For checking boolean config data it's more appropriate to use Mage::getStoreConfigFlag() [link] . 为了检查布尔配置数据,使用Mage::getStoreConfigFlag() [link]更合适。 In this case, there's a hook to do just this purely in layout XML without the need to do a block class rewrite. 在这种情况下,有一个钩子可以完全在布局XML中执行此操作, 无需执行块类重写。

Configure a custom layout update file for your module, and in that file simply do the following: 为您的模块配置自定义布局更新文件,并在该文件中执行以下操作:

<?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>

As long as your module is also configured with <depends /> on the Mage_Checkout this layout XML update will be merged in after the core instruction, thus overriding the core template. 只要您的模块在Mage_Checkout上配置了<depends /> ,此布局XML更新将在核心指令之后合并,从而覆盖核心模板。

The only reason to take the approach which you have taken is to thoroughly force that the template will be set to your module's template just prior to rendering - thereby overriding any potential conflicting layout XML instruction - assuming that there is no cache hit, a behavior which is ... debatable. 采取这种方法的唯一理由是彻底强制在渲染之前将模板设置到模块的模板 - 从而覆盖任何潜在的冲突布局XML指令 - 假设没有缓存命中,行为是......有争议的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM