| <?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]; |
| } |
| } |
| |
| ?> |