This is a nifty little shopping cart I made. Fully customizable.
By : voldomazta
<?php
/**
* + Shopping Cart Class
* + (c) 2006 Ramon Alivio Jr. (http://www.ramon.ph)
* + on 2006-08-03
**/
class Cart extends Site {
/**
* The serialized array of items
* @var string
**/
var $items;
/**
* The name of the cart
* @var string
**/
var $CART_NAME;
/**
* void Cart
* + Instantiates the class, sets the cart name
* + to your cart name and sets the items array for use
* -----
* @param string : The name of the cart
**/
function Cart($cn)
{
if (empty($cn)) return false;
$this->CART_NAME = preg_replace("/([^a-zA-Z|\s]+)/","",$cn);
if (!empty($_COOKIE[$this->CART_NAME])) {
$this->items = base64_decode($_COOKIE[$this->CART_NAME]);
$this->items = unserialize($this->items);
} else {
$this->items = array();
}
}
/**
* void add_item
* + This function adds the items to the shopping cart array
* + and serializes them
* -----
* @param int : ID of the item
* @param string : Name of the item
* @param int : Quantity of the item
* @param double : Price of the item
* @param string : Additional Notes
**/
function add_item($id, $n, $q, $p, $an='')
{
if ($this->is_zero($id, $q, $p) || empty($n)) return;
if ($this->in_cart($id)) $this->items[$id]['quantity'] += $q;
else $this->items[$oid] = array('id' => $id, 'name' => $n, 'quantity' => $q, 'price' => $p, 'notes' => $an);
$this->update_cart();
}
/**
* void remove_item
* + This function removes an item from the cart with the
* + specified "id" and "quantity" and updates the cart afterwards
* -----
* @param int : ID of the item to be removed
* @param int : Quantity of the item to be removed
**/
function remove_item($id, $q)
{
$cq = $this->items[$id]['quantity'];
if (!$this->in_cart($id)) return;
if ($q > $cq) $this->items[$id]['quantity'] = 0;
else $this->items[$id]['quantity'] -= $q;
if ($cq == 0) unset($this->items[$id]);
$this->update_cart();
}
/**
* void update_quantity
* + This function updates the quantity of a given item
* + with the specified "id" and quantity", if the given
* + quantity is 0 or less, the item is removed from the cart
* -----
* @param int : ID of the item to be updated
* @param int : New quantity of the item
**/
function update_quantity($id, $nq)
{
if (!$this->in_cart($id)) return;
$this->items[$id]['quantity'] = $nq;
$this->update_cart();
}
/**
* boolean in_cart
* + This function returns whether the given item exists
* + on the shopping cart
* -----
* @param int : ID of the item to be checked
**/
function in_cart($id)
{
return is_array($this->items[$id]) ? true : false;
}
/**
* int item_count
* + This item returns the total number of items in the cart
* -----
* @param
**/
function item_count()
{
$count = 0;
foreach ($this->items as $v) $count += $v['quantity'];
return $count;
}
/**
* void update_cart
* + This function updates the shopping cart, if a certain
* + quantity for an item is 0, it removes it from the cart
* -----
* @param
**/
function update_cart()
{
foreach ($this->items as $k=>$v) if ($v['quantity'] == 0) unset($this->items[$k]);
$temp = base64_encode(serialize($this->items));
setcookie($this->CART_NAME,'',time()-1);
setcookie($this->CART_NAME,$temp,time()+30*24*60*60);
}
/**
* double total_price
* + This function returns the total price of all the items
* + in the cart
* -----
* @param
**/
function total_price()
{
$total = 0;
foreach ($this->items as $v) $total += $v['quantity']*$v['price'];
return $total;
}
/**
* void empty_cart
* + This function empties the shoppig cart of all its items
* -----
* @param
**/
function empty_cart()
{
foreach ($this->items as $k=>$v) unset($this->items[$k]);
$this->update_cart();
}
/**
* boolean is_zero
* + This function checks whether there is a 0 value
* + from all the arguments given to it
* -----
* @param mixed : Set of numbers => is_zero(3,0,2)
**/
function is_zero()
{
foreach (func_get_args() as $arg) if ((int)$arg != 0) return false;
return true;
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |