Magento – Stock Status Not Displayed in Product Grid When Not Set

cataloggridstock

What I want to achive is, to show one column in the catalog grid, which contains the stock status.

Here is my code for adding it to the collection that:

    $collection->joinTable( 'cataloginventory/stock_status','product_id=entity_id', array("stock_status" => "stock_status") )
        ->addAttributeToSelect('stock_status');

I also add a new column:

    $this->addColumn('stock_status',
         array(
            'header'=> 'Stock Status', 
            'width' => '60px',
            'index' => 'stock_status',
            'type'  => 'options',
            'options' => array('1'=>'In Stock','0'=>'Out Of Stock'),
    ));

The Problem now is that if no stock status is set, there is no entry in the db-table cataloginventory_stock_status. This also means, that products which have no stock status set will not even be displayed in this new catalog grid.

Is there a way to display them anyway?

Best Answer

That happens because, by default joinTable performs an INNER JOIN. You can pass an additional parameter to make a LEFT JOIN.

$collection->joinTable(
        'cataloginventory/stock_status',
        'product_id=entity_id', 
        array("stock_status" => "stock_status"),
        null ,
        'left'
    )
    ->addAttributeToSelect('stock_status');