How to create attribute in magento

How to create attribute in magento

October 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to create attribute in magento

Creating an Attribute for Products in Magento CE 1.9 Programmatically

As a senior developer working on e-commerce platforms like Magento, I often encounter requests to create custom attributes for products programmatically. In this blog post, we’ll walk through the process of creating a boolean attribute named “approved” with default value set to false, assigned to the Default attribute set, and hidden from admin users.

Setting Up Your Environment

Firstly, ensure you have Magento CE 1.9 installed on your local or production environment. For this example, I’ll assume you’re working in a module called Mofosys_Quickbuy.

Step 1: Modify the Setup.php File

Open your app/code/local/Mofosys/Quickbuy/Model/Resource/Eav/Mysql4/Setup.php file. We need to extend the core setup model and adjust some attributes.

<?php

class Mofosys_Quickbuy_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup {
    // Rest of your code...
}

In the _prepareValues() method, we’ll merge our custom values with the parent’s data. This is where you can define the behavior of your new attribute.

protected function _prepareValues($attr) {
    $data = parent::_prepareValues($attr);
    // Merge default and custom attributes
    $data = array_merge($data, []);

    return $data;
}

Step 2: Configure Your Module

Update the config.xml file located in your module’s etc folder.

<global>
    <resources>
        <mofosys_quickbuy_setup>
            <setup>
                <module>Mofosys_Quickbuy</module>
                <class>Mofosys_Quickbuy_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
        </mofosys_quickbuy_setup>
    </resources>
</global>

Step 3: Define Your Attribute in the Installer Script

Open your app/code/local/Mofosys/Quickbuy/sql/mofosys_quickbuy_setup/install-0.0.1.php file and add the following code:

<?php

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

// Define your custom attribute
$setup->addAttribute(
    'catalog_product',
    'approved', // The internal name for the attribute
    array(
        'group' => 'Default',  // Assign to the default attribute group/tab in product edit page
        'input' => 'select',  // Type of input (dropdown, text, textarea, etc.)
        'type' => 'int',      // Data type (varchar, int, decimal, datetime, etc.)
        'default' => '0',     // Default value for the attribute
        'label' => 'Testing', // Label displayed in admin panel and on frontend
        'backend' => '',       // Backend model for validation, leave empty for now
        'visible' => 0,        // Set to false (0) to hide from all admin users
        'required' => 0,       // Set to false (0) since this is optional
        'user_defined' => 0,   // Set to false (0) as it's not a user-defined attribute
        'searchable' => 0,     // Set to false (0) for now, adjust later if needed
        'filterable' => 0,    // Set to false (0) for now, adjust later if needed
        'comparable' => 0,    // Set to false (0) for now, adjust later if needed
        'visible_on_front' => 0,
        'visible_in_advanced_search' => 0,
        'is_html_allowed_on_front' => 0,
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
    )
);

$installer->endSetup();

Conclusion

By following these steps, you’ve successfully created a boolean attribute named “approved” with default value set to false, assigned it to the Default attribute set, and hidden it from all admin users. This custom attribute can now be used in your product models or controllers without being visible on the frontend.

Remember that creating attributes programmatically requires careful consideration of your specific use case and requirements within your Magento environment. For more advanced configurations or complex setup processes, consider reaching out to a professional e-commerce development team like those at Laravel Company.

Happy coding!

Magento 1 Catalog Database API

Our network