SHOPPING CARTS CHAPTER 19. E-COMMERCE Typically, an e-commerce site will have public pages and admin...

Preview:

Citation preview

SHOPPING CARTSCHAPTER 19

E-COMMERCE

Typically, an e-commerce site will have public pages and admin pages.

THE PUBLIC PAGES

• In an e-commerce setting, a user's session must be maintained across every page.

• If the session is lost on any particular page, then a new session would begin on subsequent pages, losing the contents of the cart.

• The best solution is to start the session in the header file.

SESSIONS

Remember that session information needs to happen before any HTML is returned to the user:

<?php # Script 19.3 - header.html

/* This page begins the session, the HTML page, and the layout table.*/

session_start(); // Start a session.

?>

<!DOCTYPE html>

THE SHOPPING CART

Once the product catalog is created, a shopping cart can be implemented using sessions.

This example will record the product ID, the price and quantity of each item in a session variable.

THE SHOPPING CART

The cart will store values using the $_SESSION['cart'] variable:

• A multidimensional array whose keys will be productIDs

• The values of the array elements will themselves be arrays: one element for the quantity and another for the price.

THE SHOPPING CART

<?php # Script 19.9 - add_cart.php

// This page adds products to the shopping cart.

// Set the page title and include the HTML header

$page_title = 'Add to Cart';

include ('includes/header.html');

We need to include the header to start or maintain the session.

THE SHOPPING CART

// Check if the cart already contains one of these products;

// If so, increment the quantity:

if (isset($_SESSION['cart'][$pid])) {$_SESSION['cart'][$pid]['quantity']++; // Add another.

// Display a message:

echo '<p>Another copy of the print has been added to your shopping cart.</p>';

THE SHOPPING CART

// Check if the cart already contains one of these products;

// If so, increment the quantity:

if (isset($_SESSION['cart'][$pid])) {$_SESSION['cart'][$pid]['quantity']++; // Add another.

// Display a message:

echo '<p>Another copy of the print has been added to your shopping cart.</p>';

THE SHOPPING CARTelse { // New product to the cart.

// Get the price from the database:

require ('../mysqli_connect.php'); // Connect to the database.

$q = "SELECT price FROM prints WHERE print_id=$pid"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { // Valid print ID.

// Fetch the information.

list($price) = mysqli_fetch_array ($r, MYSQLI_NUM);

// Add to the cart:

$_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price);

// Display a message:

echo '<p>The print has been added to your shopping cart.</p>';

THE SHOPPING CART

$_SESSION['cart'] contains the shopping information

$_SESSION contains other data like the session ID.

The most important thing to store in the cart is the unique product ID (or whatever the PK is for products) and the quantity of that item

VIEWING THE CART

Once the cart is created, it can be displayed as a form where quantities can be changed.

CHECKOUT

AFTER the money transaction

1. Enter the order into the database using

1. The customer's id

2. The order total

2. Enter the order details into the database

3. Clear the cart

Recommended