How to add custom product attributes and attribute options in Magento 1.x

Anyone who worked on maintaining a Magento website or create a new online store from scratch using Magento 1.x definitely stumbled upon adding new custom product attributes.

And there are two ways to do this: the easy way or the hard way.

Because we, at Zitec, care about doing things by the book, we went with the hard way, meaning that we chose to add these new custom product attributes programmatically, in order to keep consistency more easily between all our development environments (our local, testing and production ones).

Let’s say that we are running a Magento 1.x store that sells clothes. And we decide to help our customers by describing our items better and showing them what’s the season our products are suitable for. But that’s not all! We are running a multi-language website, in several countries, so we must make sure we add a translation for those seasons as well.

In this article, I’m going to show you how to add a new season product attribute programmatically.

Step 1 – directory tree structure

You should be familiar with creating a new module in Magento. If not, you could visit this article and get some more info on it.

Next, I’ll be showing you the directory tree structure our module will have.

Figure 1. Directory tree structure for our new attributes module

Figure 1. Directory tree structure for our new attributes module

Step 2 – file contents

app/code/local/Zitec/Attributes/etc/config.xml

 

<config>

   <modules>

       <Zitec_Attributes>

           <version>1.0.0</version>

       </Zitec_Attributes>

   </modules>

   <global>

       <resources>

           <zitec_attributes_setup>

               <setup>

                   <module>Zitec_Attributes</module>

                   <class>Mage_Core_Model_Resource_Setup</class>

               </setup>

               <connection>

                   <use>core_setup</use>

               </connection>

           </zitec_attributes_setup>

       </resources>

       <helpers>

           <Zitec_Attributes>

               <class>Zitec_Attributes_Helper</class>

           </Zitec_Attributes>

       </helpers>

   </global>

</config>

 

app/code/local/Zitec/Attributes/Helper/Data.php

 

<?php

class Zitec_Attributes_Helper_Data extends Mage_Core_Helper_Abstract

{

   const STORE_ID_DEFAULT = 0;

   const STORE_ID_CO_UK = 1;

   const STORE_ID_RO = 2;


   const CLOTHING_ATTRIBUTE_SET_ID = 9;

   const CLOTHING_GENERAL_GROUP_ID = 24;

}

 

app/code/local/Zitec/Attributes/data/zitec_attributes_setup/data-install-1.0.0.php

 

<?php

// Add new attributes

$config = Mage::getModel('core/config');

$attributeSetSortOrder = 100;

$productEntityTypeId = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getId();

// 1.1 Add season attribute

$model = Mage::getModel('catalog/resource_eav_attribute');

$data = [

   // attribute scope: Mage_Catalog_Model_Resource_Eav_Attribute SCOPE constants as value

   'is_global'                     => '0',

   // default value for text input

   'default_value_text'            => '',

   // default value for a yes/no input

   'default_value_yesno'           => '0',

   // default value for a date input

   'default_value_date'            => '',

   // default value for a textarea input

   'default_value_textarea'        => '',

   'is_unique'                     => '0',

   'is_required'                   => '0',

   // the value will be used to modify the class attribute of the form element

   'frontend_class'                => '',

   'is_configurable'               => '0',

   'is_searchable'                 => '1',

   'is_visible_in_advanced_search' => '1',

   'is_comparable'                 => '1',

   'is_filterable'                 => '1',

   'is_filterable_in_search'       => '1',

   'is_used_for_promo_rules'       => '0',

   'position'                      => '0',

   'is_html_allowed_on_front'      => '1',

   'is_visible_on_front'           => '1',

   'used_in_product_listing'       => '1',

   // Translated label on desired stores

   'frontend_label'                =>

       [

           Zitec_Attributes_Helper_Data::STORE_ID_DEFAULT => 'Season',

           Zitec_Attributes_Helper_Data::STORE_ID_CO_UK   => 'Season',

           Zitec_Attributes_Helper_Data::STORE_ID_RO      => 'Anotimp'

       ],

   'attribute_code'                => 'season',

   'is_user_defined'               => '1',

   'frontend_input'                => 'multiselect',

   // Database type for the attribute

   'backend_type'                  => 'varchar',

   // Model used for validation and different actions on the attribute on save

   'backend_model'                  => 'eav/entity_attribute_backend_array',

];

