Prestashop Sort Attributes the Easy Way

Prestashop is a great piece of e-commerce, but one thing I with they would add is the ability to sort attributes in the backend for display on the front end of the web site. As we know, alphabetical order is not always the way you might want to display a list of product attributes on a product detail page. For example, a list of sizes like (s,m,l,xl) is best displayed in smaller to larger order, not alphabetical. There are several posts that require editing some of the core classes of Prestashop to sort the attributes by id, basically ordering them in the order they were put in the system.

The method I came up with was to add a function to smarty, the template processor and then sort the array in the product.tpl file. This article is what got me started:

http://www.smarty.net/forums/viewtopic.php?t=1079&postdays=0&postorder=asc&start=0

Here’s what I did:

  1. Created a file called modifier.sortby.php in tools/smarty/plugins/
  2. Place this code in the file
    <?php
    #
    # Modifier: sortby – allows arrays of named arrays to be sorted by a given field
    #
    function smarty_modifier_sortby($arrData,$sortfields) {
    ksort($arrData);
    return $arrData;
    }
    // You might need this for smarty to load the function, probably not
    //$smarty->register_modifier( “sortby”, “smarty_modifier_sortby” );
    ?>
  3. Use this foreach loop in your .tpl file – @sortby:”key you are sorting by here”
    {foreach from=$group.attributes|@sortby:”#id_attribute” key=id_attribute item=group_attribute}

Hope this helps someone!

This entry was posted in Prestashop Development. Bookmark the permalink.

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>