Selective attribute sorting

Selective attribute sorting

When you have a large scale Magento store with thousands of products in every top-level category, dozens or even hundreds of offered filters for a single attribute in your layered navigation, certain necessities become mandatory – like selective attribute sorting.

Even though Magento does offer sorting functionality for attribute values, in these situations that just might not be enough as you can not sort selectively for a specific category.

For instance, let’s say a customer is in a “First Category” and there he sees a brands attribute in layered navigation, filterable by these values:

  • A & A Specialties (1)
  • Abella (2)
  • Abex Friction Products (1)
  • Absco Ltd (1)
  • AC-Delco (166)
  • Accel (1)
  • Acco Ind (1)

Then customer goes to a “Second Category” and again there is this brands attribute with exactly the same options offered to filter by.

Now, why wouldn’t you offer your customers filterable options in the order you find the most suitable, maybe boost sales by emphasizing a certain brand, or just sorted to be a bit more relevant to current category?

Here’s how you can do it with the least coding required.
First download the Inchoo_SAS extension and copy it’s content to your Magento’s root folder. This extension will handle all the back-end logic for you.

https://github.com/ivicatadic/Inchoo_SAS
or for download
Inchoo_SAS

(Remember to log out and then log back into admin after extension is installed.)

Now, for the fun part.
Let’s go to Magento admin -> CMS -> Static Blocks

In order to manipulate the attribute values for a particular category follow these steps:
1. Create a static block:
Static block name example: First Category – Layer Brands
Static block identifier example: first_category_layer_brands

2. Place the attribute values in the Content area separated by new line [enter]:
example:
BMW
Audi

Note:
It must be plain text and no html tags, so hide the WYSIWYG Content Editor.

3. In admin go to the relevant category – in our example it’s First Category
a) click on the “Custom Design” tab
b) Enter the following custom layout update:

<!-- -->
 
	<block type="cms/block" name="layer_brands">
		<action method="setBlockId">
			<block_id>first_category_layer_brands</block_id>
		</action>
	</block>
 
<!-- -->

Place this code outside / after any <reference name=”something”></reference> node.

Note:
The name=”layer_brands” serves as an example, you’ll use layer_actual_attribute_code
instead, like layer_manufacturer. The only thing that always stays the same is the layer_ prefix.

Inside <block_id></block_id> you’ll enter the identifier of the relevant static block (see No. 1)

4. Click on “Save Category”
5. Go to Cache management and hit the “Flush Magento Cash” in order to see the changes.

And that’s it 🙂
Whenever you need to alter the order of attribute values for a certain category, just go to the relevant static block and reorder values the way you like it.
Another big gain you get with this extension is that if you sort products by Position in this particular category, you’ll notice that Magento will sort products the way attribute values are sorted as well.

Finally, if you don’t like the required prefix layer, you may change it if you go to
Magento admin -> System -> Configuration -> Inchoo Extensions -> Layer Navigation – sorting

I hope this will prove to be helpful to you.

Cheers 🙂

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

How I learned to stop worrying and love 3rd party modules for Magento Luka Rajcevic
Luka Rajcevic, | 2

How I learned to stop worrying and love 3rd party modules for Magento

Filter order grid by multiple ID’s Petar Sambolek
Petar Sambolek, | 14

Filter order grid by multiple ID’s

Custom Product Option and its use-case in Magento Ivan Galambos
Ivan Galambos, | 25

Custom Product Option and its use-case in Magento

9 comments

  1. Thankyou for your code. I have modified your code for my requirement.
    My attribute listing must be in this strange order.
    10.5, 11, 11.5, 12, 12.5, 13, 13.5, 1, 1.5, 2, 2.5, 3

    when i added your code as instructed it listed in this order 13.5, 13, 12.5, 12, 11.5, 11, 10.5, 10, 3, 2, 1.

    so i modified your model/catalog/filter.attribute.php’s _getItemsData function

    // Do sorting
    $foundMatch = false;
    $result = array();
    foreach ($templateArr as $t) {
       foreach ($items as $item) {			
         if ($t == $item['label']) { 
    	$foundMatch = true;
    		array_unshift($result, $item);
    		}
    	}
    
    	if (!$foundMatch) { 
    		array_push($result, $item);
    	}
    	$foundMatch = false;
    }
    
    $result_final = array_reverse($result);
    
    return $result_final;

    Hope this will help someone 🙂

  2. $product = Mage::getModel(‘catalog/product’);
    $attributes = Mage::getResourceModel(‘eav/entity_attribute_collection’)
    ->setEntityTypeFilter($product->getResource()->getTypeId())
    ->addFieldToFilter(‘attribute_code’, ‘my_attribute’);

    how to sort “my attribute”

    thanks

  3. @Justin, missing just for a bit 🙂
    “…Even though Magento does offer sorting functionality for attribute values, in these situations that just might not be enough as you >>can not sort selectively for a specific category<<…"

    Magento's sorting functionality is applicable for ALL the categories, this extension gives you a possibility to sort products separately for a desired category.

    Hope this helps.

    @farid, you can get answers here, just ask the right question 🙂

    Cheers

  4. Isnt this functionality already available by giving the attribute a position number? Or am I missing the point of this article.

  5. Thanks Ivica for a good article. Can be helpful specially in enterprise magento having big product list.

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.