0

How to invalidate block cache after product attribute was edited. E.g. there is a multiselect attribte and if I edit its values in Magento admin I need the cache of the block to be deleted and refreshed.

There are many examples of how to do this when specific product was edited, e.g. on Magento forum: http://www.magentocommerce.com/wiki/5_-_modules_and_development/block_cache_and_html_ouput

protected function _construct()
{
    $this->addData(array(
        'cache_lifetime'    => 120,
        'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG . "_" . $this->getProduct()->getId()),
        'cache_key'         => $this->getProduct()->getId(),
    ));
}

but how to do this for attributes?

I've found the Mage_Eav_Model_Entity_Attribute::CACHE_TAG constant for attributes but I'm not sure how to use it to delete the cache only when specific attribute was edited:

protected function _construct()
{
    $this->addData(array(
        'cache_lifetime'    => 120,
        'cache_tags'        => array(Mage_Eav_Model_Entity_Attribute::CACHE_TAG),
        'cache_key'         => $product->getResource()->getAttribute('color')->getFrontend()->getValue($product),
    ));
}
2

If you use 'cache_key' => $product->getResource()->getAttribute('color')->getFrontend()->getValue($product) you don't have to invalidate the cache, it will have different cache key when you edit the product. It would be better to use a key like this one 'cache_key' => 'MY_BLOCK_PRODUCT_' . $product->getId() . '_COLOR_' $product->getColor().

  • Thank you. That cache_key was only an example copied from somewhere, in my case the key will be different. So I will need to use cache_tags to refresh the cache. – zitix Mar 21 '14 at 15:45

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.