After you succed create product listing page on the previous post now this is real action now, Next step we create cart controller, this controller contain important method which will provide some action in shopping cart. Those methode are add(), view_cart(), update(), delete(), etc. Let’s take a look to this code.


<?php

class cart extends Controller{

	//

	function cart(){

		parent::Controller();

		$this->load->library('cart');

	}

	//

	function index(){

		$this->view_cart();

	}

	function add(){

		$pid = $this->input->post('product_id');

		//

		$q = $this->db->get_where('product',array('product_id'=>$pid),1);

		if($q->num_rows() > 0){

			$item = $q->row();

			//

			$data = array('id' => $item->product_id,

						  'qty' => 1,

						  'price' => $item->product_price,

						  'name' => $item->product_sku."".$item->product_name

			);

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

		}

		redirect('cart/view_cart');

	}

	////

	function view_cart(){

		$data['custom_jquery'] = '
		$("input[name=\'delete\']").click(function(){
		   var status = $(this).val();
		   location.href = "'.site_url('cart/delete').'/" + status;
		})';

		$data['ptitle'] = 'CoderShop | View Cart';
		$this->load->view('view_cart',$data);

	}

	function update(){

		//Get number of items in cart

		$count = $this->cart->total_items();

		//Get info from POST

		$item = $this->input->post('rowid');

	    $qty = $this->input->post('qty');

		//Step through items

		for($i=0;$i < $count;$i++)

		{

			$data = array(

               'rowid' => $item[$i],

               'qty'   => $qty[$i]

            );

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

		}

		redirect('cart/view_cart');

	}

	function delete()

	{

		$row_id = $this->uri->segment(3,FALSE);

		$data = array('rowid'=>$row_id,

					  'qty' => 0);

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

		redirect('cart/view_cart');

	}

}

?>

Of course then create cart view called view_cart


<? $this->load->view('header');?>

<div class="content">

    <h3>Shopping Cart</h3>

    <p><b>Item Count:</b> <? echo $this->cart->total_items();?></p>

        <? echo form_open('cart/update'); ?>

     <table class="listbox" cellpadding="6" cellspacing="0" style="width:100%" border="0">

    <tr>

      <th class="tdtop">QTY</th>

      <th class="tdtop">Item Description</th>

      <th class="tdtop">Delete</th>

      <th class="tdtop" style="text-align:right">Item Price</th>

      <th class="tdtop" style="text-align:right">Subtotal</th>

    </tr>

    <? if($this->cart->total_items() > 0): ?>

        <? $i = 1; ?>

        <? foreach($this->cart->contents() as $items): ?>

            <tr>

              <td align="right">

              <? echo form_hidden('rowid[]', $items['rowid']); ?>

              <? echo form_input(array('name' => 'qty[]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>             </td>

              <td>

                <?=$items['name'];?>

                    <? if ($this->cart->has_options($items['rowid']) == TRUE): ?>

                        <p>

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

                                <strong><?=$option_name?>:</strong> <?=$option_value?><br />

                            <? endforeach; ?>

                        </p>

                    <? endif; ?>

              </td>

              <td>

                <input type="checkbox" name="delete" value="<? echo $items['rowid'];?>" />

              </td>

              <td style="text-align:right"><? echo $this->cart->format_number($items['price']); ?></td>

              <td style="text-align:right">Rp <? echo $this->cart->format_number($items['subtotal']); ?></td>

            </tr>

        <? $i++; ?>

        <? endforeach; ?>

    <tr>

      <td colspan="3"> </td>

      <td class="right"><b>Total</b></td>

      <td class="right"><b>Rp <? echo $this->cart->format_number($this->cart->total()); ?></b></td>

    </tr>

    <? endif;

        if($this->cart->total_items() < 1):

    ?>

        <tr>

            <td colspan="4">No item in your cart.</td>

        </tr>

    <? endif;?>

    </table>

    <p><?=form_submit('submit', 'Update'); ?> or <?=anchor('products','Continue Shopping');?></p>

    <?=form_close();?>

</div>

<?=$this->load->view('footer');?>

Time for checking up your shopping cart, accsess in browser http://[yourapppath]/index.php/cart, if your cart still empty try to buy an item. This is a preview of my shopping cart;

Ok your simple shopping cart is running now

if you have any suggestion or problem please write comment here :)

DOWNLOAD all code here

GHTime Code(s): b8578 ab557 7392d nc 1b5f0 7366b nc 7d08a nc 9a18e c0050 3cce7