shopping carts chapter 19. e-commerce typically, an e-commerce site will have public pages and admin...

13
SHOPPING CARTS CHAPTER 19

Upload: georgiana-burke

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

SHOPPING CARTSCHAPTER 19

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

E-COMMERCE

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

Page 3: SHOPPING CARTS CHAPTER 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.

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

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>

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

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.

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

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.

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

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.

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

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>';

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

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>';

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

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>';

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

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

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

VIEWING THE CART

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

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

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