Skip to content

Instantly share code, notes, and snippets.

@nickdgreen
Last active October 8, 2016 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nickdgreen/dcee7db4fd4058c7882d4c46fe864f03 to your computer and use it in GitHub Desktop.
Save nickdgreen/dcee7db4fd4058c7882d4c46fe864f03 to your computer and use it in GitHub Desktop.
liveEstimatePHPCode.php creates a form to collect inputs from a client and then calculates the estimated courier charge based on the type of vehicle and the distance using UK post codes. iaf-plugin.php appears to create a widget (i.e. it appears in the widget menu) but when added to a page nothing is dispalyed.
<form action="#formend" method="post"><input name="submited" type="hidden" value="true" />
<table border="0" width="450px">
<tbody>
<tr>
<td width="200px">Collection Postcode:</td>
<td width="250px"><input name="fromCode" size="10" type="text" value="" /></td>
</tr>
<tr>
<td>Destination Postcode:</td>
<td><input name="toCode" size="10" type="text" value="" /></td>
</tr>
<tr>
<td>Van Type:</td>
<td><select name="vanType">
<option selected="selected" value="none"></option>
<option value="Small">Small</option>
<option value="SWBTransit">Short Wheelbase Transit</option>
<option value="Sprinter">Sprinter Van</option>
</select></td>
</tr>
</tbody>
</table>
<input name="submit" type="submit" />
</form>
<?php
class Estimate_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
function __construct() {
parent::__construct(
'estimate_widget', // Base ID
__( 'Courier Estimate', 'text_domain' ), // Name
array( 'description' => __( 'An Estimate Widget', 'text_domain' ), ) // Args
);
}
/*
Plugin Name: Site Plugin for www.inaflashcouriers.co.uk
Description: Site specific code changes for www.inaflashcouriers.co.uk
*/
/* Start Adding Functions Below this Line */
/* Prices Estimator */
// PARAMETRES
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
//public function widget( $args, $instance ) {
//$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
//echo $args['before_widget'];
//echo $args['before_title'] . $title . $args['after_title'];
$vatrate = "0.2";// vat rate set at 20%
$minCharge = "0";// minimum charge reset to zero
$vanmin = "60";// small van minimum charge set to £60
$swbmin = "75";// short wheelbase van minimum charge set to £75
$lwbmin = "90";// long wheelbase van minimum charge set to £90
$minDist = "49";// minimum distance set to 49 miles
// END PARAMETRES
if (isset($_POST['submit']))
{
$fromCode = $_POST['fromCode'];
$toCode = $_POST['toCode'];
$vanType = $_POST['vanType'];
// Set default van type to small
if ($vanType == "none"){
$vanType = "Small";
}
// Calculate the minimum charge that may apply for the selected van type
if ($vanType == "Small"){
$minCharge = $vanmin;
}
else {
switch ($vanType) {
case "SWBTransit":
$minCharge = $swbmin;
break;
case "Sprinter":
$minCharge = $lwbmin;
break;
}
}
// Set the mileage charge for the selected van type
if ($vanType == "Small"){
$charge = "1.10";
}
else {
switch ($vanType) {
case "SWBTransit":
$charge = "1.30";
break;
case "Sprinter":
$charge = "1.50";
break;
}
}
try
{
$info = get_driving_information($fromCode, $toCode);
$dist = $info['distance'];
$dist = ceil($dist);
$cost = $dist * $charge;
// Test if delivery is less than minimum distance and set minimum charge if true
if ($dist < $minDist){
$showMin = "(Minimum Charge Applies)";}
else {$showMin = "";}
if ($cost < $minCharge){
$cost = $minCharge;}
else {$cost = $cost;}
$vat = $cost * $vatrate;
$total = $cost + $vat;
echo "<table width='100%' border='0' cellpadding='10'>";
echo "<tr bgcolor='#CCC'>";
echo "<td colspan='2' align='center'><strong>Your Estimate for:<br/>".$fromCode." to ".$toCode."</strong></td>";
echo "</tr><tr bgcolor='#D7D9FF'>";
echo "<td>Van Type:</td>";
echo "<td align='center'>".$vanType."</td>";
echo "</tr><tr bgcolor='#FFD7D7'>";
echo "<td>Distance:</td>";
echo "<td align='center'>".$dist." miles</td>";
echo "</tr><tr bgcolor='#D7D9FF'>";
echo "<td>Net cost ".$showMin." :</td>";
echo "<td align='right'> &pound;".number_format((float)$cost, 2, '.', '')."</td>";
echo "</tr><tr bgcolor='#FFD7D7'>";
echo "<td>vat @20%:</td>";
echo "<td align='right'> &pound;".number_format((float)$vat, 2, '.', '')."</td>";
echo "</tr><tr bgcolor='#D7D9FF'>";
echo "<td>Total:</td>";
echo "<td align='right'> &pound;".number_format((float)$total, 2, '.', '')."</td></tr>";
echo "<tr bgcolor='#FFFFFF'><td> &nbsp;</td></tr></table>";
}
catch(Exception $e)
{
echo 'There was an error: '.$e->getMessage()."\n";
}
}
function get_driving_information($start, $finish, $raw = false)
{
if(strcmp($start, $finish) == 0)
{
$time = 0;
if($raw)
{
$time .= ' seconds';
}
return array('distance' => 0, 'time' => $time);
}
$start = urlencode($start);
$finish = urlencode($finish);
$distance = 'unknown';
$time = 'unknown';
$url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$finish.'&sensor=false&units=imperial';
if($data = file_get_contents($url))
{
$xml = new SimpleXMLElement($data);
if(isset($xml->route->leg->duration->value) AND (int)$xml->route->leg->duration->value > 0)
{
if($raw)
{
$distance = (string)$xml->route->leg->distance->text;
$time = (string)$xml->route->leg->duration->text;
}
else
{
$distance = (int)$xml->route->leg->distance->value / 1000 / 1.609344;
$time = (int)$xml->route->leg->duration->value;
}
}
else
{
throw new Exception('Could not find that route please check postcodes');
}
return array('distance' => $distance, 'time' => $time);
}
else
{
throw new Exception('Could not resolve URL');
}
}
}
}
// Register and load the widget
function wpb_load_widget() {
register_widget( 'estimate_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
<!- html bit ->
<form action="#formend" method="post"><input name="submited" type="hidden" value="true" />
<table border="0" width="450px">
<tbody>
<tr>
<td width="200px">Collection Postcode:</td>
<td width="250px"><input name="fromCode" size="10" type="text" value="" /></td>
</tr>
<tr>
<td>Destination Postcode:</td>
<td><input name="toCode" size="10" type="text" value="" /></td>
</tr>
<tr>
<td>Van Type:</td>
<td><select name="vanType">
<option selected="selected" value="none"></option>
<option value="Small">Small</option>
<option value="SWBTransit">Short Wheelbase Transit</option>
<option value="Sprinter">Sprinter Van</option>
</select></td>
</tr>
</tbody>
</table>
<input name="submit" type="submit" />
</form>
<?php
// PARAMETRES
$vatrate = "0.2";// vat rate set at 20%
$minCharge = "0";// minimum charge reset to zero
$vanmin = "60";// small van minimum charge set to £60
$swbmin = "75";// short wheelbase van minimum charge set to £75
$lwbmin = "90";// long wheelbase van minimum charge set to £90
$minDist = "49";// minimum distance set to 49 miles
// END PARAMETRES
if (isset($_POST['submit']))
{
$fromCode = $_POST['fromCode'];
$toCode = $_POST['toCode'];
$vanType = $_POST['vanType'];
// Set default van type to small
if ($vanType == "none"){
$vanType = "Small";
}
// Calculate the minimum charge that may apply for the selected van type
if ($vanType == "Small"){
$minCharge = $vanmin;
}
else {
switch ($vanType) {
case "SWBTransit":
$minCharge = $swbmin;
break;
case "Sprinter":
$minCharge = $lwbmin;
break;
}
}
// Set the mileage charge for the selected van type
if ($vanType == "Small"){
$charge = "1.10";
}
else {
switch ($vanType) {
case "SWBTransit":
$charge = "1.30";
break;
case "Sprinter":
$charge = "1.50";
break;
}
}
try
{
$info = get_driving_information($fromCode, $toCode);
$dist = $info['distance'];
$dist = ceil($dist);
$cost = $dist * $charge;
// Test if delivery is less than minimum distance and set minimum charge if true
if ($dist < $minDist){
$showMin = "(Minimum Charge Applies)";}
else {$showMin = "";}
if ($cost < $minCharge){
$cost = $minCharge;}
else {$cost = $cost;}
$vat = $cost * $vatrate;
$total = $cost + $vat;
echo "<table width='100%' border='0' cellpadding='10'>";
echo "<tr bgcolor='#CCC'>";
echo "<td colspan='2' align='center'><strong>Your Estimate for:<br/>".$fromCode." to ".$toCode."</strong></td>";
echo "</tr><tr bgcolor='#D7D9FF'>";
echo "<td>Van Type:</td>";
echo "<td align='center'>".$vanType."</td>";
echo "</tr><tr bgcolor='#FFD7D7'>";
echo "<td>Distance:</td>";
echo "<td align='center'>".$dist." miles</td>";
echo "</tr><tr bgcolor='#D7D9FF'>";
echo "<td>Net cost ".$showMin." :</td>";
echo "<td align='right'> &pound;".number_format((float)$cost, 2, '.', '')."</td>";
echo "</tr><tr bgcolor='#FFD7D7'>";
echo "<td>vat @20%:</td>";
echo "<td align='right'> &pound;".number_format((float)$vat, 2, '.', '')."</td>";
echo "</tr><tr bgcolor='#D7D9FF'>";
echo "<td>Total:</td>";
echo "<td align='right'> &pound;".number_format((float)$total, 2, '.', '')."</td></tr>";
echo "<tr bgcolor='#FFFFFF'><td> &nbsp;</td></tr></table>";
}
catch(Exception $e)
{
echo 'There was an error: '.$e->getMessage()."\n";
}
}
function get_driving_information($start, $finish, $raw = false)
{
if(strcmp($start, $finish) == 0)
{
$time = 0;
if($raw)
{
$time .= ' seconds';
}
return array('distance' => 0, 'time' => $time);
}
$start = urlencode($start);
$finish = urlencode($finish);
$distance = 'unknown';
$time = 'unknown';
$url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$finish.'&sensor=false&units=imperial';
if($data = file_get_contents($url))
{
$xml = new SimpleXMLElement($data);
if(isset($xml->route->leg->duration->value) AND (int)$xml->route->leg->duration->value > 0)
{
if($raw)
{
$distance = (string)$xml->route->leg->distance->text;
$time = (string)$xml->route->leg->duration->text;
}
else
{
$distance = (int)$xml->route->leg->distance->value / 1000 / 1.609344;
$time = (int)$xml->route->leg->duration->value;
}
}
else
{
throw new Exception('Could not find that route please check postcodes');
}
return array('distance' => $distance, 'time' => $time);
}
else
{
throw new Exception('Could not resolve URL');
}
}
?>
@nickdgreen
Copy link
Author

Html form part added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment