graham23s Posted May 29, 2010 Share Posted May 29, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203298-cart-calculations/ Share on other sites More sharing options...
teynon Posted May 29, 2010 Share Posted May 29, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/203298-cart-calculations/#findComment-1065127 Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/203298-cart-calculations/#findComment-1065157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.