Tutorials

Building a Shopping Cart using CodeIgniter’s Shopping Cart Class

Jan 17, 2010 insic 29 Comments

Late last year CodeIgniter v1.7.2 was released with a lot of improvements and bug fixes. This version is now compatible with PHP5.3.0, they added is_php() to Common functions to facilitate PHP version comparisons, modified show_error() to allow sending of HTTP server response codes, and all internal uses now send proper status codes, Form helper improved and a new class which we are about to cover in this tutorial the Shopping Cart Class.

Class Overview from CodeIgniter User Guide section.

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.

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

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:

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:

  • id – Each product in your store must have a unique identifier. Typically this will be an “sku” or other such identifier.
  • qty – The quantity being purchased.
  • price – The price of the item.
  • name - The name of the item.
  • options – Any additional attributes that are needed to identify the product. These must be passed via an array.

Cart Class Function reference
Function Reference

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

Permits you to add items to the shopping cart.

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

Permits you to update items in the shopping cart.

» $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.

Lets Make our Simple Shopping Cart

Lets start to by creating our Product controller

And then our cart controller

And finally our Cart View

And then a sample of a product View. What is important here is show how our addtocart function being called.

Subscribe to my RSS for more.

About the author: insic

Subscribe in my RSS Feed for more updates on Web Design and Development related articles. Follow me on twitter or drop a message to my inbox.