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(-) 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 ++++++++++++++--- user_guide_src/source/libraries/cart.rst | 10 ++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) 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]; } } diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst index fb92c280a..015f1c90e 100644 --- a/user_guide_src/source/libraries/cart.rst +++ b/user_guide_src/source/libraries/cart.rst @@ -194,7 +194,9 @@ 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() -function: +function, you may also update any property you have previously +defined when inserting the item such like (options, price +or other custom fields you defined). .. note:: If the quantity is set to zero, the item will be removed from the cart. @@ -289,10 +291,10 @@ Class Reference :returns: TRUE on success, FALSE on failure :rtype: bool - This method permits the quantity of a given item to be changed. + 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 product ID - and quantity for each item. + to the quantity before checkout. That array must contain the rowid + and qty for each item. .. method:: remove($rowid) -- 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(-) 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 ++---- user_guide_src/source/changelog.rst | 1 + user_guide_src/source/libraries/cart.rst | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) 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]; } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index b5b31dcc2..76ee7ed31 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -303,6 +303,7 @@ Release Date: Not Released - Added method ``remove()`` to remove a cart item, updating with quantity of 0 seemed like a hack but has remained to retain compatibility. - Added method ``get_item()`` to enable retrieving data for a single cart item. - Added unicode support for product names. + - ``update()`` now supports updating all properties attached to an item. - :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 015f1c90e..6d0dd2e3a 100644 --- a/user_guide_src/source/libraries/cart.rst +++ b/user_guide_src/source/libraries/cart.rst @@ -88,6 +88,18 @@ array will be stored in the session. However, it is best to standardize your data among all your products in order to make displaying the information in a table easier. +:: + + $data = array( + 'id' => 'sku_123ABC', + 'qty' => 1, + 'price' => 39.95, + 'name' => 'T-Shirt', + 'coupon' => 'XMAS-50OFF' + ); + + $this->cart->insert($data); + The insert() method will return the $rowid if you successfully insert a single item. @@ -195,8 +207,8 @@ 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() function, you may also update any property you have previously -defined when inserting the item such like (options, price -or other custom fields you defined). +defined when inserting the item such as options, price +or other custom fields you defined. .. note:: If the quantity is set to zero, the item will be removed from the cart. @@ -205,6 +217,7 @@ or other custom fields you defined). $data = array( 'rowid' => 'b99ccdf16028f015540f341130b6d8ec', + 'price' => 10, 'qty' => 3 ); -- 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 +- user_guide_src/source/libraries/cart.rst | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) 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 diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst index 6d0dd2e3a..a0870e34a 100644 --- a/user_guide_src/source/libraries/cart.rst +++ b/user_guide_src/source/libraries/cart.rst @@ -206,9 +206,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() -function, you may also update any property you have previously -defined when inserting the item such as options, price -or other custom fields you defined. +function. .. note:: If the quantity is set to zero, the item will be removed from the cart. @@ -217,7 +215,6 @@ or other custom fields you defined. $data = array( 'rowid' => 'b99ccdf16028f015540f341130b6d8ec', - 'price' => 10, 'qty' => 3 ); @@ -242,6 +239,20 @@ or other custom fields you defined. $this->cart->update($data); +You may also update any property you have previously +defined when inserting the item such as options, price +or other custom fields you defined. + +:: + $data = array( + 'rowid' => 'b99ccdf16028f015540f341130b6d8ec', + 'qty' => 1, + 'price' => 49.95, + 'coupon' => NULL + ); + + $this->cart->update($data); + What is a Row ID? ***************** -- cgit v1.2.3-24-g4f1b From c09b953be9d14158cc21cecc676f7e992c16f752 Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Thu, 13 Feb 2014 13:05:41 +0200 Subject: Added Missing new line. --- user_guide_src/source/libraries/cart.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst index a0870e34a..74e791554 100644 --- a/user_guide_src/source/libraries/cart.rst +++ b/user_guide_src/source/libraries/cart.rst @@ -244,6 +244,7 @@ defined when inserting the item such as options, price or other custom fields you defined. :: + $data = array( 'rowid' => 'b99ccdf16028f015540f341130b6d8ec', 'qty' => 1, -- cgit v1.2.3-24-g4f1b