Changing default category sort order in Magento

Changing default category sort order in Magento

Category toolbar has many options. By default it shows how many items are in the category, you can choose how many products you wish to be displayed per page, you can change the listing type (List or Grid) and you may choose Sort Order. This “Sort Order” can be confusing. The default “Sort Order” is “Best Value”. What does it mean? How is the Best value determined? Can we change the default sort order?

What is “Best Value” field?

From official Magento Community User Guide 1.7:

page 75./85. “Product Listing Sort By: The products are sorted, by default, by the selected attribute. The Best Value products are featured products as determined by the store administrator; for more information, see “Creating Categories,” page 83. The customer can select from the other options on a per-page basis. You can configure attributes to be used for sorting using the Used for Sorting in Product Listing setting of an attribute. For details, see “Managing Product Attributes,” page 92.”

page 88./98. “3. In the Position column, specify a whole number that indicates the order in which the product is listed when the products are sorted by Best Value. The product with a Position value of 1 is listed first, 2 is listed second, and so on. If more than one product has the same value for Position, the product with the lowest (that is, closest to 1) ID number is listed first.”

When you go to Category page in Magento administration (Catalog/Manage Categories), you will see “Category Products” tab. From there, you will see the list of products that are associated to this category. The last column is “Position”. That is how “Best Value” is determined. So, best value is not something that is dynamically calculated. You can tailor it to your likings.

Magento Category Product List Position

Now after you’ve changed “Best Value” you can visit your Category page on frontend and you’ll see your new sorting in Product Listing (if your default sort is by Position).

There are two options you can use via Magento administration.

1. Setting default Sort Order for category

When you go to Category page in Magento administration (Catalog/Manage Categories), you’ll see “Display Settings” tab. From there you can modify “Available Product Listing Sort By” and “Default Product Listing Sort By” values.
Let’s modify “Default Product Listing Sort By”. If you deselect “Use Config Settings” and if you select “Price” for “Default Product Listing Sort By”, on frontend for specified category you’ll see that all items are now sorted by price ascending (growing upward). If you have Magento CE 1.7 with Sample Data you can modify sorting by Price for, i.e. Bedroom category. Additionally you can deselect “Use All Available Attributes” for “Available Product Listing Sort By” and if you check only few of them (“Best Value” and “Price”), on frontend you’ll be able to sort only by selected options. Now click “Save Category” button.
You should see something like this:

2. Setting default Sort Order for Store / Website / Default Config

When you go to System / Configuration / Catalog (left menu) / Frontend you’ll see “Product Listing Sort by” drop down menu. Now if you deselect “Use Default” you’ll select default sorting for specified view (Store/Website/Default). This will apply to all categories but notice that by override rule (fallback) this has lower priority than “Sort Order for category”.

Fallback for Magento sorting

When you visit some category page on frontend, this is how Magento choose “Default Sorting” for specific category page:

  1. look for Default Sort set in Category page in Magento administration, if not set then
  2. look for Default Sort set in configuration for Store, if not set then
  3. look for Default Sort set in configuration for Website, if not set then
  4. look for Default Sort set in configuration for Default Config, if not set then

Note that configuration could be in database, table core_config_data, or config.xml file (for sorting – app/code/core/Mage/Catalog/etc/config.xml around line 723. – position). And that’s why you have default sorting by Position (“Best Value”) for category view page.

Note! This article was revised on Nov 22, 2012. You will notice that some of the comments are older. This is because the original article was posted in 2009 by Tomislav Bilic.

If this or anything else regarding Magento development confuses you, we will gladly check out your site and offer technical insights on what to improve based on our detailed custom report. Feel free to get in touch!

Related Inchoo Services

You made it all the way down here so you must have enjoyed this post! You may also like:

Shell script for converting configurable to grouped products Tsvetan Stoychev
Tsvetan Stoychev, | 5

Shell script for converting configurable to grouped products

Large number of input variables in Magento Kresimir Banovic
, | 5

Large number of input variables in Magento

Programmatically create bundle products in Magento Petar Sambolek
Petar Sambolek, | 5

Programmatically create bundle products in Magento

