We have to display out of stock products at last in the list pages.

Let me if anybody have an idea.


4 Answers 4


Try this solution:

         ['_inventory_table' => 'cataloginventory_stock_item'], 
         "_inventory_table.product_id = e.entity_id", ['is_in_stock']
  $collection->getSelect()->order(['is_in_stock desc']);
  • sure, let me know if any issue Jun 6, 2018 at 11:06
  • Ok sure I will inform Jun 6, 2018 at 11:07
  • Hi Nikunj which file exactly we need to edit Jun 6, 2018 at 11:14
  • Where i have to add the above code Jun 6, 2018 at 11:14
  • Hi @ Nikunj iam editing layer.php file, Jun 6, 2018 at 11:31

just happened upon this thread... If anyone's still having issues and getting the _inventory_table correlation error using the aforementioned solution, try this in the /vendor/magento/module-catalog/Model/Layer.php file.

For me, there's already a join with the cataloginventory_stock_status table. Tested in 2.1.7 only.

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

        $collection->getSelect()->order(['stock_status DESC']); //ADD THIS

    } else {
        $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
        $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;

    return $collection;

Let me know if this works out for ya!


How to display out of stock products at the end of the list page in Magento 2 :-

You can create a custom module or add this code in the existing module for display out of stock product at the end of the page.

Here, I am creating a custom module. so first Create registration.php and module.xml file for creating a module. I hope you have created both files for define module,

For Collection of product generate from Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection file. We need to create a plugin for the load() function. we need to set our custom condition for out of stock product to beforeLoad() function.

Create a di.xml file,

Path, app/code/Vishal/OutOfStock/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!--  out of stock product at end of list -->
    <type name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection">
        <plugin name="Vishal_OutOfStock::OutofstockEnd" type="Vishal\OutOfStock\Plugin\Product\ProductList\Toolbar"/>

Create Toolbar.php file, File Path, app/code/Vishal/OutOfStock/Plugin/Product/ProductList/Toolbar.php

 * @author Vishal Thakur
 * @package Vishal_OutOfStock

namespace Vishal\OutOfStock\Plugin\Product\ProductList;

class Toolbar
     * @param \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $subject
     * @param bool $printQuery
     * @param bool $logQuery
     * @return array
    public function beforeLoad(\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $subject, $printQuery = false, $logQuery = false)
        $orderBy = $subject->getSelect()->getPart(\Zend_Db_Select::ORDER);
        $outOfStockOrderBy = array('is_salable DESC');
        /* reset default product collection filter */

        return [$printQuery, $logQuery];

Clear Cache and Check-in front. All the Out of stock products will display at end of listing page.


You can use this free extension to show out of stock items at the end of the catalog.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.