Rewriting Head.phtml in Magento: A Comprehensive Guide
If you’re looking to modify the page title for your Magento catalog pages without directly editing the core head.phtml file, you’ve come to the right place. In this guide, we’ll walk through the process of creating a custom head template and overriding it within your own module.
Understanding the Current Setup
Before we begin, let’s take a closer look at the current structure:
<code>app/design/frontend(default)/customPackage/template/page/html/head.phtml</code>
This is the default Magento theme’s head.phtml file located in the page/html directory. It handles all page titles across your store.
The existing code within this file determines how the title is generated for each page:
<title>
<?php
if ($current_product = Mage::registry('current_product')) {
echo substr($current_product->getName() . " - " . Mage::helper('core')->currency($current_product->getFinalPrice(), true, false),0,69);
} else {
echo substr(str_replace("- Products","",$this->getTitle()),0,100);
}
?>
</title>
As you can see, it checks for a current product and if found, displays the product name along with its price in the title. If no product is present (like on category or CMS pages), it simply displays the page title.
The Challenge: Direct Modification
The challenge lies in the fact that directly modifying this core file can lead to issues when you update your Magento version. To avoid this, we’ll create a custom template within our own module and override it using theme inheritance.
Creating a Custom Head Template
- Navigate to your custom module directory (or create one if necessary):
<code>app/code/local/YourCompany/YourModule/view/frontend/templates/catalog/html</code>
- Create a new file named
head.phtmlinside thecatalog/htmlfolder:
<code>app/code/local/YourCompany/YourModule/view/frontend/templates/catalog/html/head.phtml</code>
- Open this newly created
head.phtmlfile and add your custom title logic. For example, let’s assume you want to change the catalog page title format:
<title>
<?php
$category = Mage::registry('current_category');
if ($category) {
echo substr($category->getName() . " - " . Mage::helper('catalog')->__('Category'),0,100);
} else {
// Handle other pages here...
}
?>
</title>
Remember to replace the logic with your own title generation process as per your requirements.
Overriding the Template
Now that we have our custom head.phtml file created, it’s time to tell Magento to use this instead of the default one.
- Open your module’s
config.xmlfile located at:
<code>app/code/local/YourCompany/YourModule/etc/config.xml</code>
- Add the following XML code inside the
<global>section, within the<templates>node:
<templates>
<catalog_category_layered translate="label" module="catalog">
<rewrite>
<catalog_category_view translate="label" module="catalog">
<blocks>
<head translate="label" module="page">
<template>YourCompany_YourModule::catalog/html/head.phtml</template>
</head>
</blocks>
</catalog_category_view>
</rewrite>
</catalog_category_layered>
</templates>
Replace YourCompany and YourModule with your actual module namespace.
- Save the changes to the configuration file.
Conclusion
By following this approach, you have successfully created a custom head.phtml template within your own module and overridden it from the default Magento theme. This method ensures that your title logic remains intact even after Magento updates and prevents any core file modifications.
Remember to always refresh your Magento cache (var/cache) after making these changes for them to take effect.
For more advanced theme customizations and Magento development best practices, I recommend checking out our detailed guides at LaravelCompany. Happy coding!