I need to display Out of Stock products at end of the Listing in category page. I have overided the SetCollection method in Toolbar.php using around method(upto magento 2.3.5 without elasticsearch it worked fine). But it is not working with Elastic Search.
<?php
namespace Vendor\Module\Plugin\Product\ProductList;
class Toolbar
{
/**
* Around Plugin to set the order collection based on sort type
*
* @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
* @param \Closure $proceed
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$currentDirection = $subject->getCurrentDirection();
$result = $proceed($collection);
$this->_collection = $collection;
/* To Move out of stock product at the end */
$orderBy = $subject->getCollection()->getSelect()->getPart(\Zend_Db_Select::ORDER);
$outOfStockOrderBy = array('is_salable DESC');
$subject->getCollection()->getSelect()->reset(\Zend_Db_Select::ORDER);
$subject->getCollection()->getSelect()->order($outOfStockOrderBy);
}
//print_r($subject->getCollection()->getSelect()->getPart(\Zend_Db_Select::ORDER));die;
return $result;
}
}
Is there any other way to achieve that ?