1

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();
    }
}
2
  • I don't seem to have the attribute name correct. I get an error in my Apache log (Call to a member function getBackend() on a non-object) with this line in addColumnAfter -- 'index' => 'manage_stock' I can't find any matching attribute name in the eav_attribute table, so I'm lost at this point.
    – John
    Mar 27, 2014 at 17:31
  • Does someone know if this is even possible? If it can't be done, I can just tell my boss so that I can move on to my next task. On the positive side, in my search for the answer, I've found a few good posts that should help me with other modifications we want to make.
    – John
    Mar 31, 2014 at 14:07

2 Answers 2

4

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();
}
10
  • This got me somewhere. Seems I have an issue with the 'options' element for the array in 'addColumnAfter' in the protected function _prepareColumns. It seems to be defaulting to whatever option I have first, no matter what the product is set as.
    – John
    Mar 31, 2014 at 15:59
  • Updated the answer give it a try Mar 31, 2014 at 16:35
  • Doesn't seem to have changed anything, but after further investigation, it is filtering some products, but many that are set as 'Yes' for 'Manage Stock' are showing up when I filter for 'No' instead of 'Yes'. I'm going to update the original post to reflect the changes, and post some screen shots of what it is doing.
    – John
    Mar 31, 2014 at 17:15
  • I may sound silly, but do you take the 'use_config_manage_stock' option into account? Mar 31, 2014 at 17:20
  • Nope I don't sound silly. On your screenshot notice the checked checkbox that says 'Use Config Settings' under your 'Manage Stock' DropDown? This overrides 'manage_stock'. So in your DB manage_stock is actually 0 not 1 Mar 31, 2014 at 17:26
0

I think you'll find this tool will do it for you (I use it for this purpose) -

http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html

enter image description here

2
  • Hi James. Thanks for the response. Just so I'm clear, you use this extension to add the 'Manage Stock' field to the catalog grid?
    – John
    Mar 31, 2014 at 15:39
  • Hi John. No problem. I've added a screenshot to the answer. Hopefully this will clarify. Thanks James Mar 31, 2014 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.