1

I have a payment module that requires a bit of javascript on the checkout page for stores where it is enabled. The good news is that it is correctly including the link on the checkout page. Unfortunately, its also including the link on every other page and in every store.

How do you setup a javascript include to only appear in a certain section and only in stores where it is enabled?

dotancohen
1,1306 silver badges21 bronze badges
asked Feb 25, 2013 at 15:34

3 Answers 3

6

If your module has a simple enable/disabled configuration setting for each store scope (which it seems to), you can use ifconfig, which is evaluated by Mage::getStoreConfigFlag():

<?xml version="1.0" encoding="UTF-8"?>
<layout>
 <checkout_onepage_index><!-- the onepage checkout "page" -->
 <reference name="head"><!-- The HTML head block -->
 <action method="addJs" ifconfig="your/checkout_module/enabled">
 <js>your/file.js</js><!-- i.e. js/your/file.js -->
 </action>
 </reference>
 </checkout_onepage_index>
</layout>
answered Feb 25, 2013 at 16:17
1
  • this is a lot easier :) Commented Feb 25, 2013 at 16:39
1

I built an observer which adds a layout handle, if

<?xml version="1.0"?>
<config>
 <frontend>
 <events>
 <controller_action_layout_load_before>
 <observers>
 <myobserver>
 <type>singleton</type>
 <class>Namespace_MyModule_Model_Observer</class>
 <method>controllerActionLayoutLoadBefore</method>
 </myobserver>
 </observers>
 </controller_action_layout_load_before>
 </events>
 </frontend>
</config>

And the observer:

<?php
class Namespace_MyModule_Model_Observer
{
 /**
 * add update handles
 *
 * @param Varien_Event_Observer $observer
 */
 public function controllerActionLayoutLoadBefore(Varien_Event_Observer $observer)
 {
 /* @var $controller Mage_Core_Controller_Front_Action */
 $controller = $observer->getAction();
 $request = $controller->getRequest();
 /* @var $layoutUpdate Mage_Core_Model_Layout_Update */
 $layoutUpdate = $observer->getLayout()->getUpdate();
 // check whether to add the handle or not
 $layoutUpdate->addHandle('my_layout_handle');
 }
}

Then you can add the JS to the handle and decide to add it or not.

answered Feb 25, 2013 at 15:40
0

Does this javascript is embedded within a layout file and the store have an appropriate theme? If this is the case, you can configure your application according with the following example: no need to define observer, to overload, no customizations:

imagine your module come from the a layout handle mymodulelayout.xml

you should have app/design/frontend/base/default/layout/mymodulelayout.xml

move this file only on the store where you wants to enable this module:

app/design/frontend/yourwebsite/yourtheme/layout/mymodulelayout.xml

this layout will be used only when yourwebsite package is applied and your theme is applied, so for your store where you wants to load this elements in checkout.

regards

answered Feb 26, 2013 at 17:57
1
  • Its a good idea, but unfortunately doesn't work in my case. We're doing a multisite setup for several different regions and we want to be able to enable and disable modules independently of the theme. Commented Feb 26, 2013 at 22:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.