Magento – Magento 1.9: How to show instock items first in product listing even after using sorting

frontendmagento-1.9product-listsorting

I am facing an issue in Magento 1.9 where in product listing page every thing is working fine, We have a number of sorting options for products and a paging tab, now our requirement is to show first in-stock items in a list and after that, it should show out-of-stock items if any.

I tried below code in /app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php

public function setCollection($collection)
    {
        $this->_collection = $collection;
        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
                $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
                $this->_collection->getSelect()
                ->joinLeft(
                            array('sfoi' => $this->_collection->getResource()->getTable('cataloginventory/stock_item')),
                             'e.entity_id = sfoi.product_id'
                             ,array('qty' => 'sfoi.qty')
                         )
                     //->group('e.entity_id')
                     ->order('sfoi.qty DESC');

                //echo $this->_collection->getSelect();exit;

        }
        return $this;
    }

The above code is sorting products first with in-stock but it is having issues –

1- it showing products from out of the category which is not assigned to it.
2- products are sorted as per quantity, so it will change the alignment of products when quantity changed.

Any help will be helpful to me.

Thanks
EDIT
====

After doing R & D , I come to know that if you have allowed products in Backorders then the auto updation by magento to update status to `out of stock` has been stoped, i.e. if a product has been out of stock, magento will still show and keep status `in stock`, so that customers can add them to back orders.

Now my issue is that , I still want to show those products above in a list which is actually are in stock or you can say that greater than 0 in quantity, will any one please let me help on this ,

Thanks

Best Answer

Try this, I hope this will solve your problem.

public function setCollection($collection)
    {
        $this->_collection = $collection;
        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
                $this->_collection->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id','is_in_stock>=0', 'left')->setOrder('inventory_in_stock', 'desc');
                $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
        return $this;
    }

Source : ShowInStockFirst