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:- $data = array(
- 'id' => 'sku_123ABC',
- 'qty' => 1,
- 'price' => 39.95,
- 'name' => 'T-Shirt',
- 'options' => array('Size' => 'L', 'Color' => 'Red')
- );
- $this->cart->insert($data);
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.
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- function Products(){
- parent::Controller();
- //load the cart library
- //you can do this to your autoload.php
- $this->load->library('cart');
- $this->load->helper('url');
- }
- function index(){
- //pull the list of products from your database.
- $vars['pList'] = $this->productModel->getProducts();
- //pList should have your product details. (product id, description, price, product image, etc.)
- //load the view then pass the product data.
- $this->load->view('product_view', $vars);
- }
- function addToCart($productId) {
- //you need the details for the selected products to be added to your cart
- $item = $this->productModel->getProductById($productId);
- //$item variable must have the 4 required indexes necessary for inserting data to our cart.
- //sample
- /* $item = array(
- 'id' => 'sku_123ABC',
- 'qty' => 1,
- 'price' => 99.99,
- 'name' => 'Monitor',
- 'options' => array('Color' => 'Black')
- );
- */
- //insert item to cart
- $this->cart->insert($item);
- //after a user click the add to cart button in our product page we could redirect him
- //back to our product page
- redirect('products');
- //but if you use ajax to add item to your cart maybe you could use an echo fucntion.
- }
- function Cart(){
- parent::Controller();
- $this->load->library('cart');
- $this->load->helper('form');
- $this->load->helper('url');
- }
- function index(){
- //load your cart view
- $this->load->view('cart_view');
- }
- function updateCart(){
- //To update the information in your cart, you must pass an array containing the Row ID
- //and quantity to the $this->cart->update() function.
- //ID and the Quantity can be pass by submiting the form in our cart view.
- //Our cart view can generate a data like this
- //I try add an item to cart then try to submit the form and using var dump I got something like this
- /*
- Array (
- [1] => Array (
- [rowid] => 79fcbc10fafd6b1ea8202991ba15502c
- [qty] => 12 )
- [2] => Array (
- [rowid] => e942e3675eab2e6e688bdf6a851a0a54
- [qty] => 34 )
- )
- */
- if($_POST){
- $data = $_POST; //for the sake of this example we are going to use the $_POST variable directly
- }
- $this->cart->update($data);
- redirect('cart');
- }
- <?php echo form_open('cart/updatecart'); ?>
- <table cellpadding="6" cellspacing="1" style="width:100%" border="1">
- <tr>
- <th>QTY</th>
- <th>Item Description</th>
- <th style="text-align:right">Item Price</th>
- <th style="text-align:right">Sub-Total</th>
- </tr>
- <?php $i = 1; ?>
- <?php foreach($this->cart->contents() as $items): ?>
- <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
- <tr>
- <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3',
- 'size' => '5')); ?></td>
- <td>
- <?php echo $items['name']; ?>
- <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
- <p>
- <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
- <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
- <?php endforeach; ?>
- </p>
- <?php endif; ?>
- </td>
- <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
- <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
- </tr>
- <?php $i++; ?>
- <?php endforeach; ?>
- <tr>
- <td colspan="2"> </td>
- <td ><strong>Total</strong></td>
- <td >$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
- </tr>
- </table>
- <p><?php echo form_submit('', 'Update your Cart'); ?></p>
- <table>
- <tr>
- <td>Name</td>
- <td>Desc</td>
- <td>Price</td>
- <td></td>
- </tr>
- <?php foreach ($pList as $item) { ?>
- <tr>
- <td><?php echo $item['name']; ?></td>
- <td><?php echo $item['desc']; ?></td>
- <td><?php echo $item['price']; ?></td>
- <td><a href="addtocart/<?php echo $item['id']; ?>">Add To cart</a></td>
- </tr>
- <?php } ?>
- </table>
No comments:
Post a Comment