1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
 
// ---------------------------------------------------
// Made By: Nick George, 2009
// Last Updated: March 02, 2009 09:50 PM
// Filename: ShoppingCart.php
// Description: Portable class allowing you to create
//                            a SESSION based shopping cart.
// ---------------------------------------------------
 
// NOTES:
//       1. If using SESSION's to record shopping cart, make
//              sure the session has already been started before
//              calling this class.
 
class ShoppingCart
{
       private $name;
 
       // Adds the ability to have a whole cart quantity instead of individual item quantities
       private $iscartquantity = false;
 
       public function __construct($cart_name, $iscartquantity)
       {
              // This will be the name of the SESSION variable
              $this->name = $cart_name;
 
              $this->iscartquantity = $iscartquantity;
 
              // Check and see if shopping cart already exists, if it doesn't, set the array to the variable
              if($_SESSION[$this->name] == null)
                     $_SESSION[$this->name] = array();
       }
 
       // When adding an item, you input a name, quantity of item, then an array for other options
       //       Item Array:
       //              Name = array (
       //                     1 => Quantity
       //                     2 => Array of values for given item
       //              )
       //
       //       Return Values:
       //       Will throw an error if item already exists (return 0)
       public function addItem($name, $quantity, $values = null)
       {
              try
              {
                     if(!array_key_exists($name, $_SESSION[$this->name]))
                            $_SESSION[$this->name][$name] = array($quantity, $values);
                     else
                            throw new Exception();
              }
              catch (Exception $e)
              {
                     return 0;
              }
       }
 
       // Updates an existing item in the cart
       // Return values:
       // 1 on success, 0 if error (usually being that the item does not exist)
       public function updateItem($name, $quantity, $values = null)
       {
              try
              {
                     if(array_key_exists($name, $_SESSION[$this->name]))
                            $_SESSION[$this->name][$name] = array($quantity, $values);
                     else
                            throw new Exception();
              }
              catch (Exception $e)
              {
                     return 0;
              }              
       }
 
       // Updates the quantity of an existing item in the cart
       // Return values:
       // 1 on success, 0 if error (usually being that the item does not exist)
       public function updateItemQuantity($name, $quantity)
       {
              try
              {
                     if(array_key_exists($name, $_SESSION[$this->name]))
                            $_SESSION[$this->name][$name][0] = $quantity;
                     else
                            throw new Exception();
              }
              catch (Exception $e)
              {
                     return 0;
              }              
       }
 
       // Updates the quantity of the entire cart
       // Return values:
       // 1 on success, 0 if error (because not using CartWide Quantity)
       public function setCartQuantity($quantity)
       {
              try
              {
                     if($this->iscartquantity && $quantity > 0)
                            $_SESSION[$this->name]["quantity"] = $quantity;
                     else
                            throw new Exception();
              }
              catch (Exception $e)
              {
                     return 0;
              }              
       }
 
       // Retrieves the quantity of the entire cart
       // Return values:
       // 1 on success, 0 if error (because not using CartWide Quantity)
       public function getCartQuantity()
       {
              try
              {
                     if($this->iscartquantity)
                            return (empty($_SESSION[$this->name]["quantity"])) ? 1 : (int)$_SESSION[$this->name]["quantity"];
                     else
                            throw new Exception();
              }
              catch (Exception $e)
              {
                     return 0;
              }              
       }
 
       // This will delete an item out of the cart
       // Return values:
       //       None
       public function deleteItem($name)
       {
              unset($_SESSION[$this->name][$name]);
       }
 
       // Clears all of the items from the cart
       // Return values:
       //       None
       public function clearCart()
       {
              $_SESSION[$this->name] = array();
       }
       
       // Get a single item
       // Return values:
       //       0 if item does not exist
       public function getItem($name)
       {
              try
              {
                     if(array_key_exists($name, $_SESSION[$this->name]))
                            return $_SESSION[$this->name][$name];
                     else
                            throw new Exception();
              }
              catch (Exception $e)
              {
                     return 0;
              }
       }
       
       // Counts number of items in cart
       // Return values:
       //       0 if item does not exist
       public function getItemCount()
       {
              if($this->iscartquantity)
                     // -1 because of the quantity variable being in there, which results in an extra value
                     return count($_SESSION[$this->name]) - 1;
              else
                     return count($_SESSION[$this->name]);
       }
 
       // Returns the cart in array form
       public function getCart()
       {
              return $_SESSION[$this->name];
       }
}
 
?>