PHP Magento Tips

How to Create Custom Product attribute and attribute group In Magento With Install Script

Rate this posting:
{[['']]}

In my previous post you can check How to Create Custom Product Attribute In Magento With Install Script.

In that, new attribute will be added in default "Price" group.

Here, new product attributes will be added under new attribute group (tab).

Step 1: Create file in app/etc/modules/Training_Animal.xml and add below code in it.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Training_Animal>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
             </depends>
        </Training_Animal>
    </modules>
</config>


Step 2: Create file in app/code/local/Training/Animal/etc/config.xml and add below code in it.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Training_Animal>
            <version>1.0.0</version>
        </Training_Animal>
    </modules>
    <global>
     <resources>
            <training_animal_setup>
                <setup>

                    <module>Training_Animal</module>

                    <class>Training_Animal_Model_Resource_Setup</class>

                </setup>

                <connection>

               <use>core_setup</use>

            </connection>

            </training_animal_setup>

            <training_animal_write>

           <connection>

             <use>core_write</use>

           </connection>

         </training_animal_write>

        <training_animal_read>

           <connection>

             <use>core_read</use>

           </connection>

        </training_animal_read>

        </resources>

        <models>

            <training_animal>

                <class>Training_Animal_Model</class>

                <resourceModel>training_animal_resource</resourceModel>

            </training_animal>

        </models>

    </global>

</config>


Step 3: Create file in app/code/local/Training/Animal/Model/Resource/Setup.php and add below code in it.

<?php

// app/code/local/Training/Animal/Model/Resource/Setup.php

class Training_Animal_Model_Resource_Setup extends Mage_Catalog_Model_Resource_Setup

{

}


Step 4: Create file in app/code/local/Training/Animal/sql/training_animal_setup/install-1.0.0.php and add below code in it.

<?php

$installer = Mage::getResourceModel ( 'catalog/setup', 'catalog/setup' );

$installer->startSetup ();

// addAttribute uses _prepareValues()

$data = array (

  'attribute_set' => 'Default',

  'sort_order' => 20,

  'group' => 'Extra info',

  'label' => 'Something Enable',

  'note' => 'Make Something Enable From Here',

  'frontend_class' => ' required_entry input-text ',

  'type' => 'int', // multiselect uses comma sep storage

  'input' => 'select',

  'backend' => '',

  'frontend' => '',

  'required' => 1,

  'user_defined' => 1,

  'unique' => 0, // eav_attribute.is_unique unique value required

  'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // catalog_eav_attribute.is_global (products only) scope

  'visible' => 1, // catalog_eav_attribute.is_visible (products only) visible on admin, setting to false stops import of this attribute

  'visible_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) visible on frontend (store) attribute table

  'used_in_product_listing' => 0, // catalog_eav_attribute.used_in_product_listing (products only) made available in product listing

  'searchable' => 0, // catalog_eav_attribute.is_searchable (products only) searchable via basic search

  'visible_in_advanced_search' => 0, // catalog_eav_attribute.is_visible_in_advanced_search (products only) searchable via advanced search

  'filterable' => 0, // catalog_eav_attribute.is_filterable (products only) use in layered nav

  'filterable_in_search' => 0, // catalog_eav_attribute.is_filterable_in_search (products only) use in search results layered nav

  'comparable' => 0, // catalog_eav_attribute.is_comparable (products only) comparable on frontend

  'is_html_allowed_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) seems obvious, but also see visible

  'apply_to' => 'simple,configurable', // catalog_eav_attribute.apply_to (products only) which product types to apply to

  'is_configurable' => 0, // catalog_eav_attribute.is_configurable (products only) used for configurable products or not

  'used_for_sort_by' => 0, // catalog_eav_attribute.used_for_sort_by (products only) available in the 'sort by' menu

  'position' => 0, // catalog_eav_attribute.position (products only) position in layered naviagtion

  'used_for_promo_rules' => 0,

  'source' => 'eav/entity_attribute_source_boolean',

);

$installer->addAttribute ( 'catalog_product', 'something_enable', $data );



$data = array (

  'attribute_set' => 'Default',

  'sort_order' => 21,

  'group' => 'Extra info',

  'label' => 'My Special Title',

  'note' => 'Add your Special Title Here',

  'frontend_class' => ' required_entry input-text ',

  'type' => 'text', // multiselect uses comma sep storage

  'input' => 'text',

  'backend' => '',

  'frontend' => '',

  'required' => 1,

  'user_defined' => 1,

  'unique' => 0, // eav_attribute.is_unique unique value required

  'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // catalog_eav_attribute.is_global (products only) scope

  'visible' => 1, // catalog_eav_attribute.is_visible (products only) visible on admin, setting to false stops import of this attribute

  'visible_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) visible on frontend (store) attribute table

  'used_in_product_listing' => 0, // catalog_eav_attribute.used_in_product_listing (products only) made available in product listing

  'searchable' => 0, // catalog_eav_attribute.is_searchable (products only) searchable via basic search

  'visible_in_advanced_search' => 0, // catalog_eav_attribute.is_visible_in_advanced_search (products only) searchable via advanced search

  'filterable' => 0, // catalog_eav_attribute.is_filterable (products only) use in layered nav

  'filterable_in_search' => 0, // catalog_eav_attribute.is_filterable_in_search (products only) use in search results layered nav

  'comparable' => 0, // catalog_eav_attribute.is_comparable (products only) comparable on frontend

  'is_html_allowed_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) seems obvious, but also see visible

  'apply_to' => 'simple,configurable', // catalog_eav_attribute.apply_to (products only) which product types to apply to

  'is_configurable' => 0, // catalog_eav_attribute.is_configurable (products only) used for configurable products or not

  'used_for_sort_by' => 0, // catalog_eav_attribute.used_for_sort_by (products only) available in the 'sort by' menu

  'position' => 0, // catalog_eav_attribute.position (products only) position in layered naviagtion

  'used_for_promo_rules' => 0,

);

$installer->addAttribute ( 'catalog_product', 'my_special_title', $data );



$data = array (

  'attribute_set' => 'Default',

  'backend' => 'eav/entity_attribute_backend_datetime',

  'sort_order' => 22,

  'group' => 'Extra info',

  'label' => 'My Start Date and Time',

  'note' => 'Add your Start Date and Time',

  'frontend_class' => ' required_entry input-text validate-date validate-date-range date-range-my_start_date-from',

  'type' => 'datetime', // multiselect uses comma sep storage

  'input' => 'datetime',

  'backend' => '',

  'frontend' => '',

  'required' => 1,

  'user_defined' => 1,

  'unique' => 0, // eav_attribute.is_unique unique value required

  'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // catalog_eav_attribute.is_global (products only) scope

  'visible' => 1, // catalog_eav_attribute.is_visible (products only) visible on admin, setting to false stops import of this attribute

  'visible_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) visible on frontend (store) attribute table

  'used_in_product_listing' => 0, // catalog_eav_attribute.used_in_product_listing (products only) made available in product listing

  'searchable' => 0, // catalog_eav_attribute.is_searchable (products only) searchable via basic search

  'visible_in_advanced_search' => 0, // catalog_eav_attribute.is_visible_in_advanced_search (products only) searchable via advanced search

  'filterable' => 0, // catalog_eav_attribute.is_filterable (products only) use in layered nav

  'filterable_in_search' => 0, // catalog_eav_attribute.is_filterable_in_search (products only) use in search results layered nav

  'comparable' => 0, // catalog_eav_attribute.is_comparable (products only) comparable on frontend

  'is_html_allowed_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) seems obvious, but also see visible

  'apply_to' => 'simple,configurable', // catalog_eav_attribute.apply_to (products only) which product types to apply to

  'is_configurable' => 0, // catalog_eav_attribute.is_configurable (products only) used for configurable products or not

  'used_for_sort_by' => 0, // catalog_eav_attribute.used_for_sort_by (products only) available in the 'sort by' menu

  'position' => 0, // catalog_eav_attribute.position (products only) position in layered naviagtion

  'used_for_promo_rules' => 0,

);

$installer->addAttribute ( 'catalog_product', 'my_start_date', $data );



$data = array (

  'attribute_set' => 'Default',

  'backend' => 'eav/entity_attribute_backend_datetime',

  'sort_order' => 23,

  'group' => 'Extra info',

  'label' => 'My End Date and Time',

  'note' => 'My End Date and Time',

  'frontend_class' => ' required_entry input-text validate-date validate-date-range date-range-my_end_date-to',

  'type' => 'datetime', // multiselect uses comma sep storage

  'input' => 'datetime',

  'backend' => '',

  'frontend' => '',

  'required' => 1,

  'user_defined' => 1,

  'unique' => 0, // eav_attribute.is_unique unique value required

  'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // catalog_eav_attribute.is_global (products only) scope

  'visible' => 1, // catalog_eav_attribute.is_visible (products only) visible on admin, setting to false stops import of this attribute

  'visible_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) visible on frontend (store) attribute table

  'used_in_product_listing' => 0, // catalog_eav_attribute.used_in_product_listing (products only) made available in product listing

  'searchable' => 0, // catalog_eav_attribute.is_searchable (products only) searchable via basic search

  'visible_in_advanced_search' => 0, //catalog_eav_attribute.is_visible_in_advanced_search (products only) searchable via advanced search

  'filterable' => 0, // catalog_eav_attribute.is_filterable (products only) use in layered nav

  'filterable_in_search' => 0, // catalog_eav_attribute.is_filterable_in_search (products only) use in search results layered nav

  'comparable' => 0, // catalog_eav_attribute.is_comparable (products only) comparable on frontend

  'is_html_allowed_on_front' => 0, // catalog_eav_attribute.is_visible_on_front (products only) seems obvious, but also see visible

  'apply_to' => 'simple,configurable', // catalog_eav_attribute.apply_to (products only) which product types to apply to

  'is_configurable' => 0, // catalog_eav_attribute.is_configurable (products only) used for configurable products or not

  'used_for_sort_by' => 0, // catalog_eav_attribute.used_for_sort_by (products only) available in the 'sort by' menu

  'position' => 0, // catalog_eav_attribute.position (products only) position in layered naviagtion

  'used_for_promo_rules' => 0,

);

$installer->addAttribute ( 'catalog_product', 'my_end_date', $data );

// Done:

$installer->endSetup ();
With above module there is one new tab will be added when you add/edit product from Catalog >> Manage Products tab.


0 Comments On "How to Create Custom Product attribute and attribute group In Magento With Install Script"

Back To Top