Adminhtml Product Attribute Grid – Add Custom Attribute Column in Products Grid

adminhtmlgridproduct-attribute

I need to fetch select drop-down labels of my custom product attribute into custom column in admin product grid.

What I have managed to do now is to fetch select values. So for example I have:

<select value="12">23g</select>
<select value="13">44g</select>
<select value="14">54g</select>

and in the column I get the numbers 12, 13, 14 instead of 23g, 44g and 54g.

I've created my custom column by overriding Mage_Adminhtml_Block_Catalog_Product_Grid block. The attribute is 'custom_weight'. I've added this line in _prepareCollection method:

 $collection->addAttributeToSelect('custom_weight');

and these lines in _prepareColumns method:

$this->addColumn('custom_weight',
    array(
      'header'=> Mage::helper('catalog')->__('Weight'),
      'width' => '60px',
      'index' => 'custom_weight',
      'type'  => 'text'
    ));

So how do I get the labels instead of values? Thanks.

Best Answer

Add this above your column defnition:

$weight = Mage::getModel('eav/config')->getAttribute('catalog_product', 'custom_weight');
$options = $weight->getSource()->getAllOptions(false);
$weights = array();
foreach ($options as $option){
    $weights[$option['value']] = $option['label'];
}

and define your column like this

$this->addColumn('custom_weight',
    array(
      'header'  => Mage::helper('catalog')->__('Weight'),
      'width'   => '60px',
      'index'   => 'custom_weight',
      'type'    => 'options',
      'options' => $weights
    )
);
Related Topic