I need to apply out-of-stock products at the end of the page.

How to set an order?

->add Attribute To Sort('is_in_stock', 'DESC')


->order('is_in_stock DESC')

DIR: MAGENTO\app\design\frontend\BASE\THEME\Magento_Catalog\templates\product\list.phtml

1 Answer 1


You can try this in your list.phtml

$currentCollection = $block->getLoadedProductCollection();
$inStockProducts = [];
$outOfStockProducts = [];

foreach ($currentCollection as $product) {
    if ($product->isSaleable()) {
        $inStockProducts[] = $product;
    } else {
        $outOfStockProducts[] = $product;

// Merge the arrays to put out-of-stock products at the end
$sortedProducts = array_merge($inStockProducts, $outOfStockProducts);

<!-- Rest of your list.phtml file content -->

<?php foreach ($sortedProducts as $_product) : ?>
    <!-- Your product listing code here -->
<?php endforeach; ?>
  • Perfect, it worked, thank you. Sep 27, 2023 at 14:44
  • When there is pagination, out of stock products are not on the last page, is there another way to fix this? Sep 27, 2023 at 16:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.