Customizing Magento Meta Titles for Product Pages
Magento provides built-in functionality to control meta tags on product pages, but it may not always meet your specific requirements out of the box. In our case, we want to display only “PRODUCT NAME - Mysite.com” in the title tag on product detail pages.
Understanding Magento’s Default Meta Title Setup
By default, Magento uses a template file named template/page/html/head.phtml located under app/design/frontend/default/default/template/page/html/. In this file, you’ll find the following code snippet that generates the meta title:
<?php if ($this->getTitle()): ?>
<title><?php echo $this->getTitle(); ?></title>
<?php endif; ?>
As you can see, Magento simply echoes out the value returned by $this->getTitle(). This is where we need to intervene and modify the title for product pages.
Customizing the Meta Title on Product Pages
Firstly, let’s understand how to identify a product page. In Magento 1.9, you can detect if you’re on a product view page using the following condition:
if (Mage::registry('current_product') && Mage::registry('current_product')->getId()) {
// This is a product detail page
}
Now, let’s create an observer that will execute whenever the controller_action_predispatch_catalog_product_view event occurs. This event fires right before loading the product view template.
Creating a Custom Event Observer
Navigate to the app/code/core/Mage/Catalog/etc/config.xml file and look for the <events> section inside the <global> tag. Locate the following XML:
<catalog_product_view>
<observers>
<catalog_product_view_handler>
<class>catalog/observer</class>
<method>catalogProductView</method>
</catalog_product_view_handler>
</observers>
</catalog_product_view>
We’ll add a new observer right after the existing one. Add the following code:
<custom_product_title_handler>
<type>singleton</type>
<class>Namespace_Module/Observer</class>
<method>customizeProductTitle</method>
</custom_product_title_handler>
Here, replace Namespace with your actual module name and Module with the name of the observer class.
Implementing the Title Customization
Create a file named Observer.php inside app/code/local/Namespace/Module/Model/ (if it doesn’t exist already). In this file, implement the customizeProductTitle method:
public function customizeProductTitle($observer)
{
$action = $observer->getEvent()->getAction();
$controller = $action->getFullActionName();
if ($controller == 'catalog_product_view') {
$product = Mage::registry('current_product');
$title = $product->getName() . ' - ' . Mage::app()->getStore()->getFrontendName();
Mage::getSingleton('page/config')->addTitle($title);
}
}
This method checks if the current controller is a product view page. If true, it retrieves the current product and constructs a custom meta title string.
Conclusion
Customizing Magento’s meta titles requires understanding how events work in Magento and manipulating them using observers to change the default behavior of specific pages.
Remember that this approach modifies only the frontend output; it doesn’t affect the core functionality. Always ensure your code is properly tested before deploying it into a production environment.
For more advanced SEO customizations, consider reading our guide on Improving Magento SEO Performance on Laravel Company’s blog.