The question:
I want to add a custom attribute to the Admin Product Grid.
The attribute got the attribute code import_price
.
How can we add this column?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
There is already one flexible module in github for managing grid columns of any entities(orders, products, customers etc.):
https://github.com/magento-hackathon/GridControl
Method 2
Note: A better solution is a module approach, like in the accepted answer.
I used this blog and that works great! [link] – Jelle
Here’s the gist of that post:
Copy the contents of:
/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php
into a new file (if it doesn’t exist):
/app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
Find this:
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
Below that, add the attribute you want in your admin product grid:
$this->addColumn('import_price',
array(
'header'=> Mage::helper('catalog')->__('Import Price'),
'width' => '150px',
'index' => 'import_price',
));
Find: $collection = Mage::getModel('catalog/product')->getCollection()
within the _prepareCollection()
function.
And add:
->addAttributeToSelect('import_price')
The article also goes into detail for adding a select dropdown list as well.
Magento: Add attribute columns in ‘Manage Products’ grid
Method 3
Found this question using Google, the selected answer works but I just tried out this module and wanted to share. It installs through Magento Connect and works extremely well.
(dead link) http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html
new link: https://github.com/mage-eag/mage-enhanced-admin-grids
Method 4
Add below code to select attribuet in _prepareCollection function of product grid
Mage_Adminhtml_Block_Catalog_Product_Grid before $this->setCollection($collection) line.
$attributeCode = 'qc_status';//here your attribute code
$collection->joinAttribute($attributeCode, 'catalog_product/'.$attributeCode, 'entity_id', null, 'left');
$collection->addAttributeToSelect($attributeCode);
And then below code for column in _prepareColumns function of grid.
$attributeCodeConfig ='qc_status';//Your attribute code...
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $attributeCodeConfig);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeData = $attribute->getData();
$frontEndLabel = $attributeData['frontend_label'];
$attributeOptions = $attribute->getSource()->getAllOptions();
$b = new Mage_Catalog_Model_Resource_Eav_Attribute();
$attributeOptions2 = array();
foreach ($attributeOptions as $value) {
if(!empty($value['value'])) {
$attributeOptions2[$value['value']] = $value['label'];
}
}
if(count($attributeOptions2) > 0) {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
'type' => 'options',
'options' => $attributeOptions2,
));
} else {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
));
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0