Jump to content

Cart Calculations


graham23s

Recommended Posts

Hi Guys,

 

I'm having  abrain fart lol what i am trying to do is add a charge of 0.75 to each item in a suers basket over the 2 item limit:

 

<?php
function calculateShipping($cusID, $productQT)
{
    //print "numQ " . $productQT;
    $qS               = mysql_query("SELECT `country` FROM `fcpv3_customers` WHERE `id`='$cusID'");
    $aS               = mysql_fetch_array($qS);
    $customersCountry = $aS['country'];
    
    $cutperItem = 0.75;
    
    // Add extra charges
    if ($productQT > 2)
    {
      $standardCharge = $standardCharge + $productQT * $cutperItem; 
    
    } else {
    
      $standardCharge = $standardCharge;    
    }
    
    if ($customersCountry == "Canada") 
    {
        $standardCharge = "19.99"; 
        return $standardCharge ;
    } elseif ($customersCountry == "Switzerland") {
        $standardCharge = "9.99";       
        return $standardCharge ;     
    } elseif ($customersCountry == "United Kingdom") {
        $standardCharge = "2.99"; 
        return $standardCharge ;  
    } elseif ($customersCountry == "United States") {
        $standardCharge = "24.99"; 
        return $standardCharge ;   
    } else {
        $standardCharge = "0.00"; 
        return $standardCharge ;    
    }
    
}
?>

 

If they have 2 items only the standard charge applies but for every item over 2 an additional charge of 0.75 is added per item, i can think of a way to code it.

 

any help would be appreciated

 

thanks guys

 

Graham

Link to comment
Share on other sites

Your resetting the variable at the bottom, so no matter what calculations you do, the bottom overrides it.

if ($customersCountry == "Canada") 
    {
        $standardCharge = "19.99"; 
        return $standardCharge ;
    } elseif ($customersCountry == "Switzerland") {
        $standardCharge = "9.99";       
        return $standardCharge ;     
    } elseif ($customersCountry == "United Kingdom") {
        $standardCharge = "2.99"; 
        return $standardCharge ;  
    } elseif ($customersCountry == "United States") {
        $standardCharge = "24.99"; 
        return $standardCharge ;   
    } else {
        $standardCharge = "0.00"; 
        return $standardCharge ;    
    }

 

As for the code, I would just do

$addCharge=0;

if ($qty > 2) {

  $addCharge=($qty-2)*0.75;

}

 

Link to comment
Share on other sites

I re-wrote your script to make it more flexible:

 

function findCountryByCustomerId($customerID)
{
  $query = "SELECT country FROM fcpv3_customers WHERE id = $customerID";
  $data = queryService($query);
  return $data['country'];
}

function findStandardChargeByCountry($country)
{
  switch ($country) {
    case 'Canada': return 19.99;
    case 'Switzerland': return 9.99;
    case 'United Kingdom': return 2.99;
    case 'United States': return 24.99;
    default: return 0.00;
  }
}

function appliesForExtraCharge($productQuantity)
{
  return $productQuantity > 2;
}

function calculateExtraCharge($standardCharge, $productQuantity, $cutPerItem)
{
  return $standardCharge + $productQuantity * $cutPerItem;
}

function calculateShipping($customerID, $productQuantity)
{
  $country = findCountryByCustomerId($customerID);
  $standardCharge = findStandardChargeByCountry($country);
  
  if (appliesForExtraCharge($productQuantity)) {
    $cutPerItem = 0.75;
    $standardCharge = calculateExtraCharge($standardCharge, $productQuantity, $cutPerItem);
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.