I need products that are out of stock (!$_product->isSaleable()) to be displayed at the end of the category list in the catalog (frontend part)
Magento v 2.4.2
I need products that are out of stock (!$_product->isSaleable()) to be displayed at the end of the category list in the catalog (frontend part)
Magento v 2.4.2
The following solution should meet your needs:
app/code/ModuleName/Namespace/etc/di.xml
<type name="Magento\Catalog\Model\Layer">
<plugin name="catalog_model_layer_plugin" type="ModuleName\Namespace\Plugin\Catalog\Model\Layer"/>
</type>
app/code/ModuleName/Namespace/Plugin/Catalog/Model/Layer.php
<?php
namespace ModuleName\Namespace\Plugin\Catalog\Model;
class Layer
{
public function afterGetProductCollection(
\Magento\Catalog\Model\Layer $subject,
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
) {
$collection->getSelect()->order('is_salable DESC');
return $collection;
}
}
If the above solution doesn't work for you then you should print the collection query on the PLP/Category page to confirm it. This is necessary because, in certain cases, the collection is being replaced by other events or classes. We encountered the same issue at one of our stores and had to handle the collection at that time.