6

I'm facing this problem even before CE 1.9.2

  1. In footer, I create a global custom block with page/html_wrapper type like below
<default>
    <reference name="footer">
        <block type="page/html_wrapper" name="bottom.wrapper" as="bottomWrapper">
        ...
    ...
...
  1. In some specific pages, such as Shopping Cart, Checkout Onepage,... I don't want to show this custom block, then I remove it by
<checkout_cart_index translate="label">
    <remove name="bottom.wrapper"/>
    ...
...

or by

<checkout_cart_index translate="label">
    <reference name="footer">
        <remove name="bottom.wrapper"/>
        ...
    ...
...

or by

<checkout_cart_index translate="label">
    <reference name="footer">
        <action method="unsetChild"><name>bottomWrapper</name></action>
        ...
    ...
...
  1. Prolem is, if the first visit page that generates the cache is a global page (such as home page), footer will be cached, included bottom.wrapper block, then all the above specific pages show bottom.wrapper as well. If the first visit page that generates the cache is one of above specific pages, footer will be cached without bottom.wrapper block, then all other pages won't see bottom.wrapper anymore.

Disabling BLOCK_HTML cache is a temporary solution. I don't want to hide bottom.wrapper block by css.

How to deal with it? How to make bottom.wrapper showing properly as desire?

UPDATE:

Because this question has marked duplicate, so I cannot give an answer for my own solution:

My bottom.wrapper is placed in bottom of the pages, doesn't mean it must be included in footer. Instead of including in the footer, I create a new child block of the root

<default>
    <reference name="root">
        <block type="page/html_wrapper" name="bottom.wrapper" as="bottomWrapper">
        ...
    ...
...

Then in the page template, such as 1column.phtml, 2columns-left.phtml,... I insert before footer:

<?php echo $this->getChildHtml('bottomWrapper') ?>

By now, bottomWraper is cached by its own key, not attached to the footer anymore. Other step (removing, getting,..) is just like before.

1
  • 1
    This question marked duplicate so I cannot give an answer of my own solution. Prolem is I have completely wrong approached at the first place. I should create a new child block of root instead of altering the footer. Commented Jul 14, 2015 at 6:19

2 Answers 2

4

Honestly, I didn't face such "tricky" situations before.. So cannot give you 100% guarantee on below code. I can see two solutions for this.

1. Forcefully Make bottom.wrapper Never Cached

This is the easiest solution that came into my mind. ie we need to tell Magento that, I don't want to cache this particular block. For this you can try..

<default>
    <reference name="footer">
        <block type="page/html_wrapper" name="bottom.wrapper" as="bottomWrapper">
            <action method="setCacheLifeTime"></action>
        </block>
    </reference>
 </default>

Setting cache_life_time to NULL forcefully turn off cache for this particular block.

2. Remove The Block Using an Observer

If you want to preserve the cacheing of your custom block and still need to remove your block from specific pages, you need to create a module and observe to the event controller_action_layout_generate_blocks_after and then remove the block from the layout.

File : app\code\local\Rkt\BlockRemover\etc\config.xml

<config>
    <modules>
        <Rkt_BlockRemover>
            <version>1.0.0</version>
        </Rkt_BlockRemover>
    </modules>
    <global>
        <models>
            <rkt_blockremover>
                <class>Rkt_BlockRemover_Model</class>
            </rkt_blockremover>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_generate_blocks_after>
                <observers>
                    <remove_cached_blocks>
                        <class>rkt_blockremover/observer</class>
                        <method>removeCacheBlocks</method>
                    </remove_cached_blocks>
                </observers>
            </controller_action_layout_generate_blocks_after>
        </events>
    </frontend>
</config>

File : app\code\local\Rkt\BlockRemover\Model\Observer.php

<?php
class Rkt_BlockRemover_Model_Observer
{
    public function removeCacheBlocks(Varien_Event_Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();
        $action = $observer->getEvent()->getAction();

        //make sure block is removing from specific pages that we required
        if ($action->getFullActionName() == 'checkout_cart_index') {
            $layout->unsetBlock('bottom.wrapper');
        }

        return $this;
    }
}

Code checks whether the page requested is checkout_cart_index page and if yes, it will simply remove bottom.wrapper block from the layout.


Note 1 : I didn't test this code. But I am sure it is close what you need.

Note 2 : In either, FLUSHING CACHE before testing is an essential step.

1
  • This question marked duplicate so I cannot give an answer of my own solution. Prolem is I have completely wrong approached at the first place. I should create a new child block of root instead of altering the footer. Thank you for your answer, it provides some good knowledge. Commented Jul 14, 2015 at 6:19
1

It happens because footer block is cached based on some cache keys.

To have different footer on different page, you need to modify the cache key info for footer block.

Check this out:

// Mage_Page_Block_Html_Footer

/**
 * Get cache key informative items
 *
 * @return array
 */
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()
    );
}

What modification to do in here depends on your need. The easiest solution that came across my mind is to use controller's full action name:

public function getCacheKeyInfo()
{
    $info   = parent::getCacheKeyInfo();
    $action = Mage::app()->getFrontController()->getAction();
    if ($action) {
        $info[] = $action->getFullActionName();
    }
    return $info;
}

PS: It's untested!

1
  • This question marked duplicate so I cannot give an answer of my own solution. Prolem is I have completely wrong approached at the first place. I should create a new child block of root instead of alter the footer. Thank you for your answer. It's useful for me someday. Commented Jul 14, 2015 at 6:20

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