0

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

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

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();
        $collection->getSelect()->joinLeft(
            array('_inventory_table'=>$collection->getTable('cataloginventory/stock_item')),
            "_inventory_table.product_id = e.entity_id",
            array('is_in_stock', 'manage_stock')
        );
        $collection->addExpressionAttributeToSelect(
            'on_top',
            '(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)',
            array()
        );
        $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.

1

Use

Mage::getResourceModel('cataloginventory/stock_status')
    ->addStockStatusToSelect($select, Mage::app()->getWebsite());`

And then sort by salable desc

0

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

Regards

  • 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. – Fabian Blechschmidt Jun 28 '15 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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