We have to display out of stock products at last in the list pages.
Let me if anybody have an idea.
Thanks
We have to display out of stock products at last in the list pages.
Let me if anybody have an idea.
Thanks
Try this solution:
$collection->getSelect()->joinLeft(
['_inventory_table' => 'cataloginventory_stock_item'],
"_inventory_table.product_id = e.entity_id", ['is_in_stock']
);
$collection->getSelect()->order(['is_in_stock desc']);
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->prepareProductCollection($collection);
$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"/>
</type>
</config>
Create Toolbar.php
file, File Path, app/code/Vishal/OutOfStock/Plugin/Product/ProductList/Toolbar.php
<?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 */
$subject->getSelect()->reset(\Zend_Db_Select::ORDER);
$subject->getSelect()->order($outOfStockOrderBy);
return [$printQuery, $logQuery];
}
}
Clear Cache and Check-in front. All the Out of stock products will display at end of listing page.