So, many of us might have been given task to sort products by custom product attributes i.e. in stock, popularity or any kinds of unusual sorts. I would like to give you clear view what needs to be done in this regard. For all of your kind information, you can do this many many ways and there are plenty resources available. But none shows how exactly eventually the original author of the problem managed to solve this task from start to finish.

Overview:

Product attribute types (Catalog Input Type for Store Owner) can be in the following format:

1. Text Field.

2. Text Area.

3. Date.

4. Yes/No.

5. Multiple Select.

6. Dropdown.

7. Price.

8. Media Image.

9. Fixed Product Tax.

Out of all of those Text Field, Yes/No, Dropdown are the most common ones that can be used to sort product listing.

So, first thing FIRST, we need to create a product attribute by Catalog > Attributes > Manage Attributes. Or if you prefer to create a module for this you need to create a setup script. Either way, all you need to ensure that your newly created product attribute has the following settings:

Attribute Code = in_stock

Catalog Input Type for Store Owner = Text Field

Input Validation for Store Owner = No

Used in Product Listing = Yes

Used for Sorting in Product Listing = Yes

and save the attribute and clear the cache. if require than re-index the data.

Remember:

The Input Validation for Store Owner field in Catalog > Attributes > Manage Attributes  means just a simple validation. It does not mean that the attribute type will be decimal. The value is still saved as varchar.

When attribute has the type “Text Field” or anything to do with “Text”, the sorting will work on strings.
So from the strings point of view 11 < 2. So we need to have the attribute as decimal.

We can also use letters a, b, c, d, e ….aa, ab….za, zb. if you want instead of numbers.

 

Solution:

We need to extend the following block class

/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

You can create a module to extend it or simply copy it to your local keeping the same folder structure. So, under the local path would be (if you haven’t created one):

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

Now replace the following from under setCollection method:

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

to

if ($this->getCurrentOrder()) {
  /*defines the sort option*/
  if(($this->getCurrentOrder()) == 'in_stock'){ 
    /*sort by in_stock (ascending)*/
    $this->_collection->addAttributeToSort(‘in_stock',’asc');
  } else if(($this->getCurrentOrder()) == 'another_attribute_code'){ 
    /*sort by another_attribute_code (descending)*/ 
   $this->_collection->addAttributeToSort('another_attribute_code’,'desc') 
  } else {
       $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
}
}

As you can see you can have more than one attribute using else if { }. Or to sort by two attributes at the same time use:

$this->_collection->addAttributeToSort(‘in_stock','asc')->addAttributeToSort(‘another_attribute_code','desc');

And don’t forget to assign this attribute to your preferred attribute set Catalog > Attributes > Attribute Sets. Finally, set some value for in_stock for different products i.e. 1, 2, 3.

Lastly, reindex and refresh cache on your Magento backend and your ready to test.

Cheers!

 

Categorized in: