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?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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?
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
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.
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
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,
));
}