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?
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?
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.
public function getCacheKeyInfo() { return array( 'PAGE_FOOTER', Mage::app()->getStore()->getId(), (int)Mage::app()->getStore()->isCurrentlySecure(), Mage::getDesign()->getPackageName(), Mage::getDesign()->getTheme('template'), Mage::getSingleton('customer/session')->isLoggedIn() ); }
<template>page/html/different_footer.phtml</template>
to the checkout layout file. It works with cache disabled but when I enable it I get the regular default footer. Since I'm not adding a block to the existing footer but instead using a different template alltogether, can I use this cache key info?
You can add / removed the concerning block per page in your layout XML files just like any other block.
There are handles for the different page types, for different categories, for different products and so on.
@Marius' answer will work, but it is far too complex. Rewriting core Magento classes should be avoided at all costs, especially when cleaner solutions exist.
The only thing that needs to be done is the following: add a cacheKey
to the layout XML reference that is having its template changed. For example, this is how to change the footer on the onepage checkout--and only the onepage checkout, without concern that the cache will use the original, site-wide footer:
<checkout_onepage_index>
<reference name="footer">
<action method="setTemplate">
<template>linusmoneymaker/page/html/checkout-footer.phtml</template>
</action>
<action method="setCacheKey">
<key>your-own-unique-cache-key-for-linus-moneymaker</key>
</action>
</reference>
</checkout_onepage_index>
Notice that the only addition is calling the setCacheKey
method. No unnecessary Magento rewrite.
I've explained why this works in greater detail in my original answer on StackOverflow.