codeigniterCodeIgniter Shopping Cart

Introduction

We can utilize CI's shopping cart library when we are building a e-commerce site. we can setup add to cart, update cart items, delete cart items and even clear the cart functionalities using this library.

From CodeIgniter Doc : 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.

Adding Items In Cart

You should create functions in a controller like insert, update, delete, and clear cart etc. eg : for insert new item in cart write below code that accepts value.

$cartItem = array(
'id'      => 'MOTOG5',
'qty'     => 5,
'price'   => 100.99,
'name'    => 'Motorola Moto G5 - 16 GB',
'options' => array(
                'ram' => '3GB',
                'Color' => 'Fine Gold'
             )
);

And create functions in model for cart tasks like insert, update, delete, clear etc.

eg : for insert items in cart

$this->cart->insert($cartItem);

The insert() method will return the $rowid if you successfully insert a single item. so you can check that item has inserted or not and show related message to user.

and you can insert multiple item like this

$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);

Basic Elements of CI Shopping Cart

As we can add multiple elements in Cart array and then add it to cart session, but there are 4 basic elements which Cart class requires to add data successfully in cart session.

  1. id (string)
  2. qty (number)
  3. price (number, decimal)
  4. name (String)

And if you want to add more options regarding product then you can use 5th element which is "options". you can set array of options in this element.

It will look like this :

$cartItem = array(
    'id'      => 'MOTOG5',
    'qty'     => 5,
    'price'   => 100.99,
    'name'    => 'Motorola Moto G5 - 16 GB',
    'options' => array(
                    'ram' => '3GB',
                    'Color' => 'Fine Gold'
                 )
);

Display Cart Items

You can show cart items by loop through cart or you can display single item from cart.

$cartContents = $this->cart->contents();

This will return an array of cart items so you can loop through this array using foreach loop.

 foreach ($cartContents as $items){
     echo "ID : ". $items["id"] . "<br>";
     echo "Name : ". $items["name"] . "<br>";
     echo "Quantity : ". $items["qty"] . "<br>";
     echo "Price : ". $items["price"] . "<br>";
  }

You can format this data as table cell or some div and then show in view.

Update cart item

Rowid : 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.

Every item in cart has a rowid element and by rowid you can update cart item.

$updateItem = array(
    'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
    'qty'   => 3
);

and then below code

$this->cart->update($data);

Delete cart items

By using rowid element you can delete an item from cart. you just have to set item's qty to 0

$deleteItem = array(
    'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
    'qty'   => 0
);

$this->cart->update($data);

this will delete item with this rowid.