Question

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

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. 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. P.S. 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?

Here are all of my files :)

etc/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

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

<?php
class Module_Name_Helper_Data extends Mage_Core_Helper_Abstract
{   

}

Block/Cart/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;
}

}
Was it helpful?

Solution

For checking boolean config data it's more appropriate to use 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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top