Magento: Display/Do Something Based On Current Filter Selection

One of our clients wanted to display a unique header image based on a specific attribute selection. In our case, we are using Category as a filter. So, we made a static block for each different image that we will call in depending on which category is selected.

First, we created 3 new static blocks to hold each of the different header images (so that the client could easily updated them if they wanted to). Next, in our header.phtml template file, I put the following code:

$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
$_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId('all_packs ')->toHTML();
if (is_array($_filters)) {
    foreach ($_filters as $_filter) {
        if ($_filter->getName() == "Category") {
            switch ($_filter->getLabel()) {
                case 'Movie Packs' :
                    $_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId('movie_packs')->toHTML();
                case 'TV Packs' :
                    $_headerImage = $this->getLayout()->createBlock('cms/block')->setBlockId('tv_packs')->toHTML();
echo $_headerImage;

If no filters have been selected, or if other filters are selected but not the Category filter, then it displays the “All Packs” header image. If the Category filter is selected, depending on which option, it will display the proper image instead of the All Packs image.

I know that I could have made it a little more dynamic so that I didn’t need to use a switch statement, but we needed to get it done quickly, and it works.

This entry was posted in Magento. Bookmark the permalink.

3 Responses to Magento: Display/Do Something Based On Current Filter Selection

  1. josephtikva says:

    Great tip

    I will use this as a starting point to create a custom bestsellers block on each list page.

  2. How would I implement this code in header.phtml
    Which line/position?

    It would be a great thin if I could make it work!


  3. Jamie says:

    Thanks for the tip!

    I will be using this approach to display a brand image at the top of the product list view only after the brand attribute has been filtered.