I have added the code below with the shipping and price mechanism in comments, i believe the last elseif statement is not correct and i may be doing the whole calculation wrongly. Any help in correcting this or pointing to the correct method is highly appreciated. Thank you.
<?php
// Shipping mechanism for under 10kg
// 0 - 5 km - Price $5.00
// 5 - 10 km - Price $1.00 / per km
// > 10 km - Price $0.80 / per km
// Shipping mechanism for over 10kg and under 30kg
// 0 - 5 km - Price $8.00
// 6 - 15 km - Price $1.00 / per km
// > 16 km - Price $1.50 / per km
function calc_rockrider_cost($meters, $weight) {
// $meters is coming from another calc_distance function using google api
$km = round($meters/1000);
$cost = NULL;
if( $weight <= 10000 ) {
if( $km <= 5.0 ) {
$cost = 5.00;
}
elseif( $km > 5.0 && $km <= 10.0) {
$cost = 5.00 + (1.0*($km - 5.0));
}
elseif ( $km > 5.0 && $km > 10.0) {
$cost = 5.00 + (0.8*($km - 10.0)); <- mistake ?
}
} else {
if( $km <= 5.0 ) {
$cost = 8.00;
}
elseif( $km > 5.0 && $km <= 15.0) {
$cost = 8.00 + (1.0*($km - 5.0));
}
elseif ( $km > 5.0 && $km > 16.0) {
$cost = 8.00 + (1.5*($km - 15.0)); <- mistake ?
}
}
// need and else here for more than 30kg
return $cost; // get costing based on distance and weight
}
function shipping_calc_init(){
//$distance from calc_distance function using google api
//900 is weight ( 900 grams )
$price = calc_rockrider_cost($distance, 900);
echo 'Rock Rider price ' . number_format(round($price), 2)."<br>";
echo 'Distance: '.((int)$distance / 1000).' Km <br>';
echo 'Weight: 900gm ';
}
shipping_calc_init();
// when running this, i get
// Rock Rider price 16.00 <- not right
// Distance: 24.015 Km
// Weight: 900gm