Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Add New Custom Category Attributes to Magento via Module

Time and time again I need to create a custom category attribute for a site I’m working on, and each time I search for a good article covering the topic in depth I fall short, and half written and poorly explained versions of how to do exactly this. I’ve learnt how to do this over time, and now I’ll share exactly how to, and also how it works. First of all, I’ll show you the code for each file, and explain everything afterwards.

Add New Custom Category Attributes to Magento

To create a custom attribute we are going to create it as a module, the reason for this is so when we come to install our Magento site on another server, or if our extension we’re developing needs to be used on other Magento installs our custom category attribute will be installed if it doesn’t yet exist!

First of all let’s set up our basic module file structure we need for this:


app/code/local/Patelgroup/Customcatattrb
app/code/local/Patelgroup/Customcatattrb/etc
app/code/local/Patelgroup/Customcatattrb/sql/customcatattrb_setup

Now let’s create the files, first up is our config.xml, this is the main file within your module which tells Magento which controllers and models to load, plus plenty more.

Save this file to: app/code/local/Patelgroup/Customcatattrb/etc/config.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Patelgroup_Customcatattrb>
            <version>0.1.0</version>
        </Patelgroup_Customcatattrb>
    </modules>
    <global>
        <resources>
            <customcatattrb_setup>
                <setup>
                    <module>Patelgroup_Customcatattrb</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>default_setup</use>
                </connection>
            </customcatattrb_setup>
        </resources>
    </global>
</config>


Next up, let’s create yet another XML file, this time it’s our file which lets magento know we have a module to be loaded in, and where the module is located.

Save this file to: app/etc/modules/Patelgroup_Customcatattrb.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Patelgroup_Customcatattrb>
            <active>true</active>
            <codePool>local</codePool>
        </Patelgroup_Customcatattrb>
    </modules>
</config> 


Last but not least, is our install script. This is a tiny script which will install our attribute to the database, and we can start using our newly created attribute straight away after!

Save to: app/code/local/Patelgroup/Customcatattrb/sql/customcatattrb_setup/mysql4-install-0.0.1.php


<?php
$installer = $this;
$installer->startSetup();
$attribute  = array(
    'type' => 'text',
    'label'=> 'Customer Description',
    'input' => 'textarea',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'default' => "",
    'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'cust_description', $attribute);
$installer->endSetup();
?>

Hit like or leave comment if post help!


Please support us, Like us on Facebook.

  1. hey this did not work, getting so frustated at this now tried like 15 different options and each script fails! - can you help???

    ReplyDelete
    Replies
    1. Now, this is working great. There is a little problem in my code. Please once check again and after finish all step clear your cache.
      Thanks....

      Delete
  2. Nice! The first piece of code that is working!
    THX a lot.

    ReplyDelete
  3. Awesome bit of code.... thanks. Was easy to install and worked.

    ReplyDelete
  4. hey didtn't work for my site.

    ReplyDelete
  5. Works great for me on Magento 1.6.1
    THX a lot
    Andi

    ReplyDelete
  6. thanks a lot

    could you pls tell me data is store on which table ?

    ReplyDelete
    Replies
    1. Thanks,
      You can find data in "eav_attribute" and "catalog_eav_attribute" tables.

      Delete

 

Copyright @ 2017 HKBlog.