Jump to content

Complicated Shipping Calculation


WorldDrknss

Recommended Posts

I have a list of id's between 1 - 8 which represents a print size that I need to use to calculate shipping costs, but the calculation that I need to perform is complicated. I will try to explain to the best of my abilities. I also need to query a database table to make sure the shipping rates are always up-to-date

 

id's <= 4 will ship for $4.95 now matter how many id's are listed. Eg:

1,2,3,4,3,2,4,2,4 will ship for a total of $4.95

will query price db as 1

 

id's <= 5 will ship for $6.00 now mater how many id's are listed. Eg:

1,3,3,1,2,3,5,5,5,4,3,3 will ship for a total of $6

will query price db as 2

 

id's <= 6 will ship for $8.00 now mater how many id's are listed. Eg:

1,3,6,1,6,3,5,5,5,4,3,3 will ship for a total of $8.00

will query price db as 3

 

id 7 will ship for $5 * Quanity + any of the examples above

will query price db as 4

 

id 8 will ship for $15 * Quantity + any of the examples above

will query price db as 5

 

table structure for shiprates is

id, price

Link to comment
https://forums.phpfreaks.com/topic/229748-complicated-shipping-calculation/
Share on other sites

Made something rough. Hope it helps.  :)

 

<?php
$maxId = 0;
$id8Count

foreach ($item as $items)
{

    if ($item['id'] > $maxId)
    {
        if ($item['id'] == 
        {
            $id8Count++;
        }
        
        $maxId = $item['id'];
    }
}

if ($maxid == 4)
{
    $price = "4.95";
}
elseif($maxId == 5)
{
    $price = "6.00";
}

$price = $price + 15 * $id8Count;

?>

There's probably a much nicer solution to this but I'm hungover, so this will have to do for now.

 

<?php
/* Query to get rates from database goes here, I'm just using hard coded array */
$rates = array(1 => '4.95', 2 => '6.00', 3 => '8.00', 4 => '5.00', 5 => '15.00');

/* Sample array of id's */
$products = array(1,2,3,4,4,4,5,6,8,;

/* Build an array of id's keyed on id with a count as the value */
foreach ($products as $id){
   if (isset($counts[$id])){
      $counts[$id]++;
   }
   else {
      $counts[$id] = 1;
   }
}

/* Calculate shipping */
$shipping = 0;
$flat_rate = 0;
if (isset($counts[8])){
   $shipping = $rates[5] * $counts[8];
}
if (isset($counts[7])){
   $shipping = $shipping + ($rates[4] * $counts[7]);
}
if (isset($counts[4]) || isset($counts[3]) || isset($counts[2]) || isset($counts[1])){
   $flat_rate = $rates[1];
}
if (isset($counts[5])){
   $flat_rate = $rates[2];
}
if (isset($counts[6])){
   $flat_rate = $rates[3];
}

$shipping = $shipping + $flat_rate;

echo $shipping;
?>

Aww gee, ok another way...

<?PHP
$haystack = "1,3,6,1,6,3,5,5,5,4,3,3,8,3,2,6,7,4,2,5,8,8,7,3,6,6,7";
$ship_8 = substr_count($haystack,"8") * 15.00;
$ship_7 = substr_count($haystack,"7") * 5.00;
if(substr_count($haystack,"1")>0 OR substr_count($haystack,"2")>0 OR substr_count($haystack,"3")>0 OR substr_count($haystack,"3")>0) { $base_ship = 4.95; }
if(substr_count($haystack,"5")>0) { $base_ship = 6.00; }
if(substr_count($haystack,"6")>0) { $base_ship = 8.00; }
$total_ship = $base_ship + $ship_7 + $ship_8;
echo "base ship = " . $base_ship . "<br>";
echo "7 ship = " . $ship_7 . "<br>";
echo "8 ship = " . $ship_8 . "<br>";
echo $total_ship;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.