WStudio Posted June 24, 2011 Share Posted June 24, 2011 Hi, I've been working on this shipment calculator script for some time now. It's works fine as it is except for one thing. I have been trying to figure out how to get the script to calculate the freight cost from just the weight or volume (length, width & height) fields. The way that it is now is that you have to enter a product cost and quantity for it to calculate anything. Some people just want the freight charges and nothing else. The classes: I know it's not very tidy, but I plan on cleaning it up once I get it working properly. <?php class ShippingCalculator { // Defaults var $postUrl; var $settings; var $weight = 1; var $weight_unit = "lb"; var $size_length = 4; var $size_width = 8; var $size_height = 2; var $size_unit = "in"; var $debug = false; // Change to true to see XML sent and recieved var $tax; var $minInsurance; var $brokerageFee; var $weightRates; var $volumeRates; var $volume; function ShippingCalculator(){ global $settings; $this->tax = 17.5; $this->minInsurance = 15.00; $this->brokerageFee = 10.00; $this->postUrl = $settings['siteurl_content'].'ajax.php'; } // Convert Weight function convert_weight($weight,$old_unit,$new_unit) { $units['oz'] = 1; $units['lb'] = 0.0625; $units['gram'] = 28.3495231; $units['kg'] = 0.0283495231; // Convert to Ounces (if not already) if($old_unit != "oz") $weight = $weight / $units[$old_unit]; // Convert to New Unit $weight = $weight * $units[$new_unit]; // Minimum Weight if($weight < .1) $weight = .1; // Return New Weight return round($weight,2); } // Convert Size function convert_size($size,$old_unit,$new_unit) { $units['in'] = 1; $units['cm'] = 2.54; $units['feet'] = 0.083333; // Convert to Inches (if not already) if($old_unit != "in") $size = $size / $units[$old_unit]; // Convert to New Unit $size = $size * $units[$new_unit]; // Minimum Size if($size < .1) $size = .1; // Return New Size return round($size,2); } // Weight Rates function getWeightRate($weight){ if($weight <= 6 ){ $rate = 25.00; $wTotal = $rate; }elseif($weight > 6 && $weight <= 24 ){ $rate = 4.00; $wTotal = $weight * $rate; }elseif($weight > 24 && $weight <= 100){ $rate = 3.50; $wTotal = $weight * $rate; }elseif($weight > 100){ $rate = 3.00; $wTotal = $weight * $rate; } return $wTotal; } // Calculate volume of item // Since measurements are going to be entered in inches, we have to convert it into cubic feet. function totalVolume($length, $width, $height){ $cubicft = 1728; // 1 cubic foot = inches (12x12x12) $involume = $length * $width * $height; // Volume calculated in cubic inches $volume = $involume / $cubicft; // Converting cubic inches to cubic feet return $volume; } function calculateVolume($length, $width, $height){ $cubicft = 1728; // 1 cubic foot = inches (12x12x12) $involume = $length * $width * $height; // Volume calculated in cubic inches $volume = $involume / $cubicft; // Converting cubic inches to cubic feet return $volume; } // Volume Rates function getVolumeRate($volume){ if($volume <= 2 ){ $rate = 15.00; $vTotal = $rate; }elseif($volume > 2 && $volume <= 40){ $rate = 8.00; $vTotal = $volume * $rate; }elseif($volume > 40 && $volume <= 60){ $rate = 7.00; $vTotal = $volume * $rate; }elseif($volume > 60 && $volume <= 100){ $rate = 6.00; $vTotal = $volume * $rate; }elseif($volume > 100){ $rate = 5.00; $vTotal = $volume * $rate; } return $vTotal; } function totalProductCost($product, $shipping, $qty){ $results = array(); if(isset($_POST['insurance']) == 'true'){ $tenpercent = (10 / 100); // Turn 10% into .10. $onepercent = (1.1 / 100); // Turn 1.1% into .1. $productCost = $product * $qty; $total = $productCost + $shipping; $insuranceA = $productCost * $tenpercent; // Insurance A - 10% of Product Cost $insuranceB = $productCost + $insuranceA; // Insurance B - Adding the 10% of Product Cost back to the Product Cost $insurance = $insuranceB * $onepercent; // Insurance - Working out the 1.1% of the Product Cost plus the 10% if($insurance <= $this->minInsurance){ $insuranceCost = number_format($this->minInsurance, 2); } elseif($insurance >= $this->minInsurance) { $insuranceCost = number_format($insurance, 2); } $Gtotal = $total + $insuranceCost + number_format($this->brokerageFee, 2); $results['grandtotal'] = number_format($Gtotal, 2); } else { $productCost = $product * $qty; $total = $productCost + $shipping; $Gtotal = $total + $this->brokerageFee; $results['grandtotal'] = number_format($Gtotal, 2); } $results['insurance'] = $insuranceCost; $results['subtotal'] = $total; $results['productcost'] = number_format($productCost, 2); $results['freightcost'] = number_format($shipping, 2); $results['brokeragefee'] = number_format($this->brokerageFee, 2); return $results; } function shippingCost($product, $shipping, $qty){ $results = array(); $productCost = $product * $qty; $total = $productCost + $shipping; $results['productcost'] = number_format($productCost, 2); $results['freightcost'] = number_format($shipping, 2); return $results; } function calculate_shipping(){ // Check for form submission: if (isset($_POST['submitted']) && isset($_POST['formKey']) && $_POST['formKey'] == 'shippingcost') { if(!empty($_POST['weight'])){ $shippingRate = $this->getWeightRate($_POST['weight']); } if(!empty($_POST['length']) && !empty($_POST['width']) && !empty($_POST['height'])) { $this->volume = $this->calculateVolume($_POST['length'], $_POST['width'], $_POST['height']); $shipping = $this->getVolumeRate($this->volume); $shippingRate = number_format($shipping, 2); } } // End of main isset() IF. return $shippingRate; } function calculate_ship_rate(){ // Check for form submission: if (isset($_POST['submitted']) && isset($_POST['formKey']) && $_POST['formKey'] == 'shipping') { // Minimal form validation: if ( is_numeric($_POST['price']) ) { if(!empty($_POST['weight'])){ $shippingRate = $this->getWeightRate($_POST['weight']); } else { $volume = $this->totalVolume($_POST['length'], $_POST['width'], $_POST['height']); //$volume = $_POST['volume']; $shippingRate = $this->getVolumeRate($volume); } $result = $this->totalProductCost($_POST['price'], $shippingRate, $_POST['quantity']); } else { } } // End of main isset() IF. return $result; } function shipping_calculator_form(){ ?> <div id="shipping-calculator"> <div id="lb-title-header">Shipping Calculator</div> <form action="<?php echo $this->postUrl; ?>" method="post" id="calculate"> <div class="fieldset"> <table cellspacing="3" cellpadding="3"> <tr valign="top"> <td class="label"><label>Product Cost:</label></td><td><input id="price" type="text" name="price" size="5" maxlength="10" value="<?php if (isset($_POST['price'])) echo $_POST['price']; ?>" /></td><td></td> </tr> <tr valign="top"> <td class="label"><lable>Qty:</lable></td><td><input id="quantity" type="text" name="quantity" size="5" maxlength="6" value="<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></td><td></td> </tr> <tr valign="top"> <td class="label"><label>Weight:</label></td><td><input id="weight" type="text" name="weight" size="5" maxlength="5" value="<?php if (isset($_POST['weight'])) echo $_POST['weight']; ?>" /></td><td><i>lbs</i></td> </tr> <tr valign="top"> <td class="label"><lable>Length:</lable></td><td><input id="length" type="text" name="length" size="5" maxlength="8" value="<?php if (isset($_POST['length'])) echo $_POST['length']; ?>" /></td><td><i>inches</i></td> </tr> <tr valign="top"> <td class="label"><lable>Width:</lable></td><td><input id="width" type="text" name="width" size="5" maxlength="8" value="<?php if (isset($_POST['width'])) echo $_POST['width']; ?>" /></td><td><i>inches</i></td> </tr> <tr valign="top"> <td class="label"><lable>Height:</lable></td><td><input id="height" type="text" name="height" size="5" maxlength="8" value="<?php if (isset($_POST['height'])) echo $_POST['height']; ?>" /></td><td><i>inches</i></td> </tr> <tr valign="top"> <td class="label"><label>Add Insurance?:</label></td><td><input type="checkbox" name="insurance" id="insurance" value="true" /></td><td></td> </tr> <tr valign="top"> <td><input id="submit" type="submit" name="submit" value="Calculate!" /></td><td></td><td></td> </tr> </table> </div> <input type="hidden" name="formKey" value="shipping" /> <input type="hidden" name="submitted" value="TRUE" /> </form> <div id="responce"></div> </div> <?php } } ?> The Result script <div id="shipcal"> <?php if(!class_exists('ShippingCalculator')){ include('class.shippingCalc.php'); $calculate = new ShippingCalculator(); //instantiate the shipping calculator class } if(isset($_POST['submit'])){ $calculate = new ShippingCalculator(); //instantiate the shipping calculator class // Set the shipping tax $calculate->tax = 17.5; // tax rate $calculate->minInsurance = 15.00; // Minimum Insurance Fee $calculate->brokerageFee = 10.00; // Brokerage Fee // Calculate shipping charges $results = $calculate->calculate_ship_rate(); echo '<div class="results-heading">Your Total Shipment Cost</div><table>'; echo '<tr><td class="label productcost">Product Cost:</td> <td class="result productcost">$'.$results['productcost'].'</td></tr>'; echo '<tr><td class="label freightcost">Freight Cost:</td> <td class="result freightcost">$'.$results['freightcost'].'</td></tr>'; echo '<tr><td class="label insurance">Insurance:</td> <td class="result insurance">$'.$results['insurance'].'</td></tr>'; echo '<tr><td class="label brokagefee">Brokage Fee:</td> <td class="result brokagefee">$'.$results['brokeragefee'].'</td></tr>'; echo '<tr><td class="label grandtotal">Grand Total:</td> <td class="result grandtotal">$'.$results['grandtotal'].'</td></tr>'; echo '</table>'; echo '<div class="calbutton"><a href="shipcal.php">« Back</a></div>'; } else { $calculate->postUrl = 'shipcal.php'; $calculate->shipping_calculator_form(); } ?> </div> I need to be able to calculate the shipping rates (freight cost) from the weight or volume (length, width, height) entries and without out the need to enter the product cost or quantity. Please?? I need this ASAP.. Any help, advise or instructions on how to do this will be greatly appreciated. Thanks in advanced, Winchester Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/ Share on other sites More sharing options...
gizmola Posted June 24, 2011 Share Posted June 24, 2011 Well you have this code: // Minimal form validation: if ( is_numeric($_POST['price']) ) { if(!empty($_POST['weight'])){ At quick glance it's pretty obvious you can't do that if you want to calculate the shipping cost without a price, because your code doesn't allow it. I'm having a hard time reconciling the fact that you could have written all this code and at the same time profess not to understand something so simple, but I guess anything is possible. Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/#findComment-1234210 Share on other sites More sharing options...
WStudio Posted June 24, 2011 Author Share Posted June 24, 2011 Well you have this code: // Minimal form validation: if ( is_numeric($_POST['price']) ) { if(!empty($_POST['weight'])){ At quick glance it's pretty obvious you can't do that if you want to calculate the shipping cost without a price, because your code doesn't allow it. I'm having a hard time reconciling the fact that you could have written all this code and at the same time profess not to understand something so simple, but I guess anything is possible. Thank you very much for your reply. The truth is, it wasn't apart of the design. It wasn't until later the client refused it because it wasn't doing what he wanted it to do. You know that type of client that keeps changing things during the development process. Luckly most of the new stuff were stuff I could add.. but I'm blank with this one. And this is the last thing I have to do the complete the project. What do you propose I do to get this done? Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/#findComment-1234224 Share on other sites More sharing options...
gizmola Posted June 24, 2011 Share Posted June 24, 2011 I don't know what you mean. Do you understand that code, and what the problem is with it? Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/#findComment-1234228 Share on other sites More sharing options...
WStudio Posted June 24, 2011 Author Share Posted June 24, 2011 Once again Gizmola, Thank you for your help. I started reconstruct it in javascript.. I had already started it before.. I guess I'm going to have to just finish it. Thanks Best regards, Winchester Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/#findComment-1234325 Share on other sites More sharing options...
gizmola Posted June 24, 2011 Share Posted June 24, 2011 Once again Gizmola, Thank you for your help. I started reconstruct it in javascript.. I had already started it before.. I guess I'm going to have to just finish it. So this means you don't understand it? How will doing anything in javascript help you with this situation? That code makes it very clear: if the form has no price filled out, it will not get to the shipping code. Why don't you try commenting out that if -then block and see what happens. You might be surprised. If you're really this befuddled with the simplest of code, I don't know what you hope you to gain from posting these sorts of questions here. It also doesn't help you at all when you claim to write code you didn't write. I looked at your previous posting history and my suspicions were confirmed within 10 seconds. Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/#findComment-1234531 Share on other sites More sharing options...
Nuv Posted June 25, 2011 Share Posted June 25, 2011 Once again Gizmola, Thank you for your help. I started reconstruct it in javascript.. I had already started it before.. I guess I'm going to have to just finish it. So this means you don't understand it? How will doing anything in javascript help you with this situation? That code makes it very clear: if the form has no price filled out, it will not get to the shipping code. Why don't you try commenting out that if -then block and see what happens. You might be surprised. If you're really this befuddled with the simplest of code, I don't know what you hope you to gain from posting these sorts of questions here. It also doesn't help you at all when you claim to write code you didn't write. I looked at your previous posting history and my suspicions were confirmed within 10 seconds. Wish we had a 'Like' button. I would have liked gizmola's post. Quote Link to comment https://forums.phpfreaks.com/topic/240285-calculator-script/#findComment-1234619 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.