PBirnie

Magento Developers: Add a Custom Field to the Category Admin Page

The ability to tailor a Magento installation to a specific business' needs can be critical for SEO performance. This is specifically true when it comes to title tags, header text and content on product and category pages. 

The ability to tailor a Magento installation to a specific business' needs can be critical for SEO performance. This is specifically true when it comes to title tags, header text and content on product and category pages. 

On JewelExclusive, a discount fashion jewelry site, we were faced with a dilemma on our category landing pages. 

By default, Magento uses the category "Name" attribute to populate both the title in the navigation and the main heading tag as shown below.

magento_category_dilema

Decoupling the Title and Navigation Titles

To improve SEO performance on these pages, we wanted to change the page title displayed on our category landing page to be more descriptive, while allowing our navigation title to remain the same. Importantly, we needed a solution that would allow our SEO team and others without extensive technical knowledge of HTML/Magento to update.

Our solution was to create a custom text field on our Category configuration pages within the Magento backend. Unlike product attributes which can be added using the GUI, adding new category fields is not a trivial process. Unless you are willing to hack into the core Magento install (obviously not recommended if you ever plan to upgrade!), this requires building an extension. Let's walk through this step by step.

Step 1: Create the extension.xml file

I'm going to call my extension Adept_Catlanding. You may tailor this namespace to your needs. However, as with any Magento extension, be very careful to update all of your configuration and file paths accordingly.

This file goes in app/etc/modules/Adept_Catlanding.xml

 

Step 2: Create the module directory structure

Next create the appropriate directory structure; I'm using the local code pool so we'll need to create the following files (and their respective directories.

    • app/code/local/Adept_Catlanding/etc/config.xml
    • app/code/local/Adept_Catlanding/sql/adept_catlanding_setup/mysql4-install-0.3.0.php

As you can see, this is a very simple Magento extension with just the configuration file and our installation file, which is where all the magic happens!

Step 3: Setup config.xml

 

There are few "gotchas" here:

      • Make sure that your version number (I've used 0.3.0) matches the filename in your installation script (above) or your installation script will not be called.
      • Ensure that your setup class points to Mage_Catalog_Model_Resource_Setup. We will rely on several methods (and inherited methods) of this class when we run our installation script. You will get a fatal error if you forget this!
      • As usual, double-check the usual trip-ups such as capitalization and typos.

Step 4: Write your installation script

Now let's write our installation script; I've included comments within to guide you along. Note that the custom variable I'm adding is called internal_title.

 

In my case I've added a text field that will be stored in eav_entity_varchar, but you can change this according to your needs.

Step 5: Clear your cache and login to the admin

Delete all of your files/folders in var/cache or clear your cache from the admin and check out your category pages. If you did everything correctly, you should see something like this:

finished

Once you are done, you can use something like this $category->getInternalTitle(); assuming category is an instance of Mage_Catalog_Model_Category. Cool stuff!

Troubleshooting

If things didn't work out, check your core_resource table to see if the installation script actually ran. If it did, you should see a row with adept_catlanding and the correct version. If it's missing, make sure you cleared your cache and double-check your installation script filename, path and version number.

If it looks like it ran, check your eav_attribute table and make sure that the attribute is there. If not, the problem is in your installer script. You'll need to remove the aforementioned line from core_resource to get your installer script to run again.

magneto_developer_add a custom field to the category admin page.jpg

Like what you just read? Share it!

PBirnie

About PBirnie

Phil specializes in the construction of the back-end of data driven websites. He is an expert in HTML, PHP, MySQL and is currently focused on developing expertise in Ruby on Rails and Mobile Optimization. In this ever-changing industry, Phil strives to pick up a new skill or bit of knowledge every day to refine his craft.