Shopping Cart Tutorial PHP Shopping Cart Tutorial
Learn how to create an online store with PHP and MySQL
Free shopping cart    Download:
Shop-Script FREE
Shop-Script FREE User Guide (PDF; 0 Kb)
View Live Demo    View online store demo
 Introduction
 Basic concepts
 Technologies
 File structure
 Database structure
 Back end
   Password protection
   Products catalog: structure
   Product catalog: viewing
   Managing categories
   Managing products
   Special offers
   Managing orders
 Front end
   Viewing products catalog
   Product search
   Shopping cart
   Checkout
   PayPal integration
 Security
 Live Demo
 Author
 Terms Of Use

Shopping cart software

 

Checkout

The purpose of checkout process is collect customer information, save order to the database and email store administrator an order notification. Let me cover these issues one by one.

Collecting customer information

Here I will review the simplest (and the most efficient!) checkout process consisting of only one step - customer is only prompted to enter his contact information and click "Place order!" button.

When pushing "Proceed to checkout" button on shopping cart page, customer is redirected to index.php?order_custinfo=yes page:

<form action="index.php" method="post">

<table>

<tr>
<td colspan="3" align="center">
<table bgcolor="#ADADAD" width="80%"><tr><td>CONTACT INFORMATION</td></tr></table>
</td>
</tr>

<tr>
<td colspan="3" align="center">
<font color="red">*</font> required<br><br>
</td>
</tr>

<tr>
<td colspan="2" align="right"><font color="red">*</font> First name:</td>
<td><input type="text" name="first_name" value=""></td>
</tr>

<tr>
<td colspan="2" align="right"><font color="red">*</font> Last name:</td>
<td><input type="text" name="last_name" value=""></td>
</tr>
<tr>
<td colspan="2" align="right"><font color="red">*</font> Email:</td>
<td><input type="text" name="email" value=""></td>
</tr>
<tr>
<td colspan="2" align="right">Phone number:</td>
<td><input type="text" name="phone" value=""></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="2" align="right">Address:</td>
<td><textarea name="address" rows="4"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right"><font color="red">*</font> City:</td>
<td><input type="text" name="city" value=""></td>
</tr>
<tr>
<td colspan="2" align="right"><font color="red">*</font> State:</td>
<td><input type="text" name="state" value=""></td>
</tr>
<tr>
<td colspan="2" align="right"><font color="red">*</font> Zip code:</td>
<td><input type="text" name="zip" value=""></td>
</tr>
<tr>
<td colspan="2" align="right"><font color="red">*</font> Country:</td>
<td><input type="text" name="country" value=""></td>
</tr>

</table>

<p>
<input type="submit" value="Place order!">

<input type="hidden" name="complete_order" value="1">
</p>

</form>

Clicking "Place order!" will save order to the database.

Saving order to the database

In this moment order becomes complete. Following steps are performed:

  1. New record is added to the ORDER table using a simple query:

    INSERT INTO `ORDER` (order_time, cust_firstname, cust_lastname, cust_email, cust_country, cust_zip, cust_state, cust_city, cust_address, cust_phone) VALUES ('".get_current_time()."','".$_POST["first_name"]."','".$_POST["last_name"]."','".$_POST["email"]."','".$_POST["country"]."','".$_POST["zip"]."','".$_POST["state"]."','".$_POST["city"]."','".$_POST["address"]."','".$_POST["phone"]."');

    This query only saves the information which customer has provided in the CONTACT INFORMATION form. It does not save order content and order amount information.

    Please note that the query presented above is just an example. You should keep in mind that you have to validate input $_POST data before executing a query (with mysql_escape_string() functions, for example). Please refer to Security section for more details.

  2. Unique orderID value of the new order is returned by MySQL.

  3. Shopping cart product list is fetched from session and saved to the database.
    On this step script processes each product from the list stored in shopping cart session variables (includes/order_place.php):

    <?php

        $k 
    0//order total value
        
    $admin_notification ""//a list of product to be emailed to store administrator

        
    for ($i=0$i<count($_SESSION["gids"]); $i++)
        {
            if (
    $_SESSION["gids"][$i]) //save this product in the database
            
    {
                
    $q db_query("SELECT name, Price, product_code FROM ".PRODUCTS_TABLE." WHERE productID='".(int)$_SESSION["gids"][$i]."'") or die (db_error());
                if (
    $r db_fetch_row($q))
                {
                    
    //collect product information in array
                    
    $tmp = array(
                        
    $_SESSION["gids"][$i],
                        
    $r[0],
                        
    $_SESSION["counts"][$i],
                        (
    $_SESSION["counts"][$i]*$r[1])." ".$currency_iso_3,
                        
    $r[2]
                    );

                    
    //store ordered products info into the database
                    
    $sku trim($tmp[4]) ? "[".$tmp[4]."] " "";
                    
    db_query("insert into ".ORDERED_CARTS_TABLE." (orderID, productID, name, Price, Quantity) values ('$oid', '".$tmp[0]."', '".$sku.$tmp[1]."', '".$r[1]."', '".$tmp[2]."');");

                    
    //calculate order amount
                    
    $k += $_SESSION["counts"][$i]*$r[1];

                    
    //update order notification message sent to store administrator
                    
    $admin_notification .= $sku.$tmp[1]." (x".$tmp[2]."): ".$tmp[3]."\n";

                }
            }
        }

    ?>


  4. Shopping cart information stored in session is erased to allow user  new order.
    This is done merely by unsetting session variables.

Emailing order notification

This is the last and very simple procedure of checkout process.
If you look at the source code file includes/order_place.php fragment, you may notice definition of $admin_notification variable.
When products are saved to the database, this variable is updated and at the end of order processing collects complete order information.
Now we only have to email this variable to store administrator using regular mail() function.

<?php

    mail
(
        
CONF_ORDERS_EMAIL,
        
EMAIL_ADMIN_ORDER_NOTIFICATION_SUBJECT,

            
STRING_ORDER_ID.": $oid\n\n".
            
CUSTOMER_FIRST_NAME." ".$_POST["first_name"]."\n".
            
CUSTOMER_LAST_NAME." ".$_POST["last_name"]."\n".
            
CUSTOMER_ADDRESS.": ".$_POST["country"].", ".$_POST["zip"].
            
", ".$_POST["state"].",  ".$_POST["city"].", ".$_POST["address"]."\n".
            
CUSTOMER_PHONE_NUMBER.": ".$_POST["phone"]."\n".
            
CUSTOMER_EMAIL.": ".$_POST["email"].
            
$admin_notification,

        
"From: \"".CONF_SHOP_NAME."\"<".CONF_GENERAL_EMAIL.">\n".
        
stripslashes(EMAIL_MESSAGE_PARAMETERS).
        
"\nReturn-path: <".CONF_GENERAL_EMAIL.">"
    
);

?>

 

 


Copyright © 2006 WebAsyst LLC