Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Create an attribute of a product through mysql setup Installer Script ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 463
    Comment on it

    In order to create an attribute of a product we can use our mysql setup Installer Script. In the following example we will see how can we do it.

    Lets we are required to add an attribute 'Warranty Period' to the products in the group 'General'.

    The attribute will be an 'catalog_product’ entity type.

     

    In our Installer file which is at the path “YourModule/sql/yourmodule_setup” directory, write the bellow code for the same.

     

    <?php
    
    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    
    $setup->addAttribute('catalog_product', 'warranty_period', array(
        'group'         => 'Prices',
        'note'         => 'Enter period in with respect to months only',
        'input'         => 'text',
        'type'          => 'text',
        'label'         => 'Warranty Period',
        'backend'       => '',
        'visible'       => 1,
        'required'        => 0,
        'user_defined' => 1,
        'searchable' => 1,
        'filterable' => 0,
        'comparable'    => 1,
        'visible_on_front' => 1,
        'visible_in_advanced_search'  => 0,
        'is_html_allowed_on_front' => 0,
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    ));
    
    $installer->endSetup();

     

    In the above script we created an object of 'core_setup' i.e., $setup and then in the addAttribute() method we defined our attribute.


    In the addAttribute() method we passed the parameters are :

    1- Entity Type Id : It is an entity type string i.e., 'catalog_product'  which can be an integer value. This information found in eav_entity_type table.

     

    2- Attribute Code : It is an attribute code in our case we have passed 'warranty_period'.

     

    3- Data Array : This array defines the values of the attribute code.

     

    In this Data Array of the attribute code, if we notice we have first passed the array parameter which is a group. In this case it has value 'Prices' because we are required to add the attribute in this group only. Afterwards we defined other properties of the attribute code accordingly.

     

    Now, Install your module and after clearing the cache then hit the root url of the setup. Afterwards the script gets run and the attribute with the defined properties gets created, Which will get appears in the products price tab in manage products at the admin panel.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: