Thursday, September 8, 2011

Magento: Out of stock pushed to the end of the list

Magento allows sorting by Position, Price and Name in the toolbar.

But It does not look at the status of product and if the product is available. Under normal circumstances, I would not like to show any of the products which are not available but due to SEO reasons, it makes sense to continue showing the products. So I would like to continue showing the products based on one of the sorting method but pushing the out-of-stock product at the end.

To do this a good post is http://www.magentocommerce.com/boards/viewthread/203590/#t345652

A more elegant solution is to make changes in .../Mage/Catalog/Model/Layer.php at line #102
$collection->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id','is_in_stock>=0', 'left')->setOrder('inventory_in_stock', 'desc');

This will effect both the listing as well as the results page.

Another things is to remember to copy this file from core to code/local//Mage/Catalog/Model/Layer.php so that the core files remain as it is.

EDIT:
Ricardo Martins has made this into a github project and posted the code on
https://github.com/r-martins/Magento-OutOfStockLast 

I have not checked the code but it will be helpful for lot of folks. Thanks Ricardo

21 comments:

  1. it works fine but didn't work on price sort any clue

    ReplyDelete
    Replies
    1. In /Mage/Catalog/Model/Resource/Product/Collection.php at line #1553 add:
      $this->getSelect()->order("inventory_in_stock desc");

      Delete
  2. how can we make it to work on price ?

    ReplyDelete
  3. Does not work with bundle

    ReplyDelete
  4. In Bundle the inventory is choosen dynamically and hence does not work as it expects the attribute stock to be set.

    If you want to make bundles work, you will need to manage the bundle inventory manually, which will also painful.

    ReplyDelete
  5. hello

    thank you for the so useful post

    stock status for products is a very important part of ecommerce.
    to simplify manage products status
    http://amasty.com/custom-stock-status.html

    ReplyDelete
  6. Thank you for the useful post

    ReplyDelete
  7. but there is no catalog folder in app/code/local/mage

    ReplyDelete
    Replies
    1. The original catalog folder is in app/code/core//Mage
      In order to not break the system you are not required to make changes in there. But you can do same by copying the files in the same hierarchy to local folder from core

      Delete
  8. working for list page but not for search

    ReplyDelete
  9. I was in a search of this, actually I was thinking to modify the core files or adding core files in the directory but then realized that this would be a wrong direction to move on.

    ReplyDelete
  10. Dear,

    Thank you for this helply topic but how can we do this in search?

    Thanks in advance

    ReplyDelete
  11. Nice post, thanks. Check this out:

    http://www.magentocommerce.com/magento-connect/sort-products-by-stock-status-9821.html

    ReplyDelete
  12. Thanks for the post.
    I just created a module with this.
    https://github.com/r-martins/Magento-OutOfStockLast

    ReplyDelete
  13. This doesn't seem to work for grouped products, any thoughts on how to adjust this to accomplish this?

    ReplyDelete
  14. Hello. Very cool blog!! Guy.. Excellent.. Amazing.. I will bookmark your blog and take the feeds also... Thanks for one's marvelous posting! I actually enjoyed reading it, you happen to be a great author.I will ensure that I bookmark your blog and definitely will Picscheme - PIC Bonus

    ReplyDelete
  15. your website is not showing up correctly in my browser PIC Scheme

    ReplyDelete
  16. Thank you so much. Save my time. Works fine :)

    ReplyDelete
  17. Shared things are really very helpful and it will help to many people.

    ReplyDelete
  18. Oh, this is a very useful feature. I have been trying for several weeks to implement this feature on my website. The site is built on the basis of magento, on top of the installed store template https://www.templatemonster.com/ru/magento-themes-type/. I want to implement the filter function so that buyers can sort out their own lots as they please, but I still do not get it.

    ReplyDelete