Magento – Override catalog page title

catalogmagento-1.7

I want to override page title of catalog pages to include selected attributes and subcategories.

e.g. "Shoes – Adidas – Red | Brand"

What observer or model should I override to modify the page title? And how can I get current category?

Thank you very much!

Best Answer

You can easily change the title of the page.

Get the block head and set the title e.g.

// app/code/core/Mage/Page/Block/Html/Head.php:388
Mage::app()->getLayout()->getBlock('head')->setTitle($title);

The current category can be found here:

Mage::registry('current_category');

And the event to use? Good question. I would probably hook into core_block_abstract_to_html_before and check wether the block is Mage_Page_Block_Html_Head

public function changeTitle(Varien_Event_Observer $observer) {
    $block = $observer->getBlock();
    if($block instanceof Mage_Page_Block_Html_Head) {
        $block->setTitle(Mage::registry('current_category')->getName());
    }
}

You have to add a few more checks, e.g. you are on a category page or product page. And you have to check whether current_category exists.

Related Topic