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.