Create Custom Category Attribute to Override Category H1 in Magento
Sometimes you may want to extend the title of the category to more than just the name of the category without actually changing the category name (due to navigational elements).
For example:-
You may have a category labelled ‘Blue Widgets’ and you may want to make this more descriptive such as ‘Blue Widgets – Available in Sizes S, M and L’.
First step is to create a custom category attribute. Unfortunately this is not possible from the Magento back office so requires an SQL query to add this to the database. The best way would be to create a custom extension with an SQL install like so:-
Create the following directories and place them in /app/code/community/Your_Namespace
:-
CustomCategoryAttribute/etc
CustomCategoryAttribute/sql
CustomCategoryAttribute/sql/add_category_attribute
Create the file config.xml
, adding the below and place it in CustomCategoryAttribute/etc
:-
<?xml version="1.0"?> <config> <modules> <Your_Namespace_CustomCategoryAttribute> <version>0.0.1</version> </Your_Namespace_CustomCategoryAttribute> </modules> <global> <resources> <add_category_attribute> <setup> <module>Your_Namespace_CustomCategoryAttribute</module> <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </add_category_attribute> <add_category_attribute_write> <connection> <use>core_write</use> </connection> </add_category_attribute_write> <add_category_attribute_read> <connection> <use>core_read</use> </connection> </add_category_attribute_read> </resources> </global> </config>
Create the file mysql4-install-0.0.1.php
, adding the below and place it in CustomCategoryAttribute/sql/add_category_attribute
:-
<?php $this->startSetup(); $this->addAttribute('catalog_category', 'title_override', array( 'group' => 'General Information', 'input' => 'text', 'type' => 'varchar', 'label' => 'Title Override', 'backend' => '', 'visible' => true, 'required' => false, 'visible_on_front' => true, 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, )); $this->endSetup();
Create the file Your_Namespace_CustomCategoryAttribute.xml
, adding the below and place it in /app/etc/modules
:-
<?xml version="1.0"?> <config> <modules> <Your_Namespace_CustomCategoryAttribute> <active>true</active> <codePool>community</codePool> </Your_Namespace_CustomCategoryAttribute> </modules> </config>
When you refresh a page on the front end, the custom category attribute will be added into the database.
After this step, you’ll notice an extra field on the ‘General Information’ tab of the edit category page in the Magento back office like the below:-
Finally, all that is left is to use the custom category attribute on the category pages if it exists.
Navigate to /app/design/frontend/Your_Package/Your_Theme/template/catalog/category/view.phtml and add replace:-
<h1><?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></h1>
With the following:-
<?php if($_customAttribute = $this->getCurrentCategory()->getTitleOverride()): ?> <h1><?php echo $_helper->categoryAttribute($_category, $_customAttribute, 'title_override') ?></h1> <?php else: ?> <h1><?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></h1> <?php endif; ?>
And if implementing the above isn’t your cup of tea, our guys at ClubnetSEM have kindly packaged the above up for you to download. You should ideally move the change to the category view.phtml template though into your theme’s version.
This is awesome, great tutorial – thanks!
No problem Mike, glad you liked it.
I downloaded all the files and uploaded them to my server. Then I opened the specified category view.phtml file and replaced the designated code. The backend category general tab does not reflect the Title Override field.
What did I leave out?
Nevermind, I just needed to disable the cache. First I tried to refresh the cache and the site crashed. Luckily I turned it off and then back on and it’s working!
Glad you got it sorted Mike 🙂
Hey Geoff,
I completed and uploaded the files. I see the new title override and updated my themes view.phtml file. Still the h1 is the category folder name. I can’t figure out why?
Hey Tony, did you clear your cache? You won’t see the change in the frontend until you’ve cleared your cache in Magento.
It will be work in api also?