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!
|
|
|
|
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. 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!
|
|
|
|
This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product. FREE! Go There Now!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. 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!
|
|
|
|
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!
|
|
|
|
Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |