[317] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Cart.inc.php |
---|
| 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
| 5 | * |
---|
| 6 | * Class description goes here... |
---|
| 7 | * |
---|
| 8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 9 | * @version 1.0 |
---|
| 10 | * |
---|
| 11 | * Example of use: |
---|
| 12 | --------------------------------------------------------------------- |
---|
[333] | 13 | $cart = new Cart(); |
---|
| 14 | |
---|
| 15 | switch (getFormData('op')) { |
---|
| 16 | case 'add' : |
---|
| 17 | // User clicks an "Add to cart" button. |
---|
| 18 | if ($product_id = getFormData('product_id')) { |
---|
| 19 | $qid = $db->query("SELECT * FROM product_tbl WHERE product_id = '" . $db->escapeString($product_id) . "'"); |
---|
| 20 | if ($p = mysql_fetch_assoc($qid)) { |
---|
| 21 | $cart->add($product_id, $p['price'], 1, array( |
---|
| 22 | 'title' => $p['title'], |
---|
| 23 | 'shipping' => $p['shipping'], |
---|
| 24 | 'weight' => $p['weight'], |
---|
| 25 | )); |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | break; |
---|
| 29 | |
---|
| 30 | case 'remove' : |
---|
| 31 | // User clicks a "Remove item" button. |
---|
| 32 | $cart->remove(getFormData('product_id')); |
---|
| 33 | break; |
---|
| 34 | |
---|
| 35 | case 'qty' : |
---|
| 36 | // User changes the quantity of an item. |
---|
| 37 | $cart->setQty(getFormData('product_id'), getFormData('qty')); |
---|
| 38 | break; |
---|
| 39 | |
---|
| 40 | case 'view' : |
---|
| 41 | default : |
---|
| 42 | // User views cart. |
---|
| 43 | foreach ($cart->getList() as $item) { |
---|
| 44 | printf("Item: %s\nQty: %s\nPrice: %s\n\n", $item['title'], $item['quantity'], $item['price']); |
---|
| 45 | } |
---|
| 46 | break; |
---|
| 47 | } |
---|
[317] | 48 | --------------------------------------------------------------------- |
---|
| 49 | */ |
---|
| 50 | class Cart { |
---|
| 51 | |
---|
| 52 | // Namespace of this instance. |
---|
| 53 | var $_ns; |
---|
| 54 | |
---|
| 55 | // Configuration parameters for this object. |
---|
| 56 | var $_params = array( |
---|
| 57 | ); |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Cart constructor. |
---|
| 61 | */ |
---|
| 62 | function Cart($namespace='') |
---|
| 63 | { |
---|
| 64 | $app =& App::getInstance(); |
---|
| 65 | |
---|
| 66 | $this->_ns = $namespace; |
---|
| 67 | |
---|
| 68 | // Initialize. |
---|
| 69 | if (!isset($_SESSION['_cart'][$this->_ns])) { |
---|
| 70 | $this->clear(); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Set the params of this object. |
---|
| 76 | * |
---|
| 77 | * @param array $params Array of param keys and values to set. |
---|
| 78 | */ |
---|
| 79 | function setParam($params=null) |
---|
| 80 | { |
---|
| 81 | if (isset($params) && is_array($params)) { |
---|
| 82 | // Merge new parameters with old overriding only those passed. |
---|
| 83 | $this->_params = array_merge($this->_params, $params); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Return the value of a parameter, if it exists. |
---|
| 89 | * |
---|
| 90 | * @access public |
---|
| 91 | * @param string $param Which parameter to return. |
---|
| 92 | * @return mixed Configured parameter value. |
---|
| 93 | */ |
---|
| 94 | function getParam($param) |
---|
| 95 | { |
---|
| 96 | $app =& App::getInstance(); |
---|
| 97 | |
---|
| 98 | if (isset($this->_params[$param])) { |
---|
| 99 | return $this->_params[$param]; |
---|
| 100 | } else { |
---|
| 101 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 102 | return null; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /* |
---|
[333] | 107 | * Add an item to the cart. If called twice for one item it will add the quantities together. |
---|
[317] | 108 | * |
---|
| 109 | * @access public |
---|
[333] | 110 | * @param string $item_id A unique ID for the item |
---|
| 111 | * @param float $price Price-per-quantity-unit for the item |
---|
| 112 | * @param float $quantity Quantity of items (can be fractional unites?) |
---|
| 113 | * @param array $specs An array of additional specifications (title, weight, etc) |
---|
| 114 | * @return float The new adjusted quantity of this item. |
---|
[317] | 115 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 116 | * @version 1.0 |
---|
| 117 | * @since 11 Mar 2008 18:59:37 |
---|
| 118 | */ |
---|
[333] | 119 | function add($item_id, $price, $quantity=1, $specs=array()) |
---|
[317] | 120 | { |
---|
[333] | 121 | $app =& App::getInstance(); |
---|
[317] | 122 | |
---|
[333] | 123 | // Include any previously added items in the total quantity. |
---|
| 124 | $quantity += isset($_SESSION['_cart'][$this->_ns]['items'][$item_id]) ? $_SESSION['_cart'][$this->_ns]['items'][$item_id]['quantity'] : 0; |
---|
| 125 | |
---|
| 126 | // Add item with adjusted quantity and merge in additional specs. |
---|
| 127 | $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($specs, array( |
---|
| 128 | 'quantity' => $quantity, |
---|
| 129 | 'price' => $price, |
---|
| 130 | 'extended_price' => $quantity * $price, |
---|
| 131 | )); |
---|
| 132 | |
---|
| 133 | $app->logMsg(sprintf('Added %s %s to cart (%s)', $quantity, $item_id, truncate(getDump($specs, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 134 | |
---|
| 135 | return $quantity; |
---|
[317] | 136 | } |
---|
| 137 | |
---|
| 138 | /* |
---|
[333] | 139 | * Set the absolute quantity value of a specified item in a cart. |
---|
[317] | 140 | * |
---|
| 141 | * @access public |
---|
| 142 | * @param |
---|
| 143 | * @return |
---|
| 144 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 145 | * @version 1.0 |
---|
[333] | 146 | * @since 10 May 2008 16:42:25 |
---|
| 147 | */ |
---|
| 148 | function setQty($item_id, $quantity) |
---|
| 149 | { |
---|
| 150 | if ($quantity <= 0) { |
---|
| 151 | $this->remove($item_id); |
---|
| 152 | } else { |
---|
| 153 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { |
---|
| 154 | $item = $_SESSION['_cart'][$this->_ns]['items'][$item_id]; |
---|
| 155 | $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($item, array( |
---|
| 156 | 'quantity' => $quantity, |
---|
| 157 | 'extended_price' => $quantity * $item['price'], |
---|
| 158 | )); |
---|
[339] | 159 | return true; |
---|
| 160 | } else { |
---|
| 161 | return false; |
---|
| 162 | } |
---|
| 163 | return true; |
---|
[333] | 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* |
---|
[339] | 168 | * Set the absolute price value of a specified item in a cart. |
---|
| 169 | * |
---|
| 170 | * @access public |
---|
| 171 | * @param |
---|
| 172 | * @return |
---|
| 173 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 174 | * @version 1.0 |
---|
| 175 | * @since 10 May 2008 16:42:25 |
---|
| 176 | */ |
---|
| 177 | function setPrice($item_id, $price) |
---|
| 178 | { |
---|
| 179 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { |
---|
| 180 | $item = $_SESSION['_cart'][$this->_ns]['items'][$item_id]; |
---|
| 181 | $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($item, array( |
---|
| 182 | 'price' => $price, |
---|
| 183 | 'extended_price' => $price * $item['quantity'], |
---|
| 184 | )); |
---|
| 185 | return true; |
---|
| 186 | } else { |
---|
| 187 | return false; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /* |
---|
[333] | 192 | * Remove an item from the cart. |
---|
| 193 | * |
---|
| 194 | * @access public |
---|
| 195 | * @param string $item_id Item to remove from cart. |
---|
| 196 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 197 | * @version 1.0 |
---|
[317] | 198 | * @since 11 Mar 2008 18:59:48 |
---|
| 199 | */ |
---|
| 200 | function remove($item_id) |
---|
| 201 | { |
---|
[333] | 202 | $app =& App::getInstance(); |
---|
[317] | 203 | |
---|
[333] | 204 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { |
---|
| 205 | unset($_SESSION['_cart'][$this->_ns]['items'][$item_id]); |
---|
| 206 | $app->logMsg(sprintf('Removed %s from cart', $item_id), LOG_DEBUG, __FILE__, __LINE__); |
---|
[339] | 207 | return true; |
---|
| 208 | } else { |
---|
| 209 | return false; |
---|
[333] | 210 | } |
---|
[317] | 211 | } |
---|
| 212 | |
---|
| 213 | /* |
---|
[333] | 214 | * Return the value matching key for a specified item. |
---|
[317] | 215 | * |
---|
| 216 | * @access public |
---|
[333] | 217 | * @param string $item_id The ID of the cart item to retrieve. |
---|
| 218 | * @param string $spec_key The key of the value to return. |
---|
| 219 | * @return mixed The value, or false if not found. |
---|
[317] | 220 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 221 | * @version 1.0 |
---|
| 222 | * @since 11 Mar 2008 18:59:55 |
---|
| 223 | */ |
---|
[333] | 224 | function get($item_id, $spec_key) |
---|
| 225 | { |
---|
| 226 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id][$spec_key])) { |
---|
| 227 | return $_SESSION['_cart'][$this->_ns]['items'][$item_id][$spec_key]; |
---|
| 228 | } |
---|
| 229 | return false; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /* |
---|
| 233 | * Return an array of the items in cart. |
---|
| 234 | * |
---|
| 235 | * @access public |
---|
| 236 | * @return array Array of cart items. |
---|
| 237 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 238 | * @version 1.0 |
---|
| 239 | * @since 11 Mar 2008 18:59:55 |
---|
| 240 | */ |
---|
[317] | 241 | function getList() |
---|
| 242 | { |
---|
[333] | 243 | return $_SESSION['_cart'][$this->_ns]['items']; |
---|
[317] | 244 | } |
---|
| 245 | |
---|
| 246 | /* |
---|
[333] | 247 | * Return the sum of the numeric values stored under the specified key for cart items. |
---|
| 248 | * 0 will be returned if key is invalid or not filled with numeric values. |
---|
[317] | 249 | * |
---|
| 250 | * @access public |
---|
[333] | 251 | * @param string $key The spec key to sum. |
---|
| 252 | * @return float The total added by all the found values. |
---|
[317] | 253 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 254 | * @version 1.0 |
---|
| 255 | * @since 11 Mar 2008 19:00:12 |
---|
| 256 | */ |
---|
[333] | 257 | function sum($key='extended_price') |
---|
[317] | 258 | { |
---|
[333] | 259 | switch ($key) { |
---|
| 260 | case 'items' : |
---|
| 261 | return sizeof($_SESSION['_cart'][$this->_ns]['items']); |
---|
| 262 | |
---|
[353] | 263 | case 'extended_price' : |
---|
| 264 | $sum = 0; |
---|
| 265 | foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { |
---|
| 266 | $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] : 0; |
---|
| 267 | } |
---|
| 268 | return $sum; |
---|
[333] | 269 | default : |
---|
[353] | 270 | $sum = 0; |
---|
[333] | 271 | foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { |
---|
[336] | 272 | $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] * $specs['quantity'] : 0; |
---|
[333] | 273 | } |
---|
| 274 | return $sum; |
---|
| 275 | } |
---|
[317] | 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
| 279 | * Resets the $_SESSION cart array. This should be executed with the same consideration |
---|
| 280 | * as $auth->clear(), such as when logging out. |
---|
| 281 | */ |
---|
| 282 | function clear() |
---|
| 283 | { |
---|
| 284 | $_SESSION['_cart'][$this->_ns] = array( |
---|
| 285 | 'items' => array(), |
---|
| 286 | ); |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | |
---|
[353] | 291 | ?> |
---|