Tutorialsplane

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

How to load 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

This function has five reserved indexes :-

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): ?>
Quantity Material Description Material Price Sub-Total
$i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?> cart->has_options($items['rowid']) == TRUE): ?>

cart->product_options($items['rowid']) as $option_name => $option_value): ?> :

cart->format_number($items['price']); ?> $cart->format_number($items['subtotal']); ?>
Total $cart->format_number($this->cart->total()); ?>

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.

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference