Samples Product Information Home Page Pricing Pricing

Custom Cart Configuration

The previous tutorial showed both the format of the HTML link and how to change the destination to a custom URL. This tutorial will describe how the product data in the HTML link is recorded in a mySQL database.

To begin, open this sample PDF and click any Add to Cart button.

Checkout Page

 

The Add to Cart button on the Sample PDF is an HTML link that submits to a checkout page. For the purposes of this tutorial, the page simply lists those products added to the cart, and has a button to email that list to both the buyer and vendor.

Return to the PDF to add more items. If an item already exists in the list, the quantity will be incremented.

Enter your email address and click the Submit Order button to have the list emailed to you.

The essential code for this very basic example is shown below. Briefly, it takes the submitted variables and records them in a mySQL database.

include("dbConfig.php");

// These are the names of the columns in your cart database

$cartPrimary = 'itemName';
$nameSecondary = 'on0';
$cartSecondary = 'os0';
$cartQty = 'qty';
$cartAmount = 'amount';

// These are the retrieved values passed from the cart button on the PDF

$itemName = $_GET['item_name'];
$optLabel = $_GET['on0'];
$optName = $_GET['os0'];
$amount = $_GET['amount'];

// This sections adds the item to the cart

if(!empty($itemName)) {
    $result = mysqli_query($conn, "SELECT * FROM " . $cartTable . " WHERE (" . $cartID . " = '$custID' AND " . $cartPrimary . " = '$itemName' AND " . $cartSecondary . " = '$optName')");
    $nResult = (mysqli_num_rows($result));
    if ($nResult > 0) {
      $cart = mysqli_fetch_array($result);
      $newQty = ($cart[$cartQty] + 1);
      mysqli_query($conn, "UPDATE " . $cartTable . " SET " . $cartQty . " = '$newQty' WHERE (" . $cartID . " = '$custID' AND " . $cartPrimary . " = '$itemName' AND " . $cartSecondary . " = '$optName')");
    } else {  // If the cart already contains that item, the quantity is incremented
      mysqli_query($conn, "INSERT INTO " . $cartTable . "(" . $cartID . ", " . $cartPrimary . ", " . $nameSecondary . ", " . $cartSecondary . ", " . $cartAmount . ", " . $cartQty . ") VALUES ('$custID', '$itemName', '$optLabel', '$optName', '$getPrice', 1)");
    }
}

Of course, there is more code required for the complete page and email functions, and an online database is needed to store the list, but these are well within the grasp of any competent web designer.

 

Return to Tutorial Selection