Changing default category sort order in Magento

SortBy

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.

36
Top

Care to rate this post?

Author

Ivan Galambos

Backend Developer

Ivan worked at Inchoo from February 2011 to November 2013 as a Backend Developer.

Other posts from this author

Discussion 36 Comments

Add Comment
  1. 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!

  2. Daniel

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

  3. Josh

    @Daniel: Yep, changed again.

  4. 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 ???

  5. dan

    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

  6. toniyecla

    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;
    }

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

  8. Great article.

    Do u know how we can sort by newly ordered products as default ?

    Thanks
    Prajosh

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

  10. Maier

    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?

  11. eggplanet(gowri)

    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

  12. 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!

  13. Jim

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

    Thanks

  14. Aknal

    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

  15. Jesse

    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? ;-)

  16. Rolandow

    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?

  17. Nice to see this post revamped!

  18. Josh Frank

    Is there any way to change the name of “best value” to something else?

  19. Rajeev

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

  20. 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!

  21. Tom

    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

  22. Tom

    Still useful today! Got there in the end, commerce bug really helped too.

  23. Ajitha

    very useful article.
    Thanks!

  24. How can I set to use ‘price’ in descending order not ascending.

  25. 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,

  26. jim

    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

  27. 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??

  28. 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-&gt;_getStore();
            $collection = Mage::getModel('catalog/product')-&gt;getCollection()
                -&gt;addAttributeToSelect('sku')
                -&gt;addAttributeToSelect('name')
                -&gt;addAttributeToSelect('attribute_set_id')
                -&gt;addAttributeToSelect('type_id')
    			-&gt;addAttributeToSelect('brand'); // attribute : brand
    		$collection-&gt;joinTable( 'cataloginventory/stock_item', 'product_id=entity_id', array(&quot;stock_status&quot; =&gt; &quot;is_in_stock&quot;, &quot;backorders&quot; =&gt; &quot;backorders&quot;) )
    		-&gt;addAttributeToSelect('stock_status');
    		if (Mage::helper('catalog')-&gt;isModuleEnabled('Mage_CatalogInventory')) {
                $collection-&gt;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')-&gt;getAttribute('catalog_product', 'brand');
    		$options =	array();
    					foreach( $attribute-&gt;getSource()-&gt;getAllOptions(true, true) as $option ) {
    					$options[$option['value']] = $option['label'];
    					}
    		$this-&gt;addColumn('brand',
    			array(
    				'header'=&gt; Mage::helper('catalog')-&gt;__('Brand'),
    				'width' =&gt; '80px',
    				'index' =&gt; 'brand',
    				'type'  =&gt; 'options',
    				'options' =&gt; $options,
    		));
    ...
    
  29. thank you so much

  30. jeri realubit

    Thank you so much for this great article. More power to http://inchoo.net/! ;)

  31. rodrigo v.

    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!

  32. 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!

  33. wow good article

  34. onkar

    Nice Post :)

  35. 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!

  36. Oh there it is. I thought I’m gonna modify something in the core code. Good thing it’s all in the magento admin. Haha!

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top