3

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 ?

3
  • Have you found any solution to this? – Amrit Pal Singh Jan 12 at 9:50
  • @AmritPalSingh no i haven't find any solutions for it. – aravind Jan 12 at 12:24
  • This sounds like it is related to the officially confirmed bug #31043. – zigojacko Feb 4 at 12:53

Your Answer

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

Browse other questions tagged or ask your own question.