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 VendorModulePluginProductProductList;
class Toolbar
{
/**
* Around Plugin to set the order collection based on sort type
*
* @param MagentoCatalogBlockProductProductListToolbar $subject
* @param Closure $proceed
* @param MagentoFrameworkDataCollection $collection
* @return MagentoCatalogBlockProductProductListToolbar
*/
public function aroundSetCollection(
MagentoCatalogBlockProductProductListToolbar $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 fix that ?