Chapter 15: Pre-Processing Orders

Google

Doing the math

OK. You're going to be taking payments, possibly for multiple items, and possibly including sales tax and shipping charges. You may have to multiply quantity by unit price or add several items with different prices together.

You may need to add in some or all of the other charges. In other words, you'll have to do some simple math. PHP can handle it easily in your script. For example, you might have code like this:

$Subtotal = $Quantity1 * $Unit_Price;

If you have to do subtotals on different items, add Sales Tax and Shipping Charges. It's as simple as:

$Subtotal_1 = $Quantity_1 * $Unit_Price_1;
$Subtotal_2 = $Quantity_2 * $Unit_Price_2;
$Subtotal_3 = $Quantity_3 * $Unit_Price_3;
$Subtotal = $Subtotal_1 + $Subtotal_2 + $Subtotal_3;
$Total = $Subtotal + $Tax + $Shipping;

You're not likely to need anything any more complicated than that, unless you're offering a discount percentage under certain conditions. In that case, you'd multiply the Unit_Price by {1.00 - the discount (expressed as a decimal fraction)} and continue totaling things up.

For Shipping Charges, a few "if" statements can be written to handle charges based on size of order, cost of order, weight, where it's being shipped, shipping method, etc. You could have certain choices built into your form to narrow the range of possibilities your script has to consider at one time. You can look up shipping rates for UPS, FedEx, and UPS at their web sites.

For sales tax, you could have a table of rates in which your script reads a small database with the tax rates in one table. Add a table with a set of shipping charges and let the database supply the numbers you need. All the things that affect your shipping rates can be built into the shipping rate table. When it's time to update the shipping rates, a few SQL statements can be used to update the table. Get the information for your tables direct from the shipping companies you plan to use.

Tip: If you are selling digital goods such as ebooks or software, be aware that easy ways exist for people to take your products without paying for them. ClickBank and PayPal merchants are the most vulnerable, but some merchant account-based ordering systems have similar loopholes. For the solution to this and 12 other information products disasters, you should read this fascinating report by Andy Brocklehurst: Infoproduct Magic. Andy is one of those guys you seldom hear about. But I can tell you first-hand that he's the real deal. I've known him for years and I respect him a lot more than I do a lot of the so-called "gurus"...and I know most of them personally.


Giving a receipt

Depending on how your payment system works, there can be many different ways to generate a receipt. In a lot of cases, this will be done automatically. If it isn't, just feed all the information that should be on the receipt into a block of code that will compose an email message and send it to the person who ordered something from you. Review Chapter 12: Sending Email to see how this is done.

Your customer should always get an email receipt. It's also good practice to show the customer a summary of their order just before they commit to making the payment. That's a good time to suggest something else they might like to order. This is known as "upselling." It can easily increase the size and value of the orders you get.

I found that PHP will be somewhat arbitrary in the way it displays calculated numbers. You naturally want them to have a consistent appearance. Fortunately, this is also pretty easy. Actually, it took me a good bit of experimenting to get it just right. Here's my solution coded in Perl:

if ($Total !~ /\./) {
    $Total .= ."00";
}
if ($Total !~ /\.[0-9][0-9]/) {
    $Total .= "0";
}

For any item where you'll show a price in dollars and cents, these lines will ensure that it has a decimal point and 2 digits to the right of that. If your testing reveals that a price may come out to a fraction of a cent, add a line to round to the nearest cent and you'll have no trouble with the display.

Ah...but you're reading this to pick my brains about PHP, aren't you? I really "cut my teeth" on Perl, and even in the PHP world some of it is still pretty useful. You see, Perl lets you create an infinite variety of "regular expressions" to do all sorts of tricks with text, etc. PHP developers didn't implement all of that power directly, but they did slip in a few functions that can use Perl-compatible regular expressions.

So what you can do to save a lot of time is to just grab a killer Perl regular expression and stick it into one of PHP's built in functions like this:

preg_match(pattern, text, optional:matches)

So, without further ado, here's the PHP code, wrapped up as a function you can just copy and use:

function dollar($price) {
  if (!preg_match("/\./", $price)) { # $price had no decimal point
    $price .= ."00";
    return $price;
  }
  if (!preg_match("/\.[0-9][0-9]/", $price)) { # $price had one digit after a decimal point
    $price .= "0";
    return $price;
  }
  $x_digit = preg_replace("/[d]+\.[\d]{2}/","",$price);
  if ($x_digit != 0) {$price -= ($x_digit/1000);} # remove extra digit
  if ($x_digit >= 5) {$price += .01;} # round up if necessary
  return $price;
}

This is such a useful function you might want to copy it into your configuration file so it gets included without any effort on your part. Or keep it in a "library file" where you stash useful code snippets and just copy-and-paste it wherever you need it. To use it in a script just call it like this:

$dollar_amount = dollar($price);

The function will force the value of $dollar_amount to have exactly two digits after the decimal point. It will also round up if there was originally a third digit after the decimal point in $price that is 5 or larger.

As for $price, that's a value you got from math operations performed within your script. It doesn't have to be named $price. But if you do use another name, be sure to pass that name (variable) to the function dollar() when you call it.


Thank-You Pages

I'll cover this in more detail later on.For now, just be aware that you should make sure the customer knows instantly that their order has been received and accepted. The simplest possible way is to send them to a so-called "thank-you" page. Here's an easy way to do that so that this page will look the same to every customer:

header("Location: http://www.yourdomain.com/thankyou.php");

This simple but powerful function lets you send the visitor to any page you choose. If you want to customize the page, you can do it like this:

$url = "http://www.yourdomain.com/thankyou.php?$query";
header("Location: $url");

Then, the page you send them to will have some PHP code to use whatever was passed in the query string to customize the page. Details will follow in a later chapter, so just keep reading!

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey