Sunday, March 4, 2012

Magento Add “is in stock” column in Products Grid

Adding the "Availability" in the column Catalog product gird
Here how to add new column to manage “in stock” and “out of stock” products


Edit this file:
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php
and add in local folder 
app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php

For Magento 1.3
Search the following 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');

Replace with
$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');


For Magento 1.5 and above
Search the following code
$collection->joinField( 'qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left' );

Replace with
$collection->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');



Here we have used the join with the cataloginventory/stock_item table to show the availibality in grid with status
For any versions
Find “protected function _prepareColumns()”
here we have our columns....just add the following one where you like between other two columns:
$this->addColumn('stock_status',

            array(

                'header'=> 'Availability',

                'width' => '60px',             //this is the column width

                'index' => 'stock_status',

                'type'  => 'options',

                'options' => array('0'=>'In stock','1'=>'Out of stock'),

        ));

and you will get the Availability in grid........Hope this will help!

3 comments:

  1. Great tip! I have seen another tip, but only after changing "$collection->joinField( 'qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left' );" everything worked fine! It saved me a lot of time!

    ReplyDelete
  2. great-great post. it was really useful for me.

    but now i use small but powerful extension for my magento shop

    http://amasty.com/custom-stock-status.html

    if somebody interested, you can read about it.
    maybe it would be interesting for anybody.

    ReplyDelete

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...