Magento Expert Forum - Improve your Magento experience

Results 1 to 8 of 8

Sort products by sold quantity in Magento

  1. #1
    Moderator shunavi's Avatar
    Join Date
    Mar 2013
    Posts
    130
    Thanks
    10
    Thanked 29 Times in 15 Posts

    Thumbs up Sort products by sold quantity in Magento


    Magento by default comes with a few basic product sorting options such as sorting by product position, name or price. In this article you’ll learn how to sort products by how many times they have been sold.

    To do this we have to override some of Magento’s core files.
    Modifying core files is bad practice, so we’ll rather create our own module to accomplish the same functionality and remain upgradeable.

    I’ll assume you know how to set-up your module and create its corresponding file (app/etc/modules/Inchoo_Catalog.xml) so Magento recognizes our module.

    Now that we have our module ready and recognized by Magento, lets create our config.xml atInchoo/Catalog/etc/config.xml:

    app/code/local/Inchoo/Catalog/etc/config.xml


    HTML Code:
    			<config>
    
    			    <modules>
    
    			        <Inchoo_Catalog>
    
    			            <version>0.1.0</version>
    
    			        </Inchoo_Catalog>
    
    			    </modules>
    
    			    <global>
    
    			        <blocks>
    
    			            <catalog>
    
    			                <rewrite>
    
    			                    <product_list_toolbar>Inchoo_Catalog_Block_Product_List_Toolbar</product_list_toolbar>
    
    			                </rewrite>
    
    			            </catalog>
    
    			        </blocks>
    
    			        <models>
    
    			            <catalog>
    
    			                <rewrite>
    
    			                    <config>Inchoo_Catalog_Model_Config</config>
    
    			                </rewrite>
    
    			            </catalog>
    
    			            <catalog_resource>
    
    			                <rewrite>
    
    			                    <product_collection>Inchoo_Catalog_Model_Resource_Product_Collection</product_collection>
    
    			                </rewrite>
    
    			            </catalog_resource>
    
    			        </models>
    
    			    </global>
    
    			</config>
    As you might have noticed we are overriding the following three files:

    • Mage_Catalog_Block_Product_List_Toolbar
    • Mage_Catalog_Model_Config
    • Mage_Catalog_Model_Resource_Product_Collection



    Our app/code/local/Inchoo_Catalog_Block_Product_List_Toolbar should look like this:

    app/code/local/Inchoo/Catalog/Block/Product/List/Toolbar.php



    PHP Code:
                class Inchoo_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar

                
    {

                
        public function setCollection($collection)

                
        {

                
            parent::setCollection($collection);

                
            if ($this->getCurrentOrder()) {

                
                if($this->getCurrentOrder() == 'qty_ordered') {

                
                    $this->getCollection()->getSelect()

                
                         ->joinLeft(

                
                                array('sfoi' => $collection->getResource()->getTable('sales/order_item')),

                
                                 'e.entity_id = sfoi.product_id',

                
                                 array('qty_ordered' => 'SUM(sfoi.qty_ordered)')

                
                             )

                
                         ->group('e.entity_id')

                
                         ->order('qty_ordered ' $this->getCurrentDirection());

                
                } else {

                
                    $this->getCollection()

                
                         ->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->getSelect();

                
                }

                
            }

                
            return $this;

                
        }

                } 
    We are extending all the Mage_Catalog_Block_Product_List_Toolbar properties and methods but we are overriding the setCollection() method with our own.

    Our Inchoo_Catalog_Model_Config is actually very simple:

    app/code/local/Inchoo/Catalog/Model/Config.php



    PHP Code:
                class Inchoo_Catalog_Model_Config extends Mage_Catalog_Model_Config

                
    {

                
        public function getAttributeUsedForSortByArray()

                
        {

                
            return array_merge(

                
                parent::getAttributeUsedForSortByArray(),

                
                array('qty_ordered' => Mage::helper('catalog')->__('Sold quantity'))

                
            );

                
        }

                } 

    By now the sorting on the category page should actually work, but we have a little problem with pagination as it doesn’t display the correct amount of records found. We can fix this with the following code in Inchoo/Catalog/Model/Resource/Product/Collection.php:

    app/code/local/Inchoo/Catalog/Model/Resource/Product/Collection.php


    PHP Code:
    class Inchoo_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection

                
    {

                
        protected function _getSelectCountSql($select null$resetLeftJoins true)

                
        {

                
           $this->_renderFilters();

                
           $countSelect = (is_null($select)) ?

                
               $this->_getClearSelect() :

                
               $this->_buildClearSelect($select);

                
           if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {

                
               $countSelect->reset(Zend_Db_Select::GROUP);

                
           }

                
           $countSelect->columns('COUNT(DISTINCT e.entity_id)');

                
           if ($resetLeftJoins) {

                
               $countSelect->resetJoinLeft();

                
           }

                
           return $countSelect;

                
        }

                } 
    And that’s it. After taking 4 little steps our Magento site now is able to sort products by sold quantity.
    Hope you enjoyed!

    Source: inchoo

    View more threads in the same category:


  2. #2
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi, Great post there, thank you.

  3. #3
    Junior Member Cms_ideas's Avatar
    Join Date
    Jun 2014
    Location
    https://cmsideas.net
    Posts
    380
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default

    Your job can be resolved with Advanced Sorting Product List - Quickly Sorting Extension by Cmsideas

    DEMO: http://cmsideas.net/advanced-sorting-product-list.html

    Features:
    Advanced Sorting Product List gives customers eight new sorting options
    + Sorting by "New" - products’ creation dates

    When customers choose New in Sort by list, new products will be shown at the top of search result. Store owners can attract customers who often follow new fashion trends by using custom date attribute for managing the order of products appearing on the search result for this sorting option.

    + Sorting by “Biggest saving” - the best product offers:

    By using this sorting option, you can offer the best deals for customers. The larger the difference between the normal price and discount price of a product is, the higher the position of this product is on the search result.Customers always look for the best deals among the stores, so make use of this sorting option to show your special offers.

    + Sorting by “Best sellers” - product sales:

    Best seller products are always believed to be great products. Customers often think that if many people buy this product, it must be good. In addition, store owners can set period for which the sales are counted to be bestsellers.

    + Sorting by “Most viewed”- product views:

    This sorting option is based on the number of product views. It lets customers know the popularity of products. Store owners can set period for which the product views are counted.

    + Sorting by “Top rate” and “Review counts” product ratings and reviews:

    Rating and Review which are given for products by customers can make new customers trust more because when shopping, customers often look for objective information.

    + Sorting by “Now on wishlist”:

    When customers consider buying any products, they will want to know whether fellow customers wish to have those products or not. That will definitely affect on their final purchase decisions.

    + Sorting by “Quality”:

    This sorting option will display products based on their quantity in stock. The higher the number of products in stock is, the higher the position of the product is on the search result.

    DEMO | Advanced Sorting Product Lists | Magento Packages

    NOTE: From 13/2 to 30/4. Magento Packages now discount 50%- Comprehensive solutions for e-commerce website
    Magento Package 1- Only $142.5 for 5 extensions: Magento Full Page Cache, 20 + Actions Manage Products in Backend, Quick View Pro ,Magento Revolution Slider and Magento Daily Deal .

    Magento packages 2- ONLY $132.5 for Add Columns to Order Grid in Backend, Extended PDF Invoice, Magento Full Page Cache, Magento Advanced Sorting Product List and Magento Mobile Theme

    For more, click: here

  4. #4
    Junior Member paulsimmons's Avatar
    Join Date
    Apr 2014
    Location
    USA
    Posts
    213
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    here is advance product sorting extension you can install to get different sorting options for your product catalog. For more details - http://www.fmeaddons.com/magento/imp...extension.html

  5. #5
    Junior Member
    Join Date
    Jun 2015
    Posts
    607
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default

    Also refer at magesolutio store : http://www.magesolution.com/

  6. #6
    Junior Member
    Join Date
    Feb 2016
    Posts
    93
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Great Tips and Thanks for coding. But there are so many extension available in market for sorting products

  7. #7
    New member
    Join Date
    Mar 2016
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Really great i am happy to read this tips thanks for sharing ..

    Buzz Apps

  8. #8
    New member
    Join Date
    Mar 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't know if this fits in here. But I found this extension that helps determine Customers who viewed a specific product also viewed other products. It is a bit like Google nearby search. Helped me in selling some Christmas trees and lights along with it. Thought of sharing this.

    http://bit.ly/1pvEMvn

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •