In the category products page I need to display the products in order which we select from the option drop down (price, position or name). At the same time I need to move Out Of Stock products last.
I override the Mage_Catalog_Block_Product_List_Toolbar
setCollection()
function and added below code:
$outOfStock = array();
foreach ($collection as $key => $item) {
$product = Mage::getModel('catalog/product')->load($key);
$inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIsInStock();
if ($inStock == 0) {
$outOfStock[] = $product;
$collection->removeItemByKey($key);
//Mage::log($inStock, null, 'sssssssssss.log');
}
}
// re-add $items
foreach ($outOfStock as $item) {
$collection->addItem($item);
}
Using this code all the Out Of Stock products are moving to the end of the collection. But the issue is after adding above code pagination is NOT working correctly. All the products display in one page.
Can anyone please help me. Thank You.