if you just want to add a js file for configurable product, you really dont want to do this. Create a local.xml
inside your theme layout location and put this code inside
File : app/design/frontend/<package>/<theme>/layout/local.xml
<PRODUCT_TYPE_configurable>
<reference name="head">
<action method="addItem">
<type>skin_js</type>
<name>js/test.js</name>
</action>
</reference>
</PRODUCT_TYPE_configurable>
and put your js file in skin location
File : skin/frontend/<package>/<theme>/js/test.js
PRODUCT_TYPE_configurable
is the unique handler that is render by magento, for configurable products only. So you are lucky.. For each product type, there are unique handlers. Magento use this handler in order to put product type specific layout. Take a look on catalog.xml
.
EDIT
In that case, you can go for an alternate solution. You can add a template file through this handler and do your insertion of js file there. This would be more reliable method than go for an event observation.
So these are the steps
1. Declare a template for configuration product
<PRODUCT_TYPE_configurable>
<reference name="content">
<block type="core/template" name="configurable.js.block" template="configurable/js.phtml" />
</reference>
</PRODUCT_TYPE_configurable>
2. define a helper for your module and use it to provide js files, according to the need
class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract {
public function getJsForProduct() {
$product = Mage::registry('current_prouduct');
$attribute = $product->getData('attribute_code');
if($attribute == "something"){
//returns js file here; file location is enough
}
}
}
You need to define your helper class in config. For more RefeR this
3.Define template file
File : app/design/frontend/<package>/<theme>/template/configurable/js.phtml
<
<div>
<?php $js = Mage::helper('your_helper_alias')->getJsForProduct(); ?>
<script src="<?php echo $js ?>" ></script>
</div>
You can simply get attribute in template file itself. However I showed you a reliable method that you can go with. I didn't test this. But you can make it work.
Best Answer
I have found it by myself, Magento does not care if the event action
return true/false
, it will process thepostdispatch
observer in anyway.