I want to show first all in-stock products and then out-of-stock products respectively and sort by price in the ascending order.

Following is my code. In my local.xml I wrote the following code for the price from low to high

        <reference name="product_list">
            <action method="setDefaultDirection"><dir>asc</dir></action>

and I wrote an observer to show all out-of-stock products last, i.e., after loading all in-stock products. For this I had used the catalog_product_collection_load_before event

$collection = $observer->getCollection();
            "_inventory_table.product_id = e.entity_id",
            array('is_in_stock', 'manage_stock')
            '(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR  ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',
        $collection->getSelect()->order('on_top DESC');
        $order = $collection->getSelect()->getPart('order');
        array_unshift($order, array_pop($order));
        $collection->getSelect()->setPart('order', $order);

Now what is happening is I can see the products from low to high but not out-of-stock products last.

2 Answers 2



    ->addStockStatusToSelect($select, Mage::app()->getWebsite());`

And then sort by salable desc


you have to use another event, catalog_product_collection_load_before is useless because Mage_Review Module-Observer loads the product list collection so you cant edit this collection any more. You can disable this observer if you dont use reviews or do it like described here:

catalog_product_collection_load_before and getLoadedProductCollection


  • If the collection is loaded, the catalog_product_collection_load_before event is dispatched here: \Mage_Catalog_Model_Resource_Product_Collection::_beforeLoad, so it should work. Jun 28, 2015 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.