I want the products to be displayed on the category page, if available
That is, first those that are stocks , then those that are not stocks
I didn't find anything with the title in the attribute
Is there any code?

  • do you want to show in stock product first and out of stock in the end ?
    – Pawan
    Apr 1, 2020 at 3:15
  • @Pawan Yes in want to show stock product first Apr 1, 2020 at 7:24
  • please see my answer and let me know if works.
    – Pawan
    Apr 1, 2020 at 8:08

2 Answers 2


You need to create a simple module, say Rave_MoveOutOfStock:


<?xml version="1.0"?>


<?xml version="1.0"?>


class Rave_MoveOutOfStock_Model_Observer extends Varien_Event_Observer
    public function catalogProductCollectionLoadBefore($observer)
        $toolbar = Mage::getBlockSingleton('catalog/product_list_toolbar');
        if ($toolbar) {
            $products = $observer->getEvent()->getCollection();

            $stockId = Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID;
            $websiteId = Mage::app()->getStore($products->getStoreId())->getWebsiteId();

                array('_inv' => $products->getResource()->getTable('cataloginventory/stock_status')),
                "_inv.product_id = e.entity_id and _inv.website_id=$websiteId and _inv.stock_id=$stockId",
            $products->addExpressionAttributeToSelect('in_stock', 'IFNULL(_inv.stock_status,0)', array());

            $products->getSelect()->order('in_stock DESC');

            if ($toolbar->getCurrentOrder()) {
                $products->addAttributeToSort($toolbar->getCurrentOrder(), $toolbar->getCurrentDirection());

        return $this;

Reference: Move out of stock products to the end of the catalog product list

  • Thank you very much I created all the files and folders and cleared the cache Then I refreshed the page and it was fine Now I have a question Doesn't this have a security problem? Or doesn't it? Apr 1, 2020 at 9:58
  • glad to know it worked. why do you think it has security issue ?
    – Pawan
    Apr 1, 2020 at 10:09
  • I only asked one question Apr 1, 2020 at 10:12
  • oh, as per my knowledge no security issue with above code
    – Pawan
    Apr 1, 2020 at 10:13
  • Thank you very much Apr 1, 2020 at 10:53

please check this solution for out of stock product last display :


  • this not work for my magento Apr 1, 2020 at 7: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.