Using this we can create Shopping cart features in an application.
CI Provide cart library to implement this functionality.
Step to Implement Card Functionality:-
1) Create Controller
2 ) Load cart library under Controller Constructor
$this->load->library('cart')
3) Create Controller Index method and Add Multiple Items into cart
4) Create View and show cart content
Complete Code of Cart:-
<?php
class Shoppingcart extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('cart');
}
function index()
{
$this->cart->destroy();
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 100,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
),
array(
'id' => 'sku_567ZYX',
'qty' => 1,
'price' => 1200,
'name' => 'Shoes'
),
array(
'id' => 'sku_965QRS',
'qty' => 1,
'price' => 2000,
'name' => 'Watch'
));
$this->cart->insert($data);
$this->load->view('cartview');
}
function updatecart()
{
$data = array(
array(
'rowid' => '0256a32c98ce49afbe2a4eb8c96c5884',
'qty' => $this->input->post('txt1')
),
array(
'rowid' => '90972f7cfcd380a9fa7821d30a9b2fb2',
'qty' => $this->input->post('txt2')
),
array(
'rowid' => '46acd2fb2e0d0b4a29c67e7ddf1c8946',
'qty' => $this->input->post('txt3')
)
);
$this->cart->update($data);
$this->load->view('cartview');
}
}
?>
Cartview:-
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="<?php echo site_url(); ?>/shoppingcart/updatecart" method="post" >
<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' => 'txt'.$i, '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 class="right"><strong>Total</strong></td>
<td class="right"><?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
<tr><td><input type="submit" name="btnupload" value="UpdateCart" /></td></tr>
</table>
</form>
</body>
</html>
Post a Comment
POST Answer of Questions and ASK to Doubt