summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/libraries/cart.rst
diff options
context:
space:
mode:
Diffstat (limited to 'user_guide_src/source/libraries/cart.rst')
-rw-r--r--user_guide_src/source/libraries/cart.rst319
1 files changed, 203 insertions, 116 deletions
diff --git a/user_guide_src/source/libraries/cart.rst b/user_guide_src/source/libraries/cart.rst
index 716e94bcb..d7d495967 100644
--- a/user_guide_src/source/libraries/cart.rst
+++ b/user_guide_src/source/libraries/cart.rst
@@ -11,7 +11,16 @@ Please note that the Cart Class ONLY provides the core "cart"
functionality. It does not provide shipping, credit card authorization,
or other processing components.
-.. contents:: Page Contents
+.. contents::
+ :local:
+
+.. raw:: html
+
+ <div class="custom-index container"></div>
+
+********************
+Using the Cart Class
+********************
Initializing the Shopping Cart Class
====================================
@@ -24,12 +33,12 @@ Initializing the Shopping Cart Class
utilize a database.
To initialize the Shopping Cart Class in your controller constructor,
-use the $this->load->library function::
+use the ``$this->load->library()`` method::
$this->load->library('cart');
Once loaded, the Cart object will be available using::
-
+
$this->cart
.. note:: The Cart Class will load and initialize the Session Class
@@ -40,16 +49,16 @@ Adding an Item to The Cart
==========================
To add an item to the shopping cart, simply pass an array with the
-product information to the $this->cart->insert() function, as shown
+product information to the ``$this->cart->insert()`` method, as shown
below::
$data = array(
- 'id' => 'sku_123ABC',
- 'qty' => 1,
- 'price' => 39.95,
- 'name' => 'T-Shirt',
- 'options' => array('Size' => 'L', 'Color' => 'Red')
- );
+ 'id' => 'sku_123ABC',
+ 'qty' => 1,
+ 'price' => 39.95,
+ 'name' => 'T-Shirt',
+ 'options' => array('Size' => 'L', 'Color' => 'Red')
+ );
$this->cart->insert($data);
@@ -79,7 +88,19 @@ 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.
-The insert() method will return the $rowid if you successfully insert a
+::
+
+ $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.
Adding Multiple Items to The Cart
@@ -93,26 +114,26 @@ same page.
::
$data = array(
- array(
- 'id' => 'sku_123ABC',
- 'qty' => 1,
- 'price' => 39.95,
- 'name' => 'T-Shirt',
- 'options' => array('Size' => 'L', 'Color' => 'Red')
- ),
- array(
- 'id' => 'sku_567ZYX',
- 'qty' => 1,
- 'price' => 9.95,
- 'name' => 'Coffee Mug'
- ),
- array(
- 'id' => 'sku_965QRS',
- 'qty' => 1,
- 'price' => 29.95,
- 'name' => 'Shot Glass'
- )
- );
+ array(
+ 'id' => 'sku_123ABC',
+ 'qty' => 1,
+ 'price' => 39.95,
+ 'name' => 'T-Shirt',
+ 'options' => array('Size' => 'L', 'Color' => 'Red')
+ ),
+ array(
+ 'id' => 'sku_567ZYX',
+ 'qty' => 1,
+ 'price' => 9.95,
+ 'name' => 'Coffee Mug'
+ ),
+ array(
+ 'id' => 'sku_965QRS',
+ 'qty' => 1,
+ 'price' => 29.95,
+ 'name' => 'Shot Glass'
+ )
+ );
$this->cart->insert($data);
@@ -127,15 +148,15 @@ helper </helpers/form_helper>`.
::
- <?php echo form_open('path/to/controller/update/function'); ?>
+ <?php echo form_open('path/to/controller/update/method'); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
- <th>QTY</th>
- <th>Item Description</th>
- <th style="text-align:right">Item Price</th>
- <th style="text-align:right">Sub-Total</th>
+ <th>QTY</th>
+ <th>Item Description</th>
+ <th style="text-align:right">Item Price</th>
+ <th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
@@ -145,9 +166,9 @@ helper </helpers/form_helper>`.
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
- <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
- <td>
- <?php echo $items['name']; ?>
+ <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
+ <td>
+ <?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
@@ -161,9 +182,9 @@ helper </helpers/form_helper>`.
<?php endif; ?>
- </td>
- <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
- <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
+ </td>
+ <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
+ <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
@@ -171,21 +192,21 @@ helper </helpers/form_helper>`.
<?php endforeach; ?>
<tr>
- <td colspan="2"> </td>
- <td class="right"><strong>Total</strong></td>
- <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
+ <td colspan="2"> </td>
+ <td class="right"><strong>Total</strong></td>
+ <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('', 'Update your Cart'); ?></p>
-
+
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:
+containing the Row ID and quantity to the ``$this->cart->update()``
+method.
.. note:: If the quantity is set to zero, the item will be removed from
the cart.
@@ -193,38 +214,52 @@ function:
::
$data = array(
- 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
- 'qty' => 3
- );
+ 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
+ 'qty' => 3
+ );
- $this->cart->update($data);
+ $this->cart->update($data);
// Or a multi-dimensional array
$data = array(
- array(
- 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
- 'qty' => 3
- ),
- array(
- 'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
- 'qty' => 4
- ),
- array(
- 'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
- 'qty' => 2
- )
- );
+ array(
+ 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
+ 'qty' => 3
+ ),
+ array(
+ 'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
+ 'qty' => 4
+ ),
+ array(
+ 'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
+ 'qty' => 2
+ )
+ );
+
+ $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.
+
+::
+
+ $data = array(
+ 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
+ 'qty' => 1,
+ 'price' => 49.95,
+ 'coupon' => NULL
+ );
$this->cart->update($data);
What is a Row ID?
*****************
-The row ID is a unique identifier that is
-generated by the cart code when an item is added to the cart. The reason
-a unique ID is created is so that identical products with different
-options can be managed by the cart.
+The row ID is a unique identifier that is generated by the cart code
+when an item is added to the cart. The reason a unique ID is created
+is so that identical products with different options can be managed
+by the cart.
For example, let's say someone buys two identical t-shirts (same product
ID), but in different sizes. The product ID (and other attributes) will
@@ -236,73 +271,125 @@ the product ID and any options associated with it.
In nearly all cases, updating the cart will be something the user does
via the "view cart" page, so as a developer, it is unlikely that you
-will ever have to concern yourself with the "row ID", other then making
+will ever have to concern yourself with the "row ID", other than making
sure your "view cart" page contains this information in a hidden form
-field, and making sure it gets passed to the update function when the
-update form is submitted. Please examine the construction of the "view
-cart" page above for more information.
+field, and making sure it gets passed to the ``update()`` method when
+the update form is submitted. Please examine the construction of the
+"view cart" page above for more information.
+
+
+***************
+Class Reference
+***************
+
+.. class:: CI_Cart
+
+ .. attribute:: $product_id_rules = '\.a-z0-9_-'
+
+ These are the regular expression rules that we use to validate the product
+ ID - alpha-numeric, dashes, underscores, or periods by default
+
+ .. attribute:: $product_name_rules = '\w \-\.\:'
+
+ These are the regular expression rules that we use to validate the product ID and product name - alpha-numeric, dashes, underscores, colons or periods by
+ default
+
+ .. attribute:: $product_name_safe = TRUE
+
+ Whether or not to only allow safe product names. Default TRUE.
+
+
+ .. method:: insert([$items = array()])
+
+ :param array $items: Items to insert into the cart
+ :returns: TRUE on success, FALSE on failure
+ :rtype: bool
+
+ Insert items into the cart and save it to the session table. Returns TRUE
+ on success and FALSE on failure.
+
+
+ .. method:: update([$items = array()])
+
+ :param array $items: Items to update in the cart
+ :returns: TRUE on success, FALSE on failure
+ :rtype: bool
+
+ 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.
+
+ .. method:: remove($rowid)
+
+ :param int $rowid: ID of the item to remove from the cart
+ :returns: TRUE on success, FALSE on failure
+ :rtype: bool
+
+ Allows you to remove an item from the shopping cart by passing it the
+ ``$rowid``.
+
+ .. method:: total()
+ :returns: Total amount
+ :rtype: int
-Function Reference
-==================
+ Displays the total amount in the cart.
-$this->cart->insert();
-**********************
-Permits you to add items to the shopping cart, as outlined above.
+ .. method:: total_items()
-$this->cart->update();
-**********************
+ :returns: Total amount of items in the cart
+ :rtype: int
-Permits you to update items in the shopping cart, as outlined above.
+ Displays the total number of items in the cart.
-$this->cart->remove(rowid);
-***************************
-Allows you to remove an item from the shopping cart by passing it the rowid.
+ .. method:: contents([$newest_first = FALSE])
-$this->cart->total();
-*********************
+ :param bool $newest_first: Whether to order the array with newest items first
+ :returns: An array of cart contents
+ :rtype: array
-Displays the total amount in the cart.
+ Returns an array containing everything in the cart. You can sort the
+ order by which the array is returned by passing it TRUE where the contents
+ will be sorted from newest to oldest, otherwise it is sorted from oldest
+ to newest.
-$this->cart->total_items();
-***************************
+ .. method:: get_item($row_id)
-Displays the total number of items in the cart.
+ :param int $row_id: Row ID to retrieve
+ :returns: Array of item data
+ :rtype: array
-$this->cart->contents(boolean);
-*******************************
+ Returns an array containing data for the item matching the specified row
+ ID, or FALSE if no such item exists.
-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.
+ .. method:: has_options($row_id = '')
-$this->cart->get_item($row_id);
-*******************************
+ :param int $row_id: Row ID to inspect
+ :returns: TRUE if options exist, FALSE otherwise
+ :rtype: bool
-Returns an array containing data for the item matching the specified row ID,
-or FALSE if no such item exists.
+ Returns TRUE (boolean) if a particular row in the cart contains options.
+ This method is designed to be used in a loop with ``contents()``, since
+ you must pass the rowid to this method, as shown in the Displaying
+ the Cart example above.
-$this->cart->has_options($row_id);
-**********************************
+ .. method:: product_options([$row_id = ''])
-Returns TRUE (boolean) if a particular row in the cart contains options.
-This function is designed to be used in a loop with
-$this->cart->contents(), since you must pass the rowid to this function,
-as shown in the Displaying the Cart example above.
+ :param int $row_id: Row ID
+ :returns: Array of product options
+ :rtype: array
-$this->cart->product_options($row_id);
-**************************************
+ Returns an array of options for a particular product. This method is
+ designed to be used in a loop with ``contents()``, since you
+ must pass the rowid to this method, as shown in the Displaying the
+ Cart example above.
-Returns an array of options for a particular product. This function is
-designed to be used in a loop with $this->cart->contents(), since you
-must pass the rowid to this function, as shown in the Displaying the
-Cart example above.
+ .. method:: destroy()
-$this->cart->destroy();
-***********************
+ :rtype: void
-Permits you to destroy the cart. This function will likely be called
-when you are finished processing the customer's order.
+ Permits you to destroy the cart. This method will likely be called
+ when you are finished processing the customer's order. \ No newline at end of file