1

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?

3
  • 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

1

You need to create a simple module, say Rave_MoveOutOfStock:

app/etc/modules/Rave_MoveOutOfStock.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Rave_MoveOutOfStock>
      <active>true</active>
      <codePool>local</codePool>
      <depends>
         <Mage_Catalog/>
      </depends>
    </Rave_MoveOutOfStock>
  </modules>
</config>

app/code/local/Rave/MoveOutOfStock/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Rave_MoveOutOfStock>
      <version>1.0</version>
    </Rave_MoveOutOfStock>
  </modules>
   <global>
    <models>
      <moveoutofstock>
        <class>Rave_MoveOutOfStock_Model</class>
      </moveoutofstock>
    </models>
  </global>
  <frontend>
        <events>
            <catalog_product_collection_load_before>
                <observers>
                    <review>
                        <type>model</type>
                        <class>Rave_MoveOutOfStock_Model_Observer</class>
                        <method>catalogProductCollectionLoadBefore</method>
                    </review>
                </observers>
            </catalog_product_collection_load_before>
        </events>
    </frontend>
 </config>

app/code/local/Rave/MoveOutOfStock/Model/Observer.php

<?php
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();

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

            $products->getSelect()->reset('order');
            $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

5
  • 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
0

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

https://stackoverflow.com/questions/15879978/magento-out-of-stock-products-showing-last-in-the-category-page

1
  • 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.