I want to products on product list will always be sorted from salable to not salable. Products which have "Add to cart" button should be show firstly(It should work for simple and configurable products)
I tried created plugin as is written in [https://magento.stackexchange.com/questions/178061/magento2-sort-products-and-search-result-by-stock-availability][1] :
//di.xml
<type name="Magento\Catalog\Model\Layer">
<plugin name="product_sort_by_stock" type="Company\Module\Model\Plugin\Layer"/>
</type>
//app/code/Company/Module/Model/Plugin/Layer.php
<?php declare(strict_types=1);
namespace Company\Module\Model\Plugin;
class Layer
{
public function aroundgetProductCollection(\Magento\Catalog\Model\Layer $subject, \Closure $proceed)
{
$selectedDirection = 'DESC';
$collection = $proceed();
$collection->getSelect()->order('is_salable '. $selectedDirection);
return $collection;
}
}
But it not works for me. Products are not sorted in a correct order.
Could you advice me how to sort product collection by is_salable
attribute? I have Magento 2.4.0 and Elasticsearch 7.9.2. I use multi source inventory.
[1]: Magento2 sort products and search result by stock availability