Completing Checkout

 

Introduction.  Finally, we are at the end of our ecommerce ordeal.  Now we need to complete the order processing by doing the following.
  • compute the shipping costs
  • compute the taxes
  • compute the appropriate totals and subtotals
  • display these values and the order number
  • write these and other overall values to the basket table for this id_basket
  • display what has been ordered (left for you in homework)
  • display the billing information (left for you in homework)
  • display the shipping information (left for you in homework)

Most of this processing is quite sequential and it is more an issue of being organized than being clever.  This page should be called checkout_completed.php.

 
 
<html>
<!-- checkout_completed.php - completing the checkout for this order -->
<head><title>Completing the Checkoutder</title></head>
<?php
include("includes/header.php");
include("includes/connection_config.php");
// we assign $_SESSION variables to local variables
// while this probably isn't necessary, it makes some processing look cleaner
// we need these values to compute the shipping and taxes

session_start();
$id_basket = $_SESSION['id_basket'];
$sub_total = $_SESSION['total_cost'];
$total_weight = $_SESSION['total_weight'];
$ship_state = $_SESSION["ship_state"];
$order_placed = 1;
// initializing a variable that tells whether the weight was found in the ranges in the table
$in_table = "not_found";
// now we need to compute the shipping charges
// first we need to find the shipping charges in the table based on the weight
// if the weight is greater than that contained in the table then we need to use the formula
// first we need to connect to the shipping table

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// creating the query string
$ship_query_string = "SELECT * FROM shipping";
// executing the SQL statement
$ship_result_set = mysql_db_query($db_name, $ship_query_string, $link);
// looping through the entries in the resultset to determine shipping charges
while ($row = mysql_fetch_array($ship_result_set))
{

if (($total_weight >= $row[lower_limit]) && ($total_weight <= $row[upper_limit]))
{

$in_table = "found";
$shipping_cost = $row[shipping_cost];

}

}
// if the weight is too large to be contained in the table we need to
// compute the shipping using the formula

if ($in_table == "not_found")
{

$shipping_cost = 5.00 + (0.5 * $total_weight);

}
$shipping_cost = round($shipping_cost,2);

// now we need to determine if there are any taxes to be charged
// based on the $ship_state
// first we need to develop another query to access the state tax rate

$tax_query_string = "SELECT * FROM taxes_state WHERE state_name = '$ship_state'";
// now we need to create the resultset
$tax_result_set = mysql_db_query($db_name, $tax_query_string, $link);
// looping through the entries in the resultset to determine taxes
while ($row_tax = mysql_fetch_array($tax_result_set))
{

$tax = $row_tax[tax_rate] * $sub_total;
$tax = round($tax,2);
$sub_total = round($sub_total,2);
$overall_cost = $sub_total + $tax + $shipping_cost;
$overall_cost = round($overall_cost,2);

}

// now we want to update our values in the basket
$update_query_string = "UPDATE basket SET
order_placed = $order_placed,
sub_total = $sub_total,
total =$overall_cost,
cost_shipping = $shipping_cost,
tax =$tax
WHERE (id_basket = $id_basket)";
// performing the update
mysql_db_query($db_name, $update_query_string, $link);

// now we want to retrive the order_number for the order
// get the id_order_data from the session variable
// put it into a local variable for display

$order_number = $_SESSION["id_order_data"];
?>
<br><br>
<table border="0" align="center">
<tr>
<td align="right"><font size="4" color="a66838">Your order number is:</font>
</td>
<td align="right"><font size="4" color="c0c0c0"><?php echo $order_number; ?></font>
</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td align="right"></td>
</tr>
<tr>
<td align="right"><font size="4" color="a66838">The subtotal is:</font>
</td>
<td align="right"><font size="4" color="c0c0c0">$<?php printf("%01.2f",$sub_total); ?></font>
</td>
</tr>
<tr>
<td align="right"><font size="4" color="a66838">The tax is:</font>
</td>
<td align="right"><font size="4" color="c0c0c0">$<?php printf("%01.2f",$tax); ?></font>
</td>
</tr>
<tr>
<td align="right"><font size="4" color="a66838">The shipping costs:</font>
</td>
<td align="right"><font size="4" color="c0c0c0">$<?php printf("%01.2f",$shipping_cost); ?></font>
</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td align="right"></td>
</tr>
<tr>
<td align="right"><font size="4" color="a66838">The total cost is:</font>
</td>
<td align="right"><font size="4" color="c0c0c0"><b>$<?php printf("%01.2f",$overall_cost); ?></b></font>
</td>
</tr>

</table>
<?php
// closing the link to the database
mysql_close($link);
?>

</body>
</html>

 

A typical page should look like the following.

 

 

A general code description follows.

  • we start by developing some mnemonic local variables based on the $_SESSION variables
  • then we connect to the database
  • the first query selects everything from the shipping table in order to compute the shipping costs based on the total weight
  • we loop through the resultset to see if the weight fits between any given pairs of lower_limit and upper_limit
    • if they do then we pull the cost of shipping form the table
    • if they don't then we compute the shipping based on the formula
  • we round the cost of shipping to two decimal places so that we can update out basket table later
  • next we create a query to access the tax rate from the taxes_state table based on where the order will be shipped
  • we compute the taxes
  • we accumulate our subtotals and totals and round them to two decimal places so that we can update our basket table later
  • next we perform the update to the basket table based on the id_basket
  • next we retrieve the order number for the order from the order_data table based on the id_basket
  • finally, we display these results in a table using appropriate formatting

That's all folks!  Though I may get back to developing the store management side of this.