Monday, 30 May 2016

Loading

Codeigniter Shopping Cart Class

Code igniter Shopping Cart Class 
           
A shopping cart is a software package and Ecommerce application that acts as an online store’s catalog and ordering process.

When a user is browsing your site at that time Cart Class allows the items to be added to a session that stays active. These items can be retrieved and displayed in a standard “shopping cart” format.

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 Code Igniter’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 application/config/config.php file to utilize a database.

Initializing the Shopping Cart Class:

To initialize the Shopping Cart Class in your controller constructor, use the $this->load->library function:




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


This function allows you to add items to the shopping cart, insert() method will return the unique $rowid if you successfully insert a single item

$insert_data = array( 'id' => $this->input->post('id'),
 'name' => $this->input->post('name'),
 'price' => $this->input->post('price'),
'qty' => 1 );
 
 // This function add items into cart.
$this->cart->insert($insert_data);







Add multiple Items:
$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);
Update Cart :
 
$data = array(

               'rowid' => 'b99ccdf16028f015540f341130b6d8ec',

               'qty'   => 3

            );



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



// Or a multi-dimensional array



$data = array(

               array(

                       'rowid'   => 'b99ccdf16028f015540f341130b6d8ec',

                       'qty'     => 3

                    ),

               array(

                       'rowid'   => 'xw82g9q3r495893iajdh473990rikw23',

                       'qty'     => 4

                    ),

               array(

                       'rowid'   => 'fh4kdkkkaoe30njgoe92rkdkkobec333',

                       'qty'     => 2

                    )

            );







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



Example:

Database :
CREATE TABLE `products` (
  `id` int(128) NOT NULL auto_increment,
  `name` varchar(128) NOT NULL,
  `price` varchar(32) NOT NULL,
  `image` varchar(128) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
 
Controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {  
  function __construct()
  {    parent::__construct();
  }   
         public function index()
         {    
         $this->load->helper('form');
         $this->load->helper('url');    
    // Load the cart library to use it.
    $this->load->library('cart');    
         /*
    // To add items in your shopping cart
    $data = array(
               'id'      => 'sku_999',
               'qty'     => 5,
               'price'   => 99.85,
               'name'    => 'iPad4',
               'options' => array('Storage' => '32GB', 'Color' => 'White')
            );
    $this->cart->insert($data);    
    // You can also put multiple data to your cart
    $data = array(
               array(
                       'id'      => 'sku_888',
                       'qty'     => 1,
                       'price'   => 39.95,
                       'name'    => 'T-Shirt'                       
                    ),
               array(
                       'id'      => 'sku_777',
                       'qty'     => 1,
                       'price'   => 9.95,
                       'name'    => 'Coffee Mug'
                    ),
               array(
                       'id'      => 'sku_666',
                       'qty'     => 1,
                       'price'   => 29.95,
                       'name'    => 'Shot Glass'
                    )
            );
    $this->cart->insert($data);
         */       
         // Assuming this is the products from our database
                 $data['products'] = array(
               array(
                       'id'      => 'sku_888',
                       'qty'     => 1,
                       'price'   => 39.95,
                       'name'    => 'T-Shirt'
                       
                    ),
               array(
                       'id'      => 'sku_777',
                       'qty'     => 1,
                       'price'   => 9.95,
                       'name'    => 'Coffee Mug'
                    ),
               array(
                       'id'      => 'sku_666',
                       'qty'     => 1,
                       'price'   => 29.95,
                       'name'    => 'Shot Glass'
                    )
            );                     
         // Insert the product to cart
         if ($this->input->get('id') != '')
         {
                  $this->cart->insert($data['products'][$this->input->get('id')]);
         }        
         // Lets update our cart
         if ($this->input->post('update_cart'))
         {
                 unset($_POST['update_cart']);
                 $contents = $this->input->post();
                 
                 foreach ($contents as $content)
                 {
                          $info = array(
                    'rowid' => $content['rowid'],
                    'qty'   => $content['qty']
                           );
                          $this->cart->update($info);
                 }
         }    
           $this->load->view('welcome_message', $data);
         }    
}
 
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
 
 
View :
<html><head></head><body>
<b>Products</b>
 
<table width="100%" border="1">
  
  <tr>
    <td width="37%">ID</td>
    <td width="30%">Name</td>
    <td width="16%">Price</td>
    <td width="16%">&nbsp;</td>
  </tr>
  <?php $key = 0;?>
  <?php foreach($products as $product):?>
  <tr>
    <td><?php echo $product['id'];?></td>
    <td><?php echo $product['name'];?></td>
    <td><?php echo $product['price'];?></td>
    <td><a href="?id=<?php echo $key;?>">Add to Cart</a></td>
  </tr>
  <?php $key ++;?>
  <?php endforeach;?>
</table>
 
<hr>
 
 
<b>Your Cart</b>
 
<?php echo form_open(base_url()); ?>
 
<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 class="right"><strong>Total</strong></td>
  <td class="right" align="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
 
</table>
 
<p><?php echo form_submit('update_cart', 'Update your Cart'); ?></p>
 




Other Function :
$this->cart->insert();
Permits you to add items to the shopping cart, as outlined above.
$this->cart->update();
Permits you to update items in the shopping cart, as outlined above.
$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 therowid to this function, as shown in the Displaying the Cart example above.
$this->cart->product_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.







No comments:

Post a Comment