3

could you please help me join the stock inventory resource to my grid.php? I would like to edit my grid in order to filter by availability "is in stock".

Tried adding this code, but without joining the resource it will not work.

    $this->addColumn('inventory_stock_availability ',
            array(
                'header' => Mage::helper('catalog')->__('Status'),
                'width' => '100px',
                'index' => 'inventory_stock_availability',
                'type' => 'options',
                'options' => Mage::getSingleton('catalog/inventory_stock_availability ')->getOptionArray(),
            ));

my grid file is just like this one https://github.com/jayelkaake/enhancedgrid/blob/master/app/code/community/TBT/Enhancedgrid/Block/Catalog/Product/Grid.php

7

Add below code:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('sku')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('attribute_set_id')
    ->addAttributeToSelect('type_id')
    ->joinField('qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    )->joinTable('cataloginventory/stock_item', 'product_id=entity_id', array('stock_status' => 'is_in_stock'))
    ->addAttributeToSelect('stock_status');

Add below code in function _prepareColumns()

$this->addColumn('stock_status', array(
    'header'=> 'Availability',
    'width' => '60px',
    'index' => 'stock_status',
    'type'  => 'options',
    'options' => array(
        '1' => 'In stock',
        '0' => 'Out of stock'
    )
));
| improve this answer | |
  • this solution does not work for me: Fatal error: Call to a member function getBackend() on a non-object in app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816 – Sam Provides Apr 5 '16 at 10:06
  • Update code check it.It is working for me – Prashant Valanda Apr 5 '16 at 10:12
  • sorry where do you add the first code exactly? what line? thanks! – Sam Provides Apr 5 '16 at 10:13
  • You just need to add code: ->joinTable( 'cataloginventory/stock_item', 'product_id=entity_id', array("stock_status" => "is_in_stock") ) ->addAttributeToSelect('stock_status'); after your collection code in function _prepareCollection – Prashant Valanda Apr 5 '16 at 10:18
  • ok, that's fine now I see the column, but does it work for you when you filter it? for example by searching only "out of stock" products? for me it does not thanks again – Sam Provides Apr 5 '16 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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