Skip to content

Instantly share code, notes, and snippets.

@rodrigowebjump
Created August 26, 2014 00:33
Show Gist options
  • Select an option

  • Save rodrigowebjump/da4ea2a7e2f6073c62d6 to your computer and use it in GitHub Desktop.

Select an option

Save rodrigowebjump/da4ea2a7e2f6073c62d6 to your computer and use it in GitHub Desktop.
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