How to Add Filterable ‘Manage Stock’ Column to Catalog Admin Grid

gridmagento-enterprise

After reading many answers here (and a load of Google searches), I've gotten Magento to almost do what I want it to do. Basically, I want to add a column to the 'Manage Products' page under Catalog. Right now, I've been successful in adding the column where I want it, and with a drop down, but I have a couple of issues:

1) the drop down in the header is displaying the 'Attrib. Set Name' options instead of the 'Manage Stock' options

2) Nothing is showing up in the product listings for that column on that page

Below is my code and I'm hoping someone can spot my (obvious) mistake(s). TIA.

EDIT: After a bit more reading, I'm thinking that I may need to do a 'joinTable' to get the value that I'm looking for, but still not quite there. I've updated the code below, but I'm getting a fatal error in the protected function '_prepareColumns', on the array name value pair 'index' => 'manage_stock'. The error in my Apache logs is: "PHP Fatal error: Call to a member function getBackend() on a non-object"

EDIT 2: Made minor changes over the weekend but was unable to get it working. Updated code is below; still producing the fatal error.

EDIT 3: Updated code per Yiorgos' suggestion, and the error went away, as well as something being populated in the field for the product. However, I seem to have a poor understanding of how the options field of the array in the 'addColumnAfter' method in the '_prepareColumns' protected function. No matter what I do there, the data shown in the column is whatever the first element of the array is, regardless of what the setting for the actual product is. I've tried the following:

'options' => array (0 => 'No', 1 => 'Yes') --> All products show 'No'
'options' => array ('0' => 'No', '1' => 'Yes') --> All products show 'No'
'options' => array (0 => 'Yes', 1 => 'No') --> All products show 'Yes'
'options' => array ('0' => 'Yes', '1' => 'No') --> All products show 'Yes'

EDIT 4: Code has been updated below as per Yiorgos', but I'm still having some issue with it not filtering properly. Example – when I filter for 'No', I still get products are are set to 'Yes' (says no in the category column) even though the product is set to 'Yes'.

Sample of catalog page, product example outlined in red:
Sample of catalog page

Product inventory section of product from catalog page:
Product inventory

Updated Code:

<?php

class MyCompany_Catalog_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
    public function setCollection($collection)
    {
        $store = $this->_getStore();
        $collection->joinField('manage_stock',
            'cataloginventory/stock_item',
            'manage_stock',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left');
        parent::setCollection($collection);
    }

    protected function _prepareColumns()
    {
        $store = $this->_getStore();
        $this->addColumnAfter('manage_stock',
                array(
                        'header'=> Mage::helper('catalog')->__('Manage Stock'),
                        'index' => 'manage_stock',
                        'type'  => 'options',
                        'options' => array (
                            '0' => Mage::helper('core')->__('No'),
                            '1' => Mage::helper('core')->__('Yes'),)
                ),
                'qty'
        );
        return parent::_prepareColumns();
    }
}

Best Answer

Try replacing the line

 $collection->addAttributeToSelect('manage_stock');

with

        $collection->joinField('manage_stock',
            'cataloginventory/stock_item',
            'manage_stock',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left');

Your _prepareColumns function should look like this

protected function _prepareColumns(){
    $this->addColumn('manage_stock', array(
           'header'=> Mage::helper('catalog')->__('Manage Stock'),
           'index' => 'manage_stock',
           'type'  => 'options',
           'options' => array (
                           '0' =>  Mage::helper('core')->__('No'), 
                           '1' =>  Mage::helper('core')->__('Yes'),
                        )
    ));
    return parent::_prepareColumns();
}