Created
August 26, 2014 00:33
-
-
Save rodrigowebjump/da4ea2a7e2f6073c62d6 to your computer and use it in GitHub Desktop.
Show out of stock at end
From http://stackoverflow.com/questions/15879978/magento-out-of-stock-products-showing-last-in-the-category-page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Create an observer and add event listener to catalog_product_collection_load_before | |
| /** | |
| * Fires before a product collection is loaded | |
| * | |
| * @param Varien_Event_Observer $observer | |
| */ | |
| public function catalog_product_collection_load_before($observer) | |
| { | |
| $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'); | |
| // Make sure on_top is the first order directive | |
| $order = $collection->getSelect()->getPart('order'); | |
| array_unshift($order, array_pop($order)); | |
| $collection->getSelect()->setPart('order', $order); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment