Magento – Join stock inventory resource to the product collection

gridmagento-1

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

Best Answer

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'
    )
));
Related Topic