3

I've a module in magento which is creating a new view of the product page, by calling a new options.phtml file.

the problem I have is that if I call, via ajax, this modified product page view, with the custom options.phtml file, (url is -> example.com/ajax/index/options/product_id/16490/) magento saves a cached version of it. Later if I go and visit the product page (normal magento product page NOT AJAX) the page loads the cached version of the custom options.phtml output and not the default one... and that's a problem because there's no photos product info etc. only the sizes and buy button plus header & footer

I've looked around and I think it might be the XML file, that I have in /app/design/frontend/default/MyTheme/layout/ajax.xml

here's the content of the file:

<?xml version="1.0"?>
<layout version="0.1.0">
    <catalog_category_default>
        <reference name='product_list'>
            <action method='setTemplate'><template>ajax/catalog/product/view/list.phtml</template></action>
        </reference>
    </catalog_category_default>

    <catalog_category_layered>
        <reference name='product_list'>
            <action method='setTemplate'><template>ajax/catalog/product/view/list.phtml</template></action>
        </reference>
    </catalog_category_layered>

    <ajax_index_options>
        <reference name="root">
            <action method="setTemplate"><template>page/empty.phtml</template></action>
        </reference>

        <reference name="head">
            <action method="addJs"><script>varien/product.js</script></action>
            <action method="addJs"><script>varien/configurable.js</script></action>
        </reference>

        <reference name="content">
            <block type="catalog/product_view" name="product.info" template="ajax/catalog/product/options.phtml">

                <block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="ajax/catalog/product/view/addtocart.phtml"/>

                <block type="catalog/product_view" name="product.info.options.wrapper" as="product_options_wrapper" template="catalog/product/view/options/wrapper.phtml" translate="label">
                    <label>Info Column Options Wrapper</label>
                    <block type="core/template" name="options_js" template="catalog/product/view/options/js.phtml"/>

                    <block type="catalog/product_view_options" name="product.info.options" as="product_options" template="catalog/product/view/options.phtml">
                        <action method="addOptionRenderer">
                            <type>text</type>
                            <block>catalog/product_view_options_type_text</block>
                            <template>catalog/product/view/options/type/text.phtml</template>
                        </action>

                        <action method="addOptionRenderer">
                            <type>file</type>
                            <block>catalog/product_view_options_type_file</block>
                            <template>catalog/product/view/options/type/file.phtml</template>
                        </action>

                        <action method="addOptionRenderer">
                            <type>select</type>
                            <block>catalog/product_view_options_type_select</block>
                            <template>catalog/product/view/options/type/select.phtml</template>
                        </action>

                        <action method="addOptionRenderer">
                            <type>date</type>
                            <block>catalog/product_view_options_type_date</block>
                            <template>catalog/product/view/options/type/date.phtml</template>
                        </action>
                    </block>
                </block>

                <block type="catalog/product_view" name="product.info.options.wrapper.bottom" as="product_options_wrapper_bottom" template="catalog/product/view/options/wrapper/bottom.phtml" translate="label">
                    <label>Bottom Block Options Wrapper</label>
                    <action method="insert"><block>product.tierprices</block></action>
                    <block type="catalog/product_view" name="product.clone_prices" as="prices" template="catalog/product/view/price_clone.phtml"/>
                    <action method="append"><block>product.info.addtocart</block></action>
                    <action method="append"><block>product.info.addto</block></action>
                </block>

                <block type="core/template_facade" name="product.info.container1" as="container1">
                    <action method="setDataByKey"><key>alias_in_layout</key><value>container1</value></action>
                    <action method="setDataByKeyFromRegistry"><key>options_container</key><key_in_registry>product</key_in_registry></action>
                    <action method="append"><block>product.info.options.wrapper</block></action>
                    <action method="append"><block>product.info.options.wrapper.bottom</block></action>
                </block>

                <block type="core/template_facade" name="product.info.container2" as="container2">
                    <action method="setDataByKey">
                        <key>alias_in_layout</key>
                        <value>container2</value>
                    </action>

                    <action method="setDataByKeyFromRegistry">
                        <key>options_container</key>
                        <key_in_registry>product</key_in_registry>
                    </action>

                    <action method="append">
                        <block>product.info.options.wrapper</block>
                    </action>

                    <action method="append">
                        <block>product.info.options.wrapper.bottom</block>
                    </action>
                </block>

                <action method="unsetCallChild">
                    <child>container1</child>
                    <call>ifEquals</call>
                    <if>0</if>
                    <key>alias_in_layout</key>
                    <key>options_container</key>
                </action>

                <action method="unsetCallChild">
                    <child>container2</child>
                    <call>ifEquals</call>
                    <if>0</if>
                    <key>alias_in_layout</key>
                    <key>options_container</key>
                </action>
            </block>
        </reference>
    </ajax_index_options>    
</layout>
2

Assuming you don't have any kind of full page cache, then the cached version is from the block cache.

The block cache works this way:

Block caching

You need:

  • Lifetime in seconds
  • Key, to distinguish the variants
  • CacheTag, to group them and bulk delete them

Block caching

// app/code/core/Mage/Core/Block/Abstract.php:904 - shortened

final public function toHtml()
{

$html = $this->_loadCache();
if ($html === false) {
    $this->_beforeToHtml();
    $html = $this->_toHtml();
    $this->_saveCache($html);
}
$html = $this->_afterToHtml($html);

Block caching

# …/core/Mage/Core/Block/Abstract.php:1362

public function getCacheLifetime()
{
    if (!$this->hasData('cache_lifetime')) {
        return null;
    }
    return $this->getData('cache_lifetime');
}

overwrite getCacheLifetime() or $this->setCacheLifetime(120)

Block caching

# …/core/Mage/Core/Block/Abstract.php:1289

public function getCacheKey()
{
    if ($this->hasData('cache_key')) {
        return $this->getData('cache_key');
    }
    $key = $this->getCacheKeyInfo();
    $key = array_values($key);  // ignore array keys
    $key = implode('|', $key);
    $key = sha1($key);
    return $key;
}

Block caching

protected function getCacheKeyInfo() {}

overwrite or

$this->setCacheKey(
    $storeId.
    $customerId.
    $productId
);

Block caching

# …/core/Mage/Core/Block/Abstract.php:1311

public function getCacheTags()
{
    if (!$this->hasData('cache_tags')) {
        $tags = array();
    } else {
        $tags = $this->getData('cache_tags');
    }
    $tags[] = self::CACHE_GROUP;
    return $tags;
}

Block-Caching

$this->setCacheTags(array(
    Mage_Catalog_Model_Product::CACHE_TAG,
    'STORE_'.$storeId.'-PRODUCTS',
));

One cache entry can have multiple cache tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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