Codeigniter Shopping Cart Class– Codeigniter shopping cart class provide various functions that are used to display in a standard shopping cart format and allows user to update and delete from the cart. $this->load->library(‘cart’); is used to load the library. This library serves to create a shopping cart. Here in this tutorial, we are going to explain how to use shopping cart class.
Codeigniter shopping cart class library Example
Let us first see how to load shopping cart class library and then use its function-
Load codeigniter shopping cart class library
Syntax of shopping cart library.:-
$this->load->library('cart'); |
Functions:-
There are following functions available in shopping cart class library. Now we will explain one by one with example.
- 1. Adding an item to the cart
- 2. Adding multiple items to the cart
- 3. Updating the cart
- 4. Displaying the cart
1. Adding an item to the cart
This function has five reserved indexes :-
- Id : Every product in your store must have a unique id.
- Qty : The quantity being purchase.
- Price : Price of the item.
- Name : Name of the item.
- Options : Option are needed to identify the product
EXAMPLE
Here is simple example of adding an item to the cart.
Adding an item to the cart in example:-
//Controller <?php $attributes = array( 'id' =?> '123', 'qty' => 1, 'price' => 50.95, 'name' => 'T-Shirt', 'options' => array('Size' => 'M', 'Color' => 'Green') ); echo $this->cart->insert($attributes); ?> |
2. Adding multiple items to the cart
This function has also same reserved indexes :-
EXAMPLE
Here is simple example of adding multiple items to the cart.
Adding multiple items to the cart in example:-
//Controller <?php <h4?>Adding Multiple Items to The Cart <?php $multiple = array( array( 'id' =?> 'sku_123ABC', 'qty' => 2, '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' => 85, 'price' => 29.95, 'name' => 'Shot Glass' ) ); echo $this->cart->insert($multiple); ?> |
3. Updating the cart
EXAMPLE
Here is simple example of updating the cart.
Updating the cart in example:-
//Controller <h4>Updating The Cart</h4> <?php $attributes = array( 'rowid' =?> 'abcd12345', 'qty' => 5 ); ?> |
4. Displaying the cart
EXAMPLE
Let us create a simple example to display the cart data.
Displaying the cart in example:-
//Controller <h4>Displaying The Cart</h4> <?php echo form_open('action'); ??>cart->contents() as $items): ?>
|
The output will be like this :–
Class reference
There are many types of class reference in shopping class.
1. Product id rules
$product_id_rules = '.a-z0-9_-'
This rule is used to validate the product ID like:- alpha-numeric, dashes, underscores.
2. Product name rules
$product_name_rules = 'w -.:'
This rule is used to validate the product ID and Product Name like:- alpha-numeric, dashes, underscores.
3. Insert data
insert([$items = array()])
This rule is used to insert item details and save it to session table. Its always return boolean type.
4. Update data
update([$items = array()])
We can be used this function to update property of given item. Its also return boolean return type.
5. Remove data
remove($rowid)
This function is used to delete any particular row from the shopping cart.
6. Total
total()
It is used to display total amount of your shopping cart.
7. Total items
total_items()
It is used to display total number of item in shopping cart.
8. Destroy
destroy()
This function is used to destroy the shopping cart.