Question

Looking at /app/code/Mage/Catalog/Block/Product/View.php I can see the logic for the meta description tag within the _prepareLayout function

$description = $product->getMetaDescription();
            if ($description) {
                $headBlock->setDescription( ($description) );
            } else {
                $headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
            }

This is great but I would like to change the logic so that it populates the meta description in this order

  1. Meta description
  2. Description
  3. Short Description
  4. Product Title
  5. Default meta description

Whats the best way to achieve this? Would it be to override the _prepareLayout function?

Was it helpful?

Solution

I would suggest against override of the block, it causes nightmares when it's time to upgrade Magento. A much cleaner way would be to add an observer for either

  1. "core_block_abstract_prepare_layout_after" or
  2. "core_block_abstract_to_html_before"

events. Both will give you access to the desired object. There might be other events too which will allow you to modify the block logic.

With an observer, you get an advantage, that even if core system stops triggering that event in a future upgrade(Not likely, but let's be a less optimistic for a moment), you'll just have a function doing nothing, it won't be a "Fatal Error" in Block file.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top