To create a custom freight function you define the function and call WTFreight::registerFreightFunction() from a PHP file within the code folder of a site.
The custom freight function will be passed an associative array containing the delivery details for the order as well as the total value and weight. It must either return the value of the freight or false if delivery cannot be made.
Example:
/*
$args contains: "postcode", "order value", "country", "weight", "quantity"
*/
function calculateFreight($args) {
// we do not deliver to postcode 5000
if($args["postcode"] == 5000) {
return false;
}
// overseas orders are $100
if($args["country"] != "Australia") {
return 100;
}
// free delivery for orders over $100
if($args["order value"] > 100) {
return 0;
}
// for orders under 1 kg it's $5.50 for postage
if($args["weight"] < 1000) {
return 5.5;
}
// otherwise it's $2 per item
return $args["quantity"] * 2;
}
WTFreight::registerFreightFunction("calculateFreight");
If your custom freight function needs to call the built in freight function, you can call WTFreight::getCharge(), passing $args as the first parameter and true as the second. true as the second parameter means "calculate freight ignoring the registered freight function".
function calculateFreight($args) {
// calculate freight using the inbuilt freight function
$freight = WTFreight::getCharge($args, true);
// do some calculation to the freight
$freight = $freight * 1.1;
return $freight;
}
WTFreight::registerFreightFunction("calculateFreight");