Stock Management – How to Show Products with Most Quantity First

stock

Prior to my store allowing back orders, when browsing all products that were "In Stock" were displayed with a higher priority over products that were "Out of stock".

Since allowing back orders on my website, now the logic for this functionality is flawed. Now products with 0 stock are mixed with products with stock making it difficult for visitors to find products that are available for immediate purchase.

What I would like to do is show products with the most quantity first and not just "In Stock"

I'm finding plenty of information about sorting products by "in stock" but nothing about product quantities.

What is the best method to achieve sorting products by the largest quantities first when browsing?

Best Answer

Here is what I got.
The following code works for simple products with manageable stock only. It all falls over if configurable, grouped or products for which you don't manage the stock are involved.
You will need a new extension, just like Richard also suggested that rewrites the product collection resource model.
Here is the full extension. Let's name it Easylife_Sorting.
You will need the following files:

app/etc/modules/Easylife_Sorting.xml - the declaration file

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

app/code/local/Easylife/Sorting/etc/config.xml - the configuration file:

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Sorting>
            <version>1.0.0</version>
        </Easylife_Sorting>
    </modules>
    <global>
        <models>
            <catalog_resource>
                <rewrite>
                    <product_collection>Easylife_Sorting_Model_Resource_Product_Collection</product_collection>
                </rewrite>
            </catalog_resource>
        </models>
    </global>
</config>

app/code/local/Easylife/Sorting/Model/Resource/Product/Collection.php - the rewritten collection model

<?php
class Easylife_Sorting_Model_Resource_Product_Collection
    extends Mage_Catalog_Model_Resource_Product_Collection {
    public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC){
        //don't screw up the admin sorting
        if (Mage::app()->getStore()->getId() == Mage_Core_Model_App::ADMIN_STORE_ID){
            return parent::addAttributeToSort($attribute, $dir);
        }
        //the stock sorting should apply only when sorting by position. Leave the price and name alone
        if ($attribute == 'position') {
            //join the stock status index table for the current website and check if the product is saleable and the qtu.
            $select = $this->getSelect();
            $select->joinLeft(
                array('stock_qty' => $this->getTable('cataloginventory/stock_status')),
                'e.entity_id = stock_qty.product_id AND stock_qty.website_id='.Mage::app()->getWebsite()->getId(),
                array(
                    'salable' => 'stock_qty.stock_status',
                    'qty' => 'stock_qty.qty'
                )
            );
            //get the reversed sorting
            //if by position ascending, then you need to sort by qty descending and the other way around
            $reverseDir = ($dir == 'ASC') ? 'DESC' : 'ASC';
            //this is optional - it shows the in stock products above the out of stock ones independent if sorting by position ascending or descending
            $this->getSelect()->order('salable DESC');
            //sort by qty
            $this->getSelect()->order('qty '.$reverseDir);
            return $this;
        }
        //if not sorting by position, let magento do its magic.
        return parent::addAttributeToSort($attribute, $dir);
    }
}
Related Topic