Mage Tech Support

Out Of Stock Products To The End Of The List In Magento Category Page

This useful article is to show how Magento out of stock products are to be listed in E-Commerce websites.
The leading E-Commerce sites such as Amazon, Flipkart, etc., are listing the products even if there is no stock in it, but listing it at the end of the listed items. Though, Magento is the market leader in E-Commerce platforms, this feature is not available with default Magento platform.

So we wanted to show the way to achieve the task with code (query). This is a working code, running successfully with our live projects. Please follow the below steps to achieve it.

Step 1 : Using your file explorer copy file Layer.php from app/code/core/Mage/Catalog/Model/ and paste to app/code/local/Mage/Catalog/Model/

Step 2 : Open the newly copied file i.e., app/code/local/Mage/Catalog/Model/Layer.php using your code editor such as Notepad or Sub Lime

Step 3 : Search for the function getProductCollection(), which needs to be inserted with a below query

$collection->getSelect()->joinLeft(array('stock_status' => $collection->getTable('cataloginventory/stock_status')),'e.entity_id = stock_status.product_id AND stock_status.website_id='.Mage::app()->getWebsite()->getId());
$collection->getSelect()->order('stock_status DESC');

This query to be inserted next to “$collection = $this->getCurrentCategory()->getProductCollection();” with appropriate comment

Step 4 : After inserting the query overridden function getProductCollection() should look like

public function getProductCollection()
{
	if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
		$collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
	} else {
		$collection = $this->getCurrentCategory()->getProductCollection();

/********************** QUERY TO DISPLAY OUT OF STOCK PRODUCTS TO THE END OF LIST IN CATEGORY PAGE ***********************/

		$collection->getSelect()->joinLeft(array('stock_status' => $collection->getTable('cataloginventory/stock_status')),'e.entity_id = stock_status.product_id AND stock_status.website_id='.Mage::app()->getWebsite()->getId());
		$collection->getSelect()->order('stock_status DESC');
		$this->prepareProductCollection($collection);
		$this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
	}
	return $collection;
}

You can reach us for any queries on Magento and we should be able to help you in resolving any kind of issues you might be having in your Magento E-Commerce portal.