51 comments

  1. i have created an attribute New Arrival.
    and in category page i need to sort products by new arrival
    anybody please help me with this

    1. Copy
      app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php
      to
      app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php (create a new directory if it is not exists).

      Open new file and look at these lines of code in around line 232

      REPLACE:

      if ($this->getCurrentOrder()) {
      $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
      }

      WITH THIS PIECE OF CODE:

      if ($this->getCurrentOrder()) {
      if (($this->getCurrentOrder()==’position’) && ($this->getCurrentDirection()==’asc’)) {
      $this->_collection->setOrder(‘entity_id’,’asc’);
      }
      elseif (($this->getCurrentOrder()==’position’) && ($this->getCurrentDirection()==’desc’)) {
      $this->_collection->setOrder(‘entity_id’,’desc’);
      }
      else {
      $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
      }
      }

      You can change the default order position desc or asc editing this:

      protected $_direction = ‘desc’;

  2. why not higher ID number is listed first, so The newly released products can automatically in the front

  3. How to access (in code) the “Default Product Listing Sort By” value that affects category product listing sort order in Toolbar.php? I have already looked at Mage_Catalog_Block_Product_List_Toolbar. However, I am unable to find my category specific values in getCurrentOrder(). Rather I am getting the “config settings”, which is “position”/”beat value”.

    1. Gotta love some quality marketing… The guy that works for an extension developer „just happens” to stumble over an extension developed by the very same developer. Hint: he has his profile homepage listed as the website of that developer.
      If Raj would have genuinely stated “there’s an extension for drag-drop sorting: “, I would have given it a chance. Now I’ll just look for another extension that does the same; I hate when someone is not being honest.

  4. You can also set the default sort order of your category in the local.xml of your theme:

    <catalog_category_default>
        <reference name="product_list">
            <action method="setDefaultDirection"><string>asc</string></action>
            <action method="setDefaultOrder"><string>position</string></action>
        </reference>
    </catalog_category_default>
  5. Dear Sir,
    how can i see product with store name on view order page in magento admin?
    can we purchase multiple product from different store in one time in magento?

  6. Hi, thanks for the great advice. I have attributes BestSellers, New Arrival, Most Viewed and New & Popular(Default). So i want to sort my products according to these attributes. So how it’s possible.

    Thank you

  7. Thank you! All I needed was to know was that I could number the position of my products on the “Category Products” tab. Easy! Thanks again.

  8. Thanks for the post!

    Is there a simple way to change the order the “Sort By” options are displayed?

    For example, I want it to be ordered:
    Name
    Price
    Best Selling (renamed ‘Position’)
    Model Number (set to Default in Configuration)

    But Magento is currently sorting it as
    Best Selling
    Name
    Model Number
    Price

    Thanks for the help!

  9. Hi. My category order is ok but when I change its order, how can it be reflected in homepage assuming the code for home is
    {{block type=”shoppersettings/product_list” category_id=”28″ num_products=”36″ title=…

    My category 28 is Vitrine in position sort (http://www.penelopeacessorios.com.br/vitrine.html) but when you compare to homepage products are the same but order aren’t. Cache and indexes are ok and didn´t change this inconsistency.

    Thanks!

  10. this doesn’t work in my magento 1.7.0.2 ce.
    it does only work when i change sort by new in the category design option, but due to large category catalog we have can’t do this manually….
    any ideas what else can we try? thanks!

  11. Dear all,i have modify manage products backend grid by adding 3 new columns: Brand (name of attribute), Stock Status, Back Orders. Everything are find. No problem with stock and back order. The problem is Brand column. When i short the grid by column Name (click header name of Name column), the value of Brand column is empty. Here my code:

    protected function _prepareCollection()
        {
            $store = $this->_getStore();
            $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('attribute_set_id')
                ->addAttributeToSelect('type_id')
    			->addAttributeToSelect('brand'); // attribute : brand 
    
    		$collection->joinTable( 'cataloginventory/stock_item', 'product_id=entity_id', array("stock_status" => "is_in_stock", "backorders" => "backorders") )
    		->addAttributeToSelect('stock_status');
    
    		if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
                $collection->joinField('qty',                
    				'cataloginventory/stock_item', // inventory : stock
                    'qty',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left');			
            }
    
    		....
     protected function _prepareColumns()
     ...
     $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand');
    		$options =	array();
    					foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
    					$options[$option['value']] = $option['label'];
    					}
    
    		$this->addColumn('brand',
    			array(
    				'header'=> Mage::helper('catalog')->__('Brand'),
    				'width' => '80px',
    				'index' => 'brand',
    				'type'  => 'options',
    				'options' => $options,
    		)); 
    ...
  12. Hi ,
    I sorted by pricing and all my products are with same price. But this items are changing its place for every visit. Can anybody help me??

  13. It is interesting and useful post. It is not a secret that sorting will help customers to orient in a web shop. I decided to simplify customers’ life and use http://amasty.com/improved-sorting.html magento extension.
    the extension helps me to use different types of sorting, which are suitable for different products

    thank you,
    Jim

  14. how to disable price calculator in drop down bar
    Hi, how do i disable price calculator for the drop-down bar (ie- +£4.50) or is there an extension to over ride this problem,

  15. Thanks for this article.

    And @Rajeev, you can change the name in Mage_Catalog.csv in your locale folder.
    Be aware however, that this will also change the name ‘position’ in the Magento backend (manage categories, tab category products) to your new name…

    Tom

  16. Hi Guys, my website is arranging items on the opposite, if I select Newest appear the older ones first, what file I need to edit to change this? Thank you!

  17. Thanks for sharing information for changing category sort order in Magento development. its quite informative article. I am impressed with article.

  18. I would like to order by position. Problem is that I cannot select this at the category config (only best value, name, price).

    When I view the page, it displays like it is being sorted by position by default, but in fact the “out of stock” items are at the end of the list. Since this is a “top 10 list” kind of list, I just want the position 1 at the top, no matter what the stock status is.

    When I explicitly set order=position&dir=asc to the URL, the sorting is fine. How to make this default?

  19. Wow really? There’s a field right there and you can’t just put a number in there and sort by the number? Did anyone at Magento ever actually run a store? Apparently not.

    As far as changing the oh-so-flexible sort values for the person above me, you OBVIOUSLY have to uncheck that box near the items so you can then select which one. Isn’t that totally obvious? 😉

  20. add below code below sort by foreach in toolbar.phtml
    <option value="getOrderUrl(‘price’, ‘asc’) ?>”isOrderCurrent(‘price’) && $this->getCurrentDirection() == ‘asc’): ?> selected=”selected”>
    Lowest Price

    <option value="getOrderUrl(‘price’, ‘desc’) ?>”isOrderCurrent(‘price’) && $this->getCurrentDirection() == ‘desc’): ?> selected=”selected”>
    Highest Price

  21. Is there a way to import product position using a CSV import?
    every product will have a position # within a category.

    Thanks

  22. I have 2 questions, we are just about to launch a new site and we did a bulk upload of products.
    1. I see on my category pages items are listed on pages 1,2 3 etc.. that I would like to switch with items on pages 12, 13, 14– does one need code for this or is this simple to do and how?
    2. How can one add in show ALL?
    Thanks you!

  23. Hello,
    I am having price option in sort select box.But want to make it into two types .
    1.Price:High to low(desc)
    2.Price:Low to high (asc)

    and also i would like to add my whishlist option in select list
    Any suggestion . please

    1. For make price like
      1.Price:High to low(desc)
      2.Price:Low to high (asc)

      Just Make a small change open this directory -> /app/design/frontend/default/ThemeName/template/catalog/product/list/toolbar.phtml file.

      search this lines

      <option value="getOrderUrl($_key, ‘asc’) ?>”isOrderCurrent($_key)): ?> selected=”selected”>
      __($_order) ?>


      and replace by below code

      <option value="getOrderUrl($_key, ‘asc’) ?>”isOrderCurrent($_key)): ?> selected=”selected”>
      __($_order) ?>

      <option value="getOrderUrl($_key, ‘asc’) ?>”isOrderCurrent($_key) && $this->getCurrentDirection() == ‘asc’): ?> selected=”selected”>
      __($_order) . ‘: Low to High’ ?>

      <option value="getOrderUrl($_key, ‘desc’) ?>”isOrderCurrent($_key) && $this->getCurrentDirection() == ‘desc’): ?> selected=”selected”>
      __($_order) . ‘: High to Low’ ?>

      enjoy

  24. How would I turn this into a module? It should be easy to do this, basically i want to make a module that tells the system to use the local code of Toolbar.php instead of the mage core one.

    Also does anyone know how to make it so no matter what products with qty < 1 are put in the back?

  25. Are you aware of any good extensions that provide more options in this area? Something that automatically calculated profitability per item and put the most profitable items first would be fantastic… with both price and cost as variables within the product info, you’d need to combine that info with a calculation of run rate and you’d be away.

    Oh, yes, if they could put out-of-stock items at the back then reintroduce them at their previous position when they’re back in stock that would be great! 🙂

    Might also be helpful to introduce new products at a specific place… so they get views and aren’t buried… and then let them float.

  26. For magento 1.4 chage line 119 ( /app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php )to:
    protected $_direction = ‘desc’;

  27. please try to comment this in app/code/core/Mage/Catalog/Model/Config.php:

    public function getAttributeUsedForSortByArray()
    {
    $options = array(
    // ‘position’ => Mage::helper(‘catalog’)->__(‘Position’)
    );
    foreach ($this->getAttributesUsedForSortBy() as $attribute) {
    /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
    $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
    }

    return $options;
    }

  28. The code for $this->_availableOrder isn’t there in 1.3.2.4. Instead, it sets it to simply ‘array()’, and $this->_availableOrder is set in the construct, by calling $this->_getConfig()->getAttributeUsedForSortByArray(); This hits the database for all attributes that are considered sortable, then builds a list based on that. It’s also not possible to override it as $this->_availableOrder is also ‘reset’ when Mage_Catalog_Block_Product_List kicks in (around line 150, there’s a $toolbar->setAvailableOrders). *sigh*
    Any clues how to get around this?
    Thanks

  29. in the last version we can set the default sort order between 3 deicded attribute best value name and price … that’s ok thank to the system config menu….. if we want to change the same about search result which is the best solution ??? which is exactly relevance ???

  30. Well that doesn’t seem to work for latest Magento version.
    My toolbar.php doesn’t have any of that sort of code 🙁

  31. That was great! Just changing the position number in the category products tab works perfect. This way I don’t have to be messing around with coding.

    Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.