$model->addData($data);

$model->setEntityTypeId($productEntityTypeId);

$model->setIsUserDefined(true);

$model->save();

// 1.2 Assign newly created season attribute to attribute sets

$setup = Mage::getModel('eav/entity_setup', 'core_setup');

$setup->addAttributeToSet(

   $productEntityTypeId,

   Zitec_Attributes_Helper_Data::CLOTHING_ATTRIBUTE_SET_ID,

   Zitec_Attributes_Helper_Data::CLOTHING_GENERAL_GROUP_ID,

   $model->getId(),

   $attributeSetSortOrder

);

// 1.3 Add new season store labels

$optionData = [

   'attribute_id' => $model->getId(),

   'value'        =>

       [

           [

               Zitec_Attributes_Helper_Data::STORE_ID_DEFAULT => 'Spring',

               Zitec_Attributes_Helper_Data::STORE_ID_CO_UK   => 'Spring',

               Zitec_Attributes_Helper_Data::STORE_ID_RO      => 'Primăvară'

           ]

       ]

];

$setup->addAttributeOption($optionData);

$optionData = [

   'attribute_id' => $model->getId(),

   'value'        =>

       [

           [

               Zitec_Attributes_Helper_Data::STORE_ID_DEFAULT => 'Summer',

               Zitec_Attributes_Helper_Data::STORE_ID_CO_UK   => 'Summer',

               Zitec_Attributes_Helper_Data::STORE_ID_RO      => 'Vară'

           ]

       ]

];

$setup->addAttributeOption($optionData);

$optionData = [

   'attribute_id' => $model->getId(),

   'value'        =>

       [

           [

               Zitec_Attributes_Helper_Data::STORE_ID_DEFAULT => 'Fall',

               Zitec_Attributes_Helper_Data::STORE_ID_CO_UK   => 'Fall',

               Zitec_Attributes_Helper_Data::STORE_ID_RO      => 'Toamnă'

           ]

       ]

];

$setup->addAttributeOption($optionData);

$optionData = [

   'attribute_id' => $model->getId(),

   'value'        =>

       [

           [

               Zitec_Attributes_Helper_Data::STORE_ID_DEFAULT => 'Winter',

               Zitec_Attributes_Helper_Data::STORE_ID_CO_UK   => 'Winter',

               Zitec_Attributes_Helper_Data::STORE_ID_RO      => 'Iarnă'

           ]

       ]

];

$setup->addAttributeOption($optionData);

 

Step 3 – explanations: how, why and what?!

There are 3 files that I’ll focus on.

First one is the config file. The only important thing in here is the “resources” tag containing the main setup tag that will be added in the core_resource table.

The second one is the Helper/Data file. We want to use constants and not randomly displaying numbers that other new developers maintaining our code won’t understand what those numbers mean.

The store IDs you’ll get from going into your admin area – System – Manage stores – Store View Name.

The attribute set IDs you’ll add in here are the IDs of your attribute sets where you want your attribute to be part of. We used one attribute set named “Clothing”. You can find its ID from admin – Catalog – Manage attribute sets.

When editing a product or creating a new one, if you want to display this new attribute in the General tab, you’ll have to find the General group ID from “eav_attribute_group” table with the attribute_set_id previously found and the “attribute_group_name” set as “General”. This logic still applies if you want to add the product in any tab in the product editing section. Also, Magento supports associating the attribute to an attribute set by defining the group name instead of the ID. For future reference, you can check the addAttributeToSet method implementation.

The last file is where all the logic is done: data-install-1.0.0.php.

We added 3 main sections in this file:

  1. Creating a new attribute named “season”. What’s worth mentioning here is that we added 3 frontend admin labels, and the frontend input type is multiselect, in case our clothes are wearable in multiple seasons like in spring and also in fall.
  2. Assigning the new attribute to our attribute sets we want
  3. Adding the store labels (translations on frontend).

 

Step 4 – Enjoy your new attribute

Figure 2. The new ‘Season’ attribute displayed in the admin area, when editing a product.

Figure 2. The new  ‘Season’ attribute displayed in the admin area, when editing a product.

 

P.S. Are you sure you’re not forgetting anything? How about app/etc/modules/Zitec_Attributes.xml file? *wink*

 

Leave a Reply

Your email address will not be published. Required fields are marked *