Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento in stock product first and out of stock products last in product or category page

One big problem with Magento is that when products become out of stock they still show up on top in the product listing and this can get very annoying for some users if there are several products out of stock. This very easy code fix will automatically make products that are out of stock show up last in all your categories and search results.

Go to the following folder in Magento where you will find List.php

/app/code/core/Mage/Catalog/Block/Product/List.php

If you don’t want to change the core files, which is always a bad habit then copy the file to
/app/code/local/Mage/Catalog/Block/Product/List.php

Find the following around line 86. 
 
$this->_productCollection = $layer->getProductCollection();

And change it to

$this->_productCollection = $layer->getProductCollection()
          ->joinField(
               'inventory_in_stock', 
               'cataloginventory_stock_item', 
               'is_in_stock', 
               'product_id=entity_id',
               'is_in_stock>=0', 
               'left')
            ->setOrder('inventory_in_stock','desc');

Now clear cache from admin and check your listing page.

Please support us, Like us on Facebook.

  1. Nice blog here! Also your site loads up fast! What web host are you using?
    Can I get your affiliate link to your host?
    I wish my website loaded up as quickly as yours lol

    Look into my web-site; videos

    ReplyDelete

 

Copyright @ 2017 HKBlog.