Sort Prestashop Features by Name

In this quick tip, we will see how to order Prestashop Features by name automatically, without having to manually adjust their position every time!

How to sort Prestashop Features by name

In order to change the features’ order we need to amend the method called getFrontFeaturesStatic, of the Product class. As its best practice, let’s use an override!

Go to override/classes and create a file named Product.php. Inside, add the following:

<br />class Product extends ProductCore<br />{<br />}<br />

Then, let’s have a look at the original getFrontFeaturesStatic, located in the code Product.php file, found in the classes/ folder.

<br /><br /><%%KEEPWHITESPACE%%>	public static function getFrontFeaturesStatic($id_lang, $id_product)<br /><%%KEEPWHITESPACE%%>	{<br /><%%KEEPWHITESPACE%%>		if (!Feature::isFeatureActive())<br /><%%KEEPWHITESPACE%%>			return array();<br /><%%KEEPWHITESPACE%%>		if (!array_key_exists($id_product.'-'.$id_lang, self::$_frontFeaturesCache))<br /><%%KEEPWHITESPACE%%>		{<br /><%%KEEPWHITESPACE%%>			self::$_frontFeaturesCache[$id_product.'-'.$id_lang] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('<br /><%%KEEPWHITESPACE%%>				SELECT name, value, pf.id_feature<br /><%%KEEPWHITESPACE%%>				FROM '._DB_PREFIX_.'feature_product pf<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				'.Shop::addSqlAssociation('feature', 'f').'<br /><%%KEEPWHITESPACE%%>				WHERE pf.id_product = '.(int)$id_product.'<br /><%%KEEPWHITESPACE%%>				ORDER BY f.position ASC'<br /><%%KEEPWHITESPACE%%>			);<br /><%%KEEPWHITESPACE%%>		}<br /><%%KEEPWHITESPACE%%>		return self::$_frontFeaturesCache[$id_product.'-'.$id_lang];<br /><%%KEEPWHITESPACE%%>	}<br />

Pay close attention to the ORDER BY clause. It currently says: f.position. We want to order by name, which is a column of the feature_lang table! Therefore, copy this whole method to the new override:

<br />class Product extends ProductCore<br />{<br /><br /><%%KEEPWHITESPACE%%>	public static function getFrontFeaturesStatic($id_lang, $id_product)<br /><%%KEEPWHITESPACE%%>	{<br /><%%KEEPWHITESPACE%%>		if (!Feature::isFeatureActive())<br /><%%KEEPWHITESPACE%%>			return array();<br /><%%KEEPWHITESPACE%%>		if (!array_key_exists($id_product.'-'.$id_lang, self::$_frontFeaturesCache))<br /><%%KEEPWHITESPACE%%>		{<br /><%%KEEPWHITESPACE%%>			self::$_frontFeaturesCache[$id_product.'-'.$id_lang] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('<br /><%%KEEPWHITESPACE%%>				SELECT name, value, pf.id_feature<br /><%%KEEPWHITESPACE%%>				FROM '._DB_PREFIX_.'feature_product pf<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				'.Shop::addSqlAssociation('feature', 'f').'<br /><%%KEEPWHITESPACE%%>				WHERE pf.id_product = '.(int)$id_product.'<br /><%%KEEPWHITESPACE%%>				ORDER BY f.position ASC'<br /><%%KEEPWHITESPACE%%>			);<br /><%%KEEPWHITESPACE%%>		}<br /><%%KEEPWHITESPACE%%>		return self::$_frontFeaturesCache[$id_product.'-'.$id_lang];<br /><%%KEEPWHITESPACE%%>	}<br /><br />}<br />

And change the ORDER BY clause as follows:

<br />ORDER BY fl.name ASC<br />

Done! If you are using a version newer than 1.5.3, go to the cache/ folder and delete the class_index.php file in order for the new changes to take place!

The final override:

<br />class Product extends ProductCore<br />{<br /><br /><%%KEEPWHITESPACE%%>	public static function getFrontFeaturesStatic($id_lang, $id_product)<br /><%%KEEPWHITESPACE%%>	{<br /><%%KEEPWHITESPACE%%>		if (!Feature::isFeatureActive())<br /><%%KEEPWHITESPACE%%>			return array();<br /><%%KEEPWHITESPACE%%>		if (!array_key_exists($id_product.'-'.$id_lang, self::$_frontFeaturesCache))<br /><%%KEEPWHITESPACE%%>		{<br /><%%KEEPWHITESPACE%%>			self::$_frontFeaturesCache[$id_product.'-'.$id_lang] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('<br /><%%KEEPWHITESPACE%%>				SELECT name, value, pf.id_feature<br /><%%KEEPWHITESPACE%%>				FROM '._DB_PREFIX_.'feature_product pf<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				LEFT JOIN '._DB_PREFIX_.'feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')<br /><%%KEEPWHITESPACE%%>				'.Shop::addSqlAssociation('feature', 'f').'<br /><%%KEEPWHITESPACE%%>				WHERE pf.id_product = '.(int)$id_product.'<br /><%%KEEPWHITESPACE%%>				ORDER BY fl.name ASC'<br /><%%KEEPWHITESPACE%%>			);<br /><%%KEEPWHITESPACE%%>		}<br /><%%KEEPWHITESPACE%%>		return self::$_frontFeaturesCache[$id_product.'-'.$id_lang];<br /><%%KEEPWHITESPACE%%>	}<br /><br />}<br />

Need Prestashop Modules? Have a look at my Prestashop Addons Store!

Looking for quality PrestaShop Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

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


7 × 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>