i want to check the currently applied layered navigation filters using an observer. When specific filter values are selected an additional layout handle should be added which adds a static block to the content.
For this i created a module which registers on the event controller_action_layout_load_before. In my observer method i call
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
But somehow the filter array is always empty, no matter what i tried.
I thought allready of dispatching my own event and passing the filters as an argument, but that might be the dirty way.
Anyone has an idea why i can't access that data? I can access it in the template files.
Thanks a lot for your help!
config.xml:
<config>
<modules>
<My_CustomModule>
<version>0.0.1</version>
</My_CustomModule>
</modules>
<global>
<models>
<my_custommodule>
<class>My_CustomModule_Model</class>
</my_custommodule>
</models>
</global>
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<my_custommodule_observer>
<type>singleton</type>
<class>my_custommodule/observer</class>
<method>checkActiveFilters</method>
</my_custommodule_observer>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
</config>
Observer.php:
class My_CustomModule_Model_Observer
{
public function __construct()
{
}
public function checkActiveFilters($observer)
{
$layout = $observer->getEvent()->getLayout();
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach($appliedFilters as $filter) {
Mage::log('Yay! appliedFilters is not empty!');
$layout->getUpdate()->addHandle('FILTER_'.$filter->getFilter()->getRequestVar());
}
}
}