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


03 May 2017

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

Posted By : Admin Blog, Magento,
The shopping experience has gone better and better with more and more eCommerce sites. Magentois the market leader among E-Commerce platform. 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. However, this feature is not available with default Magento platform as out of stock products should be pushed to the end of the list. Here we are giving you 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->_productCollections[$this->getCurrentCategory()->getId()] = $collection;


return $collection;


Share this Article on

Tags: magento,

© 2007 - Synamen Thinklabs Pvt Ltd. All rights reserved Privacy Policy