4

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 ?

5
  • Have you found any solution to this? Jan 12, 2021 at 9:50
  • @AmritPalSingh no i haven't find any solutions for it.
    – aravind
    Jan 12, 2021 at 12:24
  • This sounds like it is related to the officially confirmed bug #31043.
    – zigojacko
    Feb 4, 2021 at 12:53
  • Any solution for this? I am also interested into this
    – andy1786
    Jul 27, 2022 at 12:49
  • @aravind Bother, Have you found any solution for this? Dec 20, 2022 at 5:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.