How to Add Custom Attribute on Product Grid in Magento 1.9.2 Community Edition
Adding custom attributes to the product grid in Magento can enhance your admin panel’s functionality. Here’s how you can achieve this using Magento 1.9.2 community edition.
Step 1: Create the Custom Attribute Programmatically
First, let’s create a new attribute programmatically using an install script. This will add an ‘Owners’ attribute to the ‘General’ group of all product sets.
$installer = $this;
$installer->startSetup();
// Set data:
$attributeName = 'Owners'; // Name of the attribute
$attributeCode = 'owners'; // Code of the attribute
$attributeGroup = 'General'; // Group to add the attribute to
$attributeSetIds = array(4); // Array with attribute set ID's to add this attribute to. (ID:4 is the Default Attribute Set)
// Configuration:
$data = array(
'type' => 'varchar', // Attribute type
'input' => 'text', // Input type
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
'required' => false, // Is this attribute required?
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'used_in_product_listing' => true,
'default_value_yesno' => '0',
// Filled from above:
'label' => $attributeName
);
// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->addAttribute('catalog_product', $attributeCode, $data);
$installer->addAttributeToSet(
'catalog_product',
'Default',
$attributeGroup,
$attributeCode
); //Default = attribute set, General = group
$installer->endSetup();
This script adds a new text attribute called ‘Owners’ to the ‘General’ group of all product sets.
Step 2: Add the Custom Attribute to the Product Grid
Next, we need to add this new attribute to the product grid view in the admin panel. To do this:
-
Open
app/code/core/Mage/Adminhtml/blocks/catalog/product/edit/tabs/default.php. -
Look for the line that says
$this->addTab('products',.... -
Add a new line immediately after that, with:
$this->addColumn('owners',
array(
'header' => Mage::helper('catalog')->__('Owner'),
'width' => '100px'
)
);
-
Save the file.
-
Refresh the admin panel and you should now see a new column called ‘Owner’.
Step 3: Ensure the Custom Attribute is Visible
Finally, we need to make sure that this custom attribute is actually visible on the front end of your site if it’s set to be displayed there.
-
Open
app/code/core/Mage/Adminhtml/blocks/catalog/product/edit/tabs/default.php. -
Look for the line that says
$this->setTabLabel('products',.... -
Make sure the ‘Owner’ attribute is set to visible on the front end by setting:
$this->setTabLabel(
'products',
Mage::helper('catalog')->__($tab_label),
array(
'class' => $active ? 'active tab' : '',
'label' => Mage::helper('catalog')->__('Products'),
'title' => Mage::helper('catalog')->__('Product Information')
)
);
If you follow these steps, you should be able to add a custom attribute called ‘Owner’ to your product grid in Magento 1.9.2 community edition.
Remember to clear the cache and refresh the page after making any changes to the admin panel files.
For more advanced Magento development tips and resources, check out Laravel Company, a leading provider of Laravel and Magento development services.
Happy coding!