Codeigniter Shopping Cart Class
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.:-
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
Here is simple example of adding an item to the cart.
Adding an item to the cart in example:-
2. Adding multiple items to the cart
This function has also same reserved indexes :-
Here is simple example of adding multiple items to the cart.
Adding multiple items to the cart in example:-
3. Updating the cart
Here is simple example of updating the cart.
Updating the cart in example:-
4. Displaying the cart
Let us create a simple example to display the cart data.
Displaying the cart in example:-
The output will be like this :–
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
This function is used to delete any particular row from the shopping cart.
It is used to display total amount of your shopping cart.
7. Total items
It is used to display total number of item in shopping cart.
This function is used to destroy the shopping cart.