sorting carriers in Prestashop - Mon Oct 10, 2011

Prestashop 1.4 has some very customization friendly features. In particular the new concept of class overrides adds an immense amount of flexibility to the system without compromising the integrity of the core codebase.

As of this writing, Prestashop does not sort shipping carriers before displaying them to the customer . . . they will show up in the order they have been added to the system which may not be ideal. Changing this behaviour used to mean modifying core files but thanks to the override system, this is now possible without breaking upgradeability.

The following override class changes the default behaviour to instead sort the carriers by ascending price. To use it, save the file as "/overrides/classes/Carrier.php" in the root folder of your Prestashop install.

<?php class Carrier extends CarrierCore { public static function getCarriersForOrder($id_zone, $groups = NULL) { $carriers=array(); $result=parent::getCarriersForOrder($id_zone, $groups); if($result) { if(count($result)>0) { $sort=array(); foreach($result AS $k=>$v) { $sort['_'.$k]=$v['price']; } asort($sort,SORT_NUMERIC); foreach(array_keys($sort) AS $k) { $carriers[]=$result[(substr($k,1))]; } } } return $carriers; } } ?>

topics: PHP, Prestashop

blog

CODEgrunt consulting

Experienced PHP web developer.

$35 an hour, no job too small.

There is no replacement for integrity and experience.

Contact me for more information.

calendar

February 2012

Sun
Mon
Tue
Wed
Thu
Fri
Sat
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3