From 2e5bda3bde171e71306c80479f04d886127d88a2 Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Tue, 13 Dec 2011 11:40:15 +0000 Subject: Updated Cart.php to handle quantity changes differently (now auto-increments), also updated the changelog to reflect this change for the people that are currently using this library --- system/libraries/Cart.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index a0e1bb91e..13485a3ee 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -41,6 +41,7 @@ class CI_Cart { // These are the regular expression rules that we use to validate the product ID and product name var $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods + var $product_name_safe = true; // only allow safe product names // Private variables. Do not change! var $CI; @@ -195,10 +196,13 @@ class CI_Cart { // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods. // Note: These can be user-specified by setting the $this->product_name_rules variable. - if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) + if($this->product_name_safe) { - log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces'); - return FALSE; + if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) + { + log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces'); + return FALSE; + } } // -------------------------------------------------------------------- @@ -242,7 +246,18 @@ class CI_Cart { // -------------------------------------------------------------------- // Now that we have our unique "row ID", we'll add our cart items to the master array - + // grab quantity if it's already there and add it on + if(isset($this->_cart_contents[$rowid]['qty'])) + { + // set our old quantity + $old_quantity = (int)$this->_cart_contents[$rowid]['qty']; + } + else + { + // we have no old quantity but - we don't want to throw an error + $old_quantity = 0; + } + // let's unset this first, just to make sure our index contains only the data from this submission unset($this->_cart_contents[$rowid]); @@ -254,7 +269,10 @@ class CI_Cart { { $this->_cart_contents[$rowid][$key] = $val; } - + + // add old quantity back in + $this->_cart_contents[$rowid]['qty'] = ($this->_cart_contents[$rowid]['qty'] + $old_quantity); + // Woot! return $rowid; } -- cgit v1.2.3-24-g4f1b From 3dd66631330841e4d0e29a95fc279a5d5cc921f2 Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Tue, 13 Dec 2011 15:50:52 +0000 Subject: Merged the two if's together as suggested by @philsturgeon - updated the Changelog to include something that may be important --- system/libraries/Cart.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 13485a3ee..f9f3bca47 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -196,13 +196,10 @@ class CI_Cart { // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods. // Note: These can be user-specified by setting the $this->product_name_rules variable. - if($this->product_name_safe) + if ( $this->product_name_safe && ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) { - if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) - { - log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces'); - return FALSE; - } + log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces'); + return FALSE; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From fefff9fcdfc26676b8c57653c3d8176b729901b2 Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Tue, 13 Dec 2011 15:52:50 +0000 Subject: Spacing added, @philsturgeon is too picky! --- system/libraries/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index f9f3bca47..997b9b8de 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -244,7 +244,7 @@ class CI_Cart { // Now that we have our unique "row ID", we'll add our cart items to the master array // grab quantity if it's already there and add it on - if(isset($this->_cart_contents[$rowid]['qty'])) + if( isset($this->_cart_contents[$rowid]['qty'])) { // set our old quantity $old_quantity = (int)$this->_cart_contents[$rowid]['qty']; -- cgit v1.2.3-24-g4f1b From de2e96a298cc48b8e86a03d530bc328bd43b0c80 Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Tue, 13 Dec 2011 16:44:59 +0000 Subject: Added the ability to get the contents in a different order --- system/libraries/Cart.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 997b9b8de..2e580863b 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -476,9 +476,17 @@ class CI_Cart { * @access public * @return array */ - function contents() + function contents($newest_first = false) { - $cart = $this->_cart_contents; + // do we want the newest first? + if($newest_first) + { + // reverse the array + $cart = array_reverse($this->_cart_contents); + } else { + // just added first to last + $cart = $this->_cast_contents; + } // Remove these so they don't create a problem when showing the cart table unset($cart['total_items']); -- cgit v1.2.3-24-g4f1b From 17aebce8e06fae0edf1d54cf6823f1b8f48924c0 Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Tue, 13 Dec 2011 16:57:13 +0000 Subject: Changed the syntax slightly, @philsturgeon may be claustrophobic as he doesn't like no spaces --- system/libraries/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 2e580863b..e78ecd5f6 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -244,7 +244,7 @@ class CI_Cart { // Now that we have our unique "row ID", we'll add our cart items to the master array // grab quantity if it's already there and add it on - if( isset($this->_cart_contents[$rowid]['qty'])) + if (isset($this->_cart_contents[$rowid]['qty'])) { // set our old quantity $old_quantity = (int)$this->_cart_contents[$rowid]['qty']; -- cgit v1.2.3-24-g4f1b From f75ec11d7ed0592f930dd3a090db7c1c6d251eed Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Wed, 14 Dec 2011 09:36:39 +0000 Subject: Added in a remove item from cart function to save the "hacky" method of updating a product with 0 quantity, updated the changelog to reflect and to be neater and more readable surrounding the changes of the Cart library --- system/libraries/Cart.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index e78ecd5f6..7f6cdf5dd 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -450,7 +450,29 @@ class CI_Cart { { return $this->_cart_contents['cart_total']; } - + + // -------------------------------------------------------------------- + + /** + * Remove Item + * + * Removes an item from the cart + * + * @access public + * @return boolean + */ + public function remove($rowid) + { + // just do an unset + unset($this->_cart_contents[$rowid]); + + // we need to save the cart now we've made our changes + $this->_save_cart(); + + // completed + return true; + } + // -------------------------------------------------------------------- /** -- cgit v1.2.3-24-g4f1b From 3a67ed15d984a341612af9e1dd9ba52f30d02e89 Mon Sep 17 00:00:00 2001 From: Andrew Seymour Date: Wed, 14 Dec 2011 15:35:06 +0000 Subject: Slight syntax change to the Cart class --- system/libraries/Cart.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 7f6cdf5dd..717ccd9fb 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -505,7 +505,9 @@ class CI_Cart { { // reverse the array $cart = array_reverse($this->_cart_contents); - } else { + } + else + { // just added first to last $cart = $this->_cast_contents; } -- cgit v1.2.3-24-g4f1b