2

I wrote the following code

    <?php $appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
    $_headerImage1 = $this->getLayout()->createBlock('cms/block')->setBlockId('movie')->toHTML();
    $_headerImage2 = $this->getLayout()->createBlock('cms/block')->setBlockId('music')->toHTML();
    foreach ($appliedFilters as $item) {
    if ($item->getFilter()->getRequestVar() == 'my_attribute1') {
    echo $_headerImage1;
    }

    else

    if ($item->getFilter()->getRequestVar() == 'my_attribute2') {
     echo $_headerImage2;
    }
    }
    ?>

It worked fine. But, if the first and second attributes are selected at the same time - on page appears two static blocks.

I need - when you select the second attribute - displaying only second static block.

Perhaps the use of a specific space for static block or resctict first static block if selected second attribute.

In any case , I do not know how to do it.

Please help!

UPDATED

I found another case.

                <?php $_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
                $_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId('static_block_0')->toHTML();
                if (is_array($_filters)) {
                foreach ($_filters as $_filter) {
                if ($_filter->getRequestVar() == 'my_attribute_0') {
                    switch ($_filter->getRequestVar()) {
                case 'my_attribute_1' :
                    $_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId('static_block_1')->toHTML();
                    break;
                case 'my_attribute_2' :
                    $_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId('static_block_2')->toHTML();
                    break;
            }
        }
    }
}
echo $_headerImage;
?>

But it not worked.

3 Answers 3

1

How about this? -> https://stackoverflow.com/questions/4668656/magento-get-a-static-block-as-html-in-a-phtml-file

Now you create the blocks in current layout and I'm not sure how they would act. Maybe you should try something like this (in your modules app/design/frontend/default/base/your_company/your_module.xml - or whatever xml file name you defined in config.xml):

<layout>
    <default> <!-- or your store / theme handle -->
        <reference name="content">
            <block type="cms/block" name="movie" as="movie">
                <action method="setBlockId">
                    <block_id>movie</block_id>
                </action>
            </block>
            <block type="cms/block" name="music" as="music">
                <action method="setBlockId">
                    <block_id>music</block_id>
                </action>
            </block>
        </reference>
    </default>
</layout>

And then in your code:

$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach ($appliedFilters as $item) {
    if ($item->getFilter()->getRequestVar() == 'my_attribute1') {
        echo $this->getChildHtml('movie');
    }
    else if ($item->getFilter()->getRequestVar() == 'my_attribute2') {
        echo $this->getChildHtml('music');
    }
}
0

You can just take the last (or a random) filtered attribute in $_filters array then create a block using a block id pattern such as "static_block_my_attribute_x"

 //GET LAST FILTER ITEM IN FILTERS
 $_filter = end($_filters);

 //CREATE BLOCK ID PATTERN
 $blockid = "static_block_".$_filter->getRequestVar();

 //GET HTML BLOCK WITH THAT BLOCK ID (IF EXISTS)    
 $_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId($blockid)->toHTML();

Let me know if it works, it's not tested

4
  • At the end of the array is always an attribute whose position below in the magento attributes settings. And with each filter selection - output doubles. Tested with <?php $_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters(); if (is_array($_filters)) { foreach ($_filters as $item) { if ($item = end($_filters)) { echo '<a>'.$item->getFilter()->getRequestVar().'</a>'; } } } ?> Commented Oct 30, 2014 at 4:41
  • Need a precise definition of attribute is applied, but how to do it right? I'm confused and spent a lot of time. Who can help? Commented Oct 30, 2014 at 4:44
  • Oh so you want to show the last clicked filter? Then you could use cookies ;) Commented Oct 30, 2014 at 9:57
  • I can not imagine, how it might look? Commented Oct 30, 2014 at 12:28
0

I don't usually enjoy promoting extensions on SE (a lot of them are bogus and out of date), but I've used this one several times for this purpose alone. Keeps my SEO team happy as well, which is important.

https://ecommerce-team.com/seo-ajax-layered-navigation.html (Costs $100)

Basically, you create a static block and assign it to a filtered URL. It's not as fancy schmancy as most of your AheadWorks/Amasty/MageStores, but trust me, it does the job well. It'll save you fannying about.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.