From f69f1822558645aa00944b436558fb6948f42aa9 Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Tue, 11 Feb 2014 22:55:47 +0200 Subject: Allow updating all properties for an item. --- system/libraries/Cart.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 5a31a1d45..b00ccd862 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -350,7 +350,11 @@ class CI_Cart { } else { - $this->_cart_contents[$items['rowid']]['qty'] = $items['qty']; + // find updatable keys + $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items)); + foreach ( $keys as $key ) { + $this->_cart_contents[$items['rowid']][$key] = $items[$key]; + } } return TRUE; -- cgit v1.2.3-24-g4f1b From 7d16de620a46f84ffeb52a54ae82439a06f89c74 Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Thu, 13 Feb 2014 01:45:27 +0200 Subject: Fixed code style & added few extra checks. Updated cart documentation. --- system/libraries/Cart.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index b00ccd862..f5e85b715 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -323,10 +323,10 @@ class CI_Cart { /** * Update the cart * - * This function permits the quantity of a given item to be changed. + * This function permits changing item properties. * Typically it is called from the "view cart" page if a user makes * changes to the quantity before checkout. That array must contain the - * product ID and quantity for each item. + * rowid and qty for each item. * * @param array * @return bool @@ -352,7 +352,18 @@ class CI_Cart { { // find updatable keys $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items)); - foreach ( $keys as $key ) { + // if a price was passed, make sure it contains valid data + if (isset($keys['price'])) + { + $keys['price'] = (float) $keys['price']; + } + + // product name & id shouldn't be changed + unset($keys['name']); + unset($keys['id']); + + foreach ($keys as $key) + { $this->_cart_contents[$items['rowid']][$key] = $items[$key]; } } -- cgit v1.2.3-24-g4f1b From 11db7a7da940a6ab0168a70b71194f078cdf953d Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Thu, 13 Feb 2014 02:40:45 +0200 Subject: Delete by values, not keys --- system/libraries/Cart.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index f5e85b715..8a2516f1c 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -353,14 +353,13 @@ class CI_Cart { // 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($keys['price'])) + if (isset($items['price'])) { - $keys['price'] = (float) $keys['price']; + $items['price'] = (float) $items['price']; } // product name & id shouldn't be changed - unset($keys['name']); - unset($keys['id']); + $keys = array_diff($keys, array('id', 'name')); foreach ($keys as $key) { -- cgit v1.2.3-24-g4f1b From 576439fda03d4e81cb5b0cfed371f7776a73fe3a Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Thu, 13 Feb 2014 06:16:31 +0200 Subject: Added changelog entry. An example to the docs. Tidy code a little bit. --- system/libraries/Cart.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'system/libraries/Cart.php') diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php index 8a2516f1c..ec67d9b4a 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -358,10 +358,8 @@ class CI_Cart { $items['price'] = (float) $items['price']; } - // product name & id shouldn't be changed - $keys = array_diff($keys, array('id', 'name')); - - foreach ($keys as $key) + // product id & name shouldn't be changed + foreach (array_diff($keys, array('id', 'name')) as $key) { $this->_cart_contents[$items['rowid']][$key] = $items[$key]; } -- cgit v1.2.3-24-g4f1b From da2bdf2b95a55f03aadfb6a1d21a90a0fff627db Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Thu, 13 Feb 2014 12:59:18 +0200 Subject: Re-arranged documentation, fixed comment. --- 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 ec67d9b4a..389b1b77e 100644 --- a/system/libraries/Cart.php +++ b/system/libraries/Cart.php @@ -326,7 +326,7 @@ class CI_Cart { * This function permits changing item properties. * 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. + * rowid and quantity for each item. * * @param array * @return bool -- cgit v1.2.3-24-g4f1b