<?php
// "CookieCarter" rev 1.1
//
// A shopping cart in PHP, implemented using cookies
//
// Supplied ***FREE*** by Thomas Dowad, Squamish, BC, Canada
// Send me 10 bucks if you feel like it
//
// copyright (c) 2002 Thomas Dowad.
//
// Contact me at: tdowad@hotmail.com
//
// Please report bugs, improvement ideas, comments to tdowad@hotmail.com
//
//
// ABOUT THIS CLASS
//
// Try it at www.mythicalcoffeehouse.com/_cart/
//
// There are various ways to store shopping cart contents. For instance,
// in a file, in a database, in a cookie, in hidden fields, etc. I chose
// to use a cookie, because I did not want to store anything in my order
// database before the order was confirmed. It seemed a cleaner solution,
// as it is not necessary to later remove incomplete orders from the database.
//
// The cookie only stores a part number and a quantity. One needs to be able
// to display the contents of the cart, and this is dependent on what kind of
// things you are dealing with, how you are storing the details, etc.
// There is one method that you will need to work on, that is "fetch_details()"
//
// It is assumed throughout that cookies are enabled on the client browser.
//
// This class has had minimal testing in the real world (part of my reason
// for offering it freely is to get it tested!), so please forgive
// me if there are glaring errors.I think it is structurally sound, though
// there could be a few gotcha's I have not considered.
//
//
// GETTING STARTED
//
//
// You will need to edit this file. Search the text for "EDIT HERE". Where
// there are multiple lines to edit, I have ended these sections with "END EDIT".
//
// Here's what you need to do:
//
// Set $MyDomain to your domain name. Set $MyPath
// to the path where your cart scripts will run from.
//
// Once you have the thing up and running, you will probably want to edit the
// templates $MyItemTemplate and $MyListTemplate.
//
// You will need to edit the function fetch_details() (near the end of this file)
// so that it can retrieve the part description and price from your database.
// The code provided should work with MySql, if you set the database variables below.
//
//
// REVISION HISTORY
//
// 1.0 Initial release
// 1.01 Changes to comments, path variable, etc
// 1.1 Fix to a bug in the restore method which blew away the cookie
// Thanks to Serge T. for flagging it
//******************************************************************************
// SET YOUR DOMAIN NAME AND PATH HERE
//******************************************************************************
// EDIT HERE to set you domain name and path
$MyDomain = "www.mythicalcoffeehouse.com"; // enter your domain name here
$MyPath = "/_cart"; // enter path in domain where your scripts are run from
// END EDIT
//******************************************************************************
// IF YOU ARE USING A MySQL DATABASE TO STORE PART INFO...
//******************************************************************************
// the following information is used by the function fetch_details()
// to get information for displaying the list of items in the cart
// EDIT HERE
$MyDbName = "yourdbname"; // the name of a MySql database where part information is stored
$MyDbUser = "youruser"; // name of a user who has access to this database
$MyDbPassword = "yourpassword"; // password for the user above
$MyPartTable = "PartTbl"; // assume you have a database table where part details are stored
$MyKeyField = "PartNo"; // assume this is the name of the key field for this table
// END EDIT
//******************************************************************************
// CLASS Cart
//
// A shopping cart, stored in a cookie
//
//
// DATA MEMBERS
//
// $Lifetime length of time in seconds the cookie will exist
// $Items list of items, each item is of class CartItem
//
//
//
// METHODS
//
// restore convert cookie text back into cart
// store write to the cookie
// set add or change an item
// add add an item or add to the quantity of an existing item
// remove remove an item, or reduce the quantity of an item
// clear remove all items
// position get the line number (0 to n-1) of an item
// quantity get the quantity of a specific item
// count get the total number of lines or total quantity of items
// text get a text representation of the items in the cart
//
//******************************************************************************
class Cart
{
var $Lifetime; // length of time in seconds the cookie will exist
var $Items; // list of items, each item is of class CartItem
// NAME: Cart
// DESCRIPTION: Initialization method
function Cart()
{
// EDIT HERE if you want to change the lifetime of the cookie
$this->Lifetime = 60 * 60; // 1 hour
settype($this->Items,"array");
}
// NAME: restore
// DESCRIPTION: Restore the cart from the cookie text
function restore($CookieCart)
{
$nItems = count($this->Items); // remove any items present
array_splice($this->Items,0,$nItems);
if($CookieCart) // if a cookie is there...
{
$Lines = split(",",$CookieCart); // parse it into Items array
$nLines = count($Lines) - 1;
for($i = 0;$i < $nLines;$i++)
{
$CartItem = new CartItem; // create a new item
$CartItem->text($Lines[$i]); // set it's value from text
$this->Items[$i] = $CartItem; // store it in the list
}
}
}
// NAME: store
// DESCRIPTION: Write the cart cookie text value based on the items array
function store()
{
$CookieString = "";
$nItems = count($this->Items);
for($i = 0;$i < $nItems;$i++)
$CookieString .= $this->Items[$i]->text("") . ",";
setcookie("CookieCarter",$CookieString,time() + $this->Lifetime,$MyPath,$MyDomain);
}
// NAME: set
// DESCRIPTION: Set an item and it's quantity, deleting previous entry for item
function set($Item,$Quantity)
{
$Pos = $this->position($Item);
if($Pos < 0) // if not there already
$Pos = count($this->Items);
$CartItem = new CartItem; // store the item into the array
$CartItem->Item = $Item;
$CartItem->Quantity = $Quantity;
$this->Items[$Pos] = $CartItem;
$this->store(); // store it
}
// NAME: add
// DESCRIPTION: Add an item (or some quantity thereof) to the cart
function add($Item,$Quantity)
{
if($Quantity > 0) // must be positive quantity
{
$Quantity += $this->quantity($Item);
$this->set($Item,$Quantity);
}
}
// NAME: remove
// DESCRIPTION: Remove an item (or some quantity thereof) from the cart
function remove($Item,$Quantity)
{
if($Quantity < 0) // positive quantity means "remove this number"
{
$Pos = $this->position($Item);
if($Pos >= 0)
{
array_splice($this->Items,$Pos,1); // cut it out!
$this->store();
}
}
else
{
$Quantity = $this->quantity($Item) - $Quantity;
if($Quantity