Abhishek Dhanraj Shahdeo Abhishek Dhanraj Shahdeo - 1 year ago 115
PHP Question

How to sort product collection on stock status in magento

I want to sort product collection on category page so that the in stock products shall appear before out of stock products. All the out of stock products will be displayed after the in stock products. I have already rewritten the Mage_Product_Block_List block.

Answer Source
You can use catalog_product_collection_load_before event.   
And in the observer:

public function modifyProductCollection(Varien_Event_Observer $observer) {
    $collection = $observer->getCollection();

                array('_inventory_table' => $collection->getTable('cataloginventory/stock_item')),
                "_inventory_table.product_id = e.entity_id",
            ->order('is_in_stock DESC')
            ->order('created_at DESC');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download