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
Experienced PHP web developer.
$35 an hour, no job too small.
There is no replacement for integrity and experience.