Magento – Different Footer per Page

footermagento-1.7template

I have a request from client where he wants to put google analytics code on different pages in Magento under footer.

I see that footer is generic, so is there a way to place a different code under footers for pages / create different footers?

Best Answer

This is a common problem: Adding something in the footer but only on some pages. Everything works perfectly as Alex suggested but only when the block cache is disabled. You can add a block to the footer on any page using layouts. For example adding something only on the home page can be done like this:

<cms_index_index>
    <reference name="footer">
        <block type="some/block_type" template="some/template.phtml" name="some_name" as="some_name" />
    </reference>
</cms_index_index>

The problem occurs when you activate the block cache. The footer cache is general and does not take into account the page it's in.

Here is my general solution for this kind of problem. I override the footer block (Mage_Page_Block_Html_Footer) and change the cache key info. Let's say that my new footer block is Easylife_Page_Block_Html_Footer, the method getCacheKeyInfo() would look like this:

public function getCacheKeyInfo()
{
    $info = parent::getCacheKeyInfo();
    $info[] = $this->getHasSomeBlock();
    /* this needs to be added for each block that 
       should appear only on specific pages, just 
       choose a different key for each block
    */
    return $info;
}

Now in the layout section where I added the block to the footer I add a new line:

<cms_index_index>
    <reference name="footer">
        <action method="setHasSomeBlock"><param>1</param></action> 
        <block type="some/block_type" template="some/template.phtml" name="some_name" as="some_name" />
    </reference>
</cms_index_index>

This solved all my issues for this kind of problem.