From ac69d42f0b5d46adb06fa1283f85e937392561d9 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Tue, 17 Feb 2009 02:28:10 +0000 Subject: Added Cart Class to docs --- user_guide/libraries/cart.html | 381 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 381 insertions(+) create mode 100644 user_guide/libraries/cart.html (limited to 'user_guide/libraries/cart.html') diff --git a/user_guide/libraries/cart.html b/user_guide/libraries/cart.html new file mode 100644 index 000000000..7c709d392 --- /dev/null +++ b/user_guide/libraries/cart.html @@ -0,0 +1,381 @@ + + + + + +Shopping Cart Class : CodeIgniter User Guide + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.7

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

Shopping Cart Class

+ +

The Cart Class permits items to be added to a session that stays active while a user is browsing your site. +These items can be retrieved and displayed in a standard "shopping cart" format, allowing the user to update the quantity or remove items from the cart.

+ +

Please note that the Cart Class ONLY provides the core "cart" functionality. It does not provide shipping, credit card authorization, or other processing components.

+ + +

Initializing the Shopping Cart Class

+ +

Important: The Cart class utilizes CodeIgniter's +Session Class to save the cart information to a database, so before using the Cart class you must set up a database table +as indicated in the Session Documentation , and set the session preferences in your appliction/config/config.php file to utilize a database.

+ +

To initialize the Shopping Cart Class in your controller constructor, use the $this->load->library function:

+ +$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 automatically, so unless you are using sessions elsewhere in your application, you do not need to load the Session class.

+ +

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 below:

+ + +$data = array(
+               'id'      => 'sku_123ABC',
+               'qty'     => 1,
+               'price'   => 39.95,
+               'name'    => 'T-Shirt',
+               'options' => array('Size' => 'L', 'Color' => 'Red')
+            );
+
+ +$this->cart->insert($data); + +
+ +

Important: The first four array indexes above (id, qty, price, and name) are required. +If you omit any of them the data will not be saved to the cart. The fifth index (options) is optional. +It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.

+ +

The five reserved indexes are:

+ + + +

In addition to the five indexes above, there are two reserved words: rowid and subtotal. These are used internally by the Cart class, so +please do NOT use those words as index names when inserting data into the cart.

+ +

Your array may contain additional data. Anything you include in your 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.

+ + +

Adding Multiple Items to The Cart

+ +

By using a multi-dimensional array, as shown below, it is possible to add multiple products to the cart in one action. This is useful in cases where you wish to allow +people to select from among several items on the 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'
+                    )
+ +            );
+
+ +$this->cart->insert($data); + +
+ + +

Updating The Cart

+ +

To update the information in your cart, pass an array containing the product ID and quantity to the $this->cart->update() function:

+ + + +$data = array(
+               'id'  => '123XYZ',
+               'qty' => 3
+            );
+
+ +$this->cart->update($data); + +
+ +

Just as you can when inserting, you may use a multi-dimensional array in order to update multiple items simultaneously:

+ + + +$data = array(
+ +               array(
+                       'id'      => 'sku_123ABC',
+                       'qty'     => 3
+                    ),
+ +               array(
+                       'id'      => 'sku_567ZYX',
+                       'qty'     => 4
+                    ),
+ +               array(
+                       'id'      => 'sku_965QRS',
+                       'qty'     => 2
+                    )
+ +            );
+
+ +$this->cart->update($data); + +
+ + + + +

Deleting an Item From The Cart

+ +

To delete an item from your cart, pass an array containing the product ID and a quantity of zero to the $this->cart->update() function:

+ + + +$data = array(
+               'id'  => '123XYZ',
+               'qty' => 0
+            );
+
+ +$this->cart->update($data); + +
+ + +

You may use a multi-dimensional array in order to delete multiple items simultaneously:

+ + + +$data = array(
+ +               array(
+                       'id'      => 'sku_123ABC',
+                       'qty'     => 0
+                    ),
+ +               array(
+                       'id'      => 'sku_567ZYX',
+                       'qty'     => 0
+                    ),
+ +               array(
+                       'id'      => 'sku_965QRS',
+                       'qty'     => 0
+                    )
+ +            );
+
+ +$this->cart->update($data); + +
+ + + +

Displaying the Cart

+ +

To display the cart you will create a view file with code similar to the one shown below.

+ +

Please note that this example uses the form helper.

+ + + + + + +

 

+ + +

Function Reference

+ +

$this->cart->insert();

+ +

Permits you to add items to the shopping cart, as outlined above.

+ + +

$this->cart->update();

+ +

Permits you to update items in the shopping cart, as outlined above.

+ + +

$this->cart->total();

+ +

Displays the total amount in the cart.

+ + +

$this->cart->total_items();

+ +

Displays the total number of items in the cart.

+ + +

$this->cart->contents();

+ +

Returns an array containing everything in the cart.

+ + + +

$this->cart->has_options(rowid);

+ +

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.

+ + +

$this->cart->options(rowid);

+ +

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.

+ + + +

$this->cart->destroy();

+ +

Permits you to destroy the cart. This function will likely be called when you are finished processing the customer's order.

+ + + + +
+ + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b