Kannan Naidu
Kannan Naidu

Reputation: 35

Calculating shipping cost based on distance and weight with php math formula

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 

Upvotes: 1

Views: 654

Answers (1)

KIKO Software
KIKO Software

Reputation: 16759

I came up with the function below.

function calc_rockrider_cost($meters, $grams) 
{
    // invalid weight?
    if ($grams > 30000) return false;
    // determine cost parameters
    $costPerKilometer = 0.00;
    if( $grams <= 10000 ) {
        $startingCost = 5.00;
        if ($meters > 10000 ) {
            $costPerKilometer = 0.80;
        }
        elseif ($meters > 5000 ) {
            $costPerKilometer = 1.00;
        }
    } else {
        $startingCost = 8.00;
        if ($meters > 15000 ) {
            $costPerKilometer = 1.50;
        }
        elseif ($meters > 5000 ) {
            $costPerKilometer = 1.00;
        }
    }    
    // calculate cost based on parameters
    return $startingCost + round($meters / 1000) * $costPerKilometer;
}

First of all I changed $weight to $grams to make it similar to the $meters parameter of the function. The alternative would be the parameters $distance and $weight.

Then I separated the calculation of the cost from determining the value of the cost parameters. This way the cost calculation is only done in one place, making easier to maintain the code.

Upvotes: 2

Related Questions