From 4321cb0912ad24d78b851a9f39c688cab0341c96 Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Fri, 14 Mar 2014 15:21:13 +0200 Subject: Removed the requirment of quantity to trigger cart update. --- system/libraries/Cart.php | 49 ++++++++++++++++---------------- user_guide_src/source/changelog.rst | 1 + user_guide_src/source/libraries/cart.rst | 4 +-- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 5b05974e4..6cc1509b6 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -282,10 +282,10 @@ class CI_Cart { // You can either update a single product using a one-dimensional array, // or multiple products using a multi-dimensional one. The way we - // determine the array type is by looking for a required array key named "id". + // determine the array type is by looking for a required array key named "rowid". // If it's not found we assume it's a multi-dimensional array $save_cart = FALSE; - if (isset($items['rowid'], $items['qty'])) + if (isset($items['rowid'])) { if ($this->_update($items) === TRUE) { @@ -296,7 +296,7 @@ class CI_Cart { { foreach ($items as $val) { - if (is_array($val) && isset($val['rowid'], $val['qty'])) + if (is_array($val) && isset($val['rowid'])) { if ($this->_update($val) === TRUE) { @@ -332,37 +332,38 @@ class CI_Cart { protected function _update($items = array()) { // Without these array indexes there is nothing we can do - if ( ! isset($items['qty'], $items['rowid'], $this->_cart_contents[$items['rowid']])) + if ( ! isset($items['rowid'], $this->_cart_contents[$items['rowid']])) { return FALSE; } // Prep the quantity - $items['qty'] = (float) $items['qty']; - - // Is the quantity zero? If so we will remove the item from the cart. - // If the quantity is greater than zero we are updating - if ($items['qty'] == 0) - { - unset($this->_cart_contents[$items['rowid']]); - } - else + if ( isset($items['qty']) ) { - // find updatable keys - $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items)); - // if a price was passed, make sure it contains valid data - if (isset($items['price'])) + $items['qty'] = (float) $items['qty']; + // Is the quantity zero? If so we will remove the item from the cart. + // If the quantity is greater than zero we are updating + if ($items['qty'] == 0) { - $items['price'] = (float) $items['price']; - } - - // product id & name shouldn't be changed - foreach (array_diff($keys, array('id', 'name')) as $key) - { - $this->_cart_contents[$items['rowid']][$key] = $items[$key]; + unset($this->_cart_contents[$items['rowid']]); + return TRUE } + } + + // find updatable keys + $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items)); + // if a price was passed, make sure it contains valid data + if (isset($items['price'])) + { + $items['price'] = (float) $items['price']; } + // product id & name shouldn't be changed + foreach (array_diff($keys, array('id', 'name')) as $key) + { + $this->_cart_contents[$items['rowid']][$key] = $items[$key]; + } + return TRUE; } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 36ddbc92e..55146308f 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -318,6 +318,7 @@ Release Date: Not Released - Added support for disabling product name strictness via the ``$product_name_safe`` property. - Changed ``insert()`` method to auto-increment quantity for an item when inserted twice instead of resetting it. - Changed ``update()`` method to support updating all properties attached to an item. + - Removed the requirment of quantity to trigger ``update()``. - :doc:`Image Manipulation Library ` changes include: diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst index d7d495967..0ccb6e2f6 100644 --- a/user_guide_src/source/libraries/cart.rst +++ b/user_guide_src/source/libraries/cart.rst @@ -205,7 +205,7 @@ Updating The Cart ================= To update the information in your cart, you must pass an array -containing the Row ID and quantity to the ``$this->cart->update()`` +containing the Row ID and one or more pre-defined properties to the ``$this->cart->update()`` method. .. note:: If the quantity is set to zero, the item will be removed from @@ -318,7 +318,7 @@ Class Reference This method permits changing the properties of a given item. Typically it is called from the "view cart" page if a user makes changes to the quantity before checkout. That array must contain the rowid - and qty for each item. + for each item. .. method:: remove($rowid) -- cgit v1.2.3-24-g4f1b From 9af0746825a1af1c463462c10bdff70f58ded72a Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Fri, 14 Mar 2014 15:33:18 +0200 Subject: Added missing semicolon --- system/libraries/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 6cc1509b6..9c9a285cd 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -346,7 +346,7 @@ class CI_Cart { if ($items['qty'] == 0) { unset($this->_cart_contents[$items['rowid']]); - return TRUE + return TRUE; } } -- cgit v1.2.3-24-g4f1b From 2702a3b647785c06bf21ba0b6b36cacef2f8ee1f Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Fri, 14 Mar 2014 16:53:44 +0200 Subject: Fixed coding style & line length. --- system/libraries/Cart.php | 2 +- user_guide_src/source/changelog.rst | 3 +-- user_guide_src/source/libraries/cart.rst | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 9c9a285cd..cd46a6ef6 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -338,7 +338,7 @@ class CI_Cart { } // Prep the quantity - if ( isset($items['qty']) ) + if (isset($items['qty'])) { $items['qty'] = (float) $items['qty']; // Is the quantity zero? If so we will remove the item from the cart. diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 55146308f..6f246e3a0 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -317,8 +317,7 @@ Release Date: Not Released - Added unicode support for product names. - Added support for disabling product name strictness via the ``$product_name_safe`` property. - Changed ``insert()`` method to auto-increment quantity for an item when inserted twice instead of resetting it. - - Changed ``update()`` method to support updating all properties attached to an item. - - Removed the requirment of quantity to trigger ``update()``. + - Changed ``update()`` method to support updating all properties attached to an item and not to require 'qty'. - :doc:`Image Manipulation Library ` changes include: diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst index 0ccb6e2f6..bedea4dbf 100644 --- a/user_guide_src/source/libraries/cart.rst +++ b/user_guide_src/source/libraries/cart.rst @@ -205,8 +205,8 @@ Updating The Cart ================= To update the information in your cart, you must pass an array -containing the Row ID and one or more pre-defined properties to the ``$this->cart->update()`` -method. +containing the Row ID and one or more pre-defined properties to the +``$this->cart->update()`` method. .. note:: If the quantity is set to zero, the item will be removed from the cart. -- cgit v1.2.3-24-g4f1b