* Copyright 2001-2010 Strangecode, LLC * * This file is part of The Strangecode Codebase. * * The Strangecode Codebase is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * The Strangecode Codebase is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * The Strangecode Codebase. If not, see . */ /** * Cart.inc.php * * Class description goes here... * * @author Quinn Comendant * @version 1.0 * * Example of use: --------------------------------------------------------------------- $cart = new Cart(); switch (getFormData('op')) { case 'add' : // User clicks an "Add to cart" button. if ($product_id = getFormData('product_id')) { $qid = $db->query("SELECT * FROM product_tbl WHERE product_id = '" . $db->escapeString($product_id) . "'"); if ($p = mysql_fetch_assoc($qid)) { $cart->add($product_id, $p['price'], 1, array( 'title' => $p['title'], 'shipping' => $p['shipping'], 'weight' => $p['weight'], )); } } break; case 'remove' : // User clicks a "Remove item" button. $cart->remove(getFormData('product_id')); break; case 'qty' : // User changes the quantity of an item. $cart->setQty(getFormData('product_id'), getFormData('qty')); break; case 'view' : default : // User views cart. foreach ($cart->getList() as $item) { printf("Item: %s\nQty: %s\nPrice: %s\n\n", $item['title'], $item['quantity'], $item['price']); } break; } --------------------------------------------------------------------- */ class Cart { // Namespace of this instance. var $_ns; // Configuration parameters for this object. var $_params = array( ); /** * Cart constructor. */ function Cart($namespace='') { $app =& App::getInstance(); $this->_ns = $namespace; // Initialize. if (!isset($_SESSION['_cart'][$this->_ns])) { $this->clear(); } } /** * Set the params of this object. * * @param array $params Array of param keys and values to set. */ function setParam($params=null) { if (isset($params) && is_array($params)) { // Merge new parameters with old overriding only those passed. $this->_params = array_merge($this->_params, $params); } } /** * Return the value of a parameter, if it exists. * * @access public * @param string $param Which parameter to return. * @return mixed Configured parameter value. */ function getParam($param) { $app =& App::getInstance(); if (isset($this->_params[$param])) { return $this->_params[$param]; } else { $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); return null; } } /* * Add an item to the cart. If called twice for one item it will add the quantities together. * * @access public * @param string $item_id A unique ID for the item * @param float $price Price-per-quantity-unit for the item * @param float $quantity Quantity of items (can be fractional unites?) * @param array $specs An array of additional specifications (title, weight, etc) * @return float The new adjusted quantity of this item. * @author Quinn Comendant * @version 1.0 * @since 11 Mar 2008 18:59:37 */ function add($item_id, $price, $quantity=1, $specs=array()) { $app =& App::getInstance(); // Include any previously added items in the total quantity. $quantity += isset($_SESSION['_cart'][$this->_ns]['items'][$item_id]) ? $_SESSION['_cart'][$this->_ns]['items'][$item_id]['quantity'] : 0; // Add item with adjusted quantity and merge in additional specs. $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($specs, array( 'quantity' => $quantity, 'price' => $price, 'extended_price' => $quantity * $price, )); $app->logMsg(sprintf('Added %s %s to cart (%s)', $quantity, $item_id, truncate(getDump($specs, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); return $quantity; } /* * Set the absolute quantity value of a specified item in a cart. * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 10 May 2008 16:42:25 */ function setQty($item_id, $quantity) { if ($quantity <= 0) { $this->remove($item_id); } else { if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { $item = $_SESSION['_cart'][$this->_ns]['items'][$item_id]; $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($item, array( 'quantity' => $quantity, 'extended_price' => $quantity * $item['price'], )); return true; } else { return false; } return true; } } /* * Set the absolute price value of a specified item in a cart. * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 10 May 2008 16:42:25 */ function setPrice($item_id, $price) { if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { $item = $_SESSION['_cart'][$this->_ns]['items'][$item_id]; $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($item, array( 'price' => $price, 'extended_price' => $price * $item['quantity'], )); return true; } else { return false; } } /* * Remove an item from the cart. * * @access public * @param string $item_id Item to remove from cart. * @author Quinn Comendant * @version 1.0 * @since 11 Mar 2008 18:59:48 */ function remove($item_id) { $app =& App::getInstance(); if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { unset($_SESSION['_cart'][$this->_ns]['items'][$item_id]); $app->logMsg(sprintf('Removed %s from cart', $item_id), LOG_DEBUG, __FILE__, __LINE__); return true; } else { return false; } } /* * Return the value matching key for a specified item. * * @access public * @param string $item_id The ID of the cart item to retrieve. * @param string $spec_key The key of the value to return. * @return mixed The value, or false if not found. * @author Quinn Comendant * @version 1.0 * @since 11 Mar 2008 18:59:55 */ function get($item_id, $spec_key) { if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id][$spec_key])) { return $_SESSION['_cart'][$this->_ns]['items'][$item_id][$spec_key]; } return false; } /* * Return an array of the items in cart. * * @access public * @return array Array of cart items. * @author Quinn Comendant * @version 1.0 * @since 11 Mar 2008 18:59:55 */ function getList() { return $_SESSION['_cart'][$this->_ns]['items']; } /* * Return the sum of the numeric values stored under the specified key for cart items. * 0 will be returned if key is invalid or not filled with numeric values. * Most commonly, $cart->sum() will be called when checking out to retrieve the total * cost of the items in the cart. * * @access public * @param string $key The spec key to sum. * @return float The total added by all the found values. * @author Quinn Comendant * @version 1.0 * @since 11 Mar 2008 19:00:12 */ function sum($key='extended_price') { global $app; $sum = 0; switch ($key) { case 'items' : $sum = sizeof($_SESSION['_cart'][$this->_ns]['items']); break; case 'extended_price' : foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] : 0; } break; default : // Retreive arbitrary values stored in the cart (shipping, air miles, etc). foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] * $specs['quantity'] : 0; } break; } return $sum; } /** * Resets the $_SESSION cart array. This should be executed with the same consideration * as $auth->clear(), such as when logging out. */ function clear() { $_SESSION['_cart'][$this->_ns] = array( 'items' => array(), ); } } ?>