Magento Expert Forum - Improve your Magento experience

Results 1 to 13 of 13

Sort products by sold quantity in Magento

  1. #1
    Moderator shunavi's Avatar
    Join Date
    Mar 2013
    Posts
    124
    Thanks
    9
    Thanked 26 Times in 17 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. The Following User Says Thank You to shunavi For This Useful Post:

    klifftech (17-12-2018)

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

    Default

    Hi, Great post there, thank you.

  4. #3
    Junior Member Cms_ideas's Avatar
    Join Date
    Jun 2014
    Location
    https://cmsideas.net
    Posts
    385
    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

  5. #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

  6. #5
    Junior Member Magento Nguyen's Avatar
    Join Date
    Jun 2015
    Posts
    958
    Thanks
    0
    Thanked 13 Times in 13 Posts

    Default

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

  7. #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

  8. #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

  9. #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

  10. #9
    Senior Member
    Join Date
    Aug 2018
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    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 file

  11. #10
    Junior Member
    Join Date
    Sep 2018
    Location
    United Kingdom
    Posts
    228
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    Adjusting center documents is awful practice, so we'll rather make our very own module to achieve a similar usefulness and stay upgradeable.
    I'll accept you realize how to set-up your module and make its comparing document (application/and so forth/modules/Inchoo_Catalog.xml) so Magento perceives our module.

  12. #11
    Junior Member
    Join Date
    Nov 2019
    Posts
    1,083
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default


    Hello, Flocks!
    I'd also like to introduce one more tool - Advanced Sorting Magento 2 Extension. This module simplifies the product search process and gives better user-experience to the customer.

    It's no new providing satisfactory user-interface to customers, increases more engagement. Advanced Sorting enables visitors to search for the right product in the easiest way.

    It allows the store owner to select multiple sort options from the backend and enable customers to sort products based on Best Sellers, Most Viewed, Biggest Savings, and many more.

    Name:  advanced-sorting-front-side.png
Views: 0
Size:  199.9 KB

    Highlighted Features-
    • Easy To Install & Manage
    • Enable/Disable From Backend
    • Select Multiple Sort Option
    • Set Default Product Listing Sort By
    • More User-Friendly Site For Customers


  13. #12
    Junior Member
    Join Date
    Sep 2018
    Location
    Oman, Muscat
    Posts
    2,084
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    Better Sorting augmentation upgrades client experience during shopping time fundamentally and further develops deals of online stores viably. 12 Product arranging alternatives

  14. #13
    Junior Member
    Join Date
    Jun 2016
    Location
    Bhavnagar, Gujarat, India
    Posts
    1,125
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    Hey,

    You can also do it without coding using Magento 2 Out of Stock at Last Extension. This extension helps you in sort of products which is sold out.

    Hope this helps you.

    Thank you.

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
  •