summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sturgeon <email@philsturgeon.co.uk>2011-12-14 16:46:34 +0100
committerPhil Sturgeon <email@philsturgeon.co.uk>2011-12-14 16:46:34 +0100
commitc0681aac4be7062f7a541274ff5ef7d01ec8eee2 (patch)
tree520ef3793eab302dcb5162b17c7de58a272c706c
parentcde712ce354dcfd69f9c5e2b56ef7d6f1206d11a (diff)
parent3a67ed15d984a341612af9e1dd9ba52f30d02e89 (diff)
Merge pull request #775 from andrew-s/develop
Multiple improvements to Cart.php
-rw-r--r--system/libraries/Cart.php59
-rw-r--r--user_guide_src/source/changelog.rst4
-rw-r--r--user_guide_src/source/libraries/cart.rst12
3 files changed, 67 insertions, 8 deletions
diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php
index a0e1bb91e..717ccd9fb 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,7 +196,7 @@ 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 && ! 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 +243,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 +266,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;
}
@@ -435,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;
+ }
+
// --------------------------------------------------------------------
/**
@@ -461,9 +498,19 @@ 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']);
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 6f41f4519..72b7f7e8a 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -47,6 +47,10 @@ Release Date: Not Released
- CI_Loader::_ci_autoloader() is now a protected method.
- Modified valid_ip() to use PHP's filter_var() when possible (>= PHP 5.2) in the :doc:`Form Validation library <libraries/form_validation>`.
- Added custom filename to Email::attach() as $this->email->attach($filename, $disposition, $newname)
+ - Cart library changes include;
+ - It now auto-increments quantity's instead of just resetting it, this is the default behaviour of large e-commerce sites.
+ - Product Name strictness can be disabled via the Cart Library by switching "$product_name_safe"
+ - Added function remove() to remove a cart item, updating with quantity of 0 seemed like a hack but has remained to retain compatability
- Core
diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst
index 850d7e9a8..fbf777884 100644
--- a/user_guide_src/source/libraries/cart.rst
+++ b/user_guide_src/source/libraries/cart.rst
@@ -256,6 +256,11 @@ $this->cart->update();
Permits you to update items in the shopping cart, as outlined above.
+$this->cart->remove(rowid);
+**********************
+
+Allows you to remove an item from the shopping cart by passing it the rowid.
+
$this->cart->total();
*********************
@@ -266,10 +271,13 @@ $this->cart->total_items();
Displays the total number of items in the cart.
-$this->cart->contents();
+$this->cart->contents(boolean);
************************
-Returns an array containing everything in the cart.
+Returns an array containing everything in the cart. You can sort the order,
+by which this is returned by passing it "true" where the contents will be sorted
+from newest to oldest, by leaving this function blank, you'll automatically just get
+first added to the basket to last added to the basket.
$this->cart->has_options(rowid);
*********************************