Is there a way to push out of stock items to the bottom of the product page?
You need to create a module to display the out-of-products at bottom of the page.
Create registration.php file and module.xml file to create the 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 products to beforeLoad() function
Create a di.xml file in the following path
app/code/companyName/moduleName/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 products at bottom of the page --> <type name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection"> <plugin name="CompanyName_OutOfStock::OutofstockEnd" type="CompanyName\ModuleName\Plugin\Product\ProductList\Toolbar"/> </type> </config>
Create Toolbar.php file in the following path app/code/CompanyName/ModuleName/Plugin/Product/ProductList/Toolbar.php
<?php namespace CompanyName\ModuleName\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 */ $subject->getSelect()->reset(\Zend_Db_Select::ORDER); $subject->getSelect()->order($outOfStockOrderBy); return [$printQuery, $logQuery]; } }
And then clear the cache and check in the front end, all out of stock products will display at bottom of the page.
you can also follow the below link
https://www.rakeshjesadiya.com/magento-2-display-out-of-stock-products-at-the-end-of-the-list-page
Thanks.