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 webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. 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!
|
|
|
|
This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. 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! |