codeigniter shopping cart

codeigniter shopping cart

codeigniter shopping cart : - 

   In this post You learn how to develope shopping cart in codeigniter,in easy way and flexable to use in any ecommerce project .

Using CodeIgniter  cart library and there methods use below database , model, view , and controller or download full code with Codeigniter 3.x 

create database phpcooker_script and import this  


--
-- Database: `phpcooker_script`
--

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

CREATE TABLE IF NOT EXISTS `product` (
  `product_id` int(20) NOT NULL AUTO_INCREMENT,
  `product_title` varchar(150) NOT NULL,
  `product_desc` text NOT NULL,
  `product_brand` varchar(100) NOT NULL,
  `product_price` decimal(12,2) NOT NULL,
  `product_image` varchar(100) NOT NULL,
  `product_status` enum('0','1') NOT NULL COMMENT '0-active,1-inactive',
  PRIMARY KEY (`product_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`product_id`, `product_title`, `product_desc`, `product_brand`, `product_price`, `product_image`, `product_status`) VALUES
(1, 'Vagitable', 'This is best Vagitable', 'test brand', '200.00', 'intro_cream_of_crop.jpg', '1'),
(2, 'Broccoli', 'Broccoli is best in test', 'new brand', '250.00', 'broccoli.jpg', '1'),
(3, 'cucumbers', 'Friesh cucumbers for saled', 'test brand', '150.00', 'cucumbers.jpg', '1'),
(4, 'carrots', 'carrots is testy', 'new brand', '300.00', 'carrots.jpg', '1');

Shoppingcart_model.php 


<?php 
defined("BASEPATH") or exit("no direct script allowed");
class Shoppingcart_model extends CI_Model{
    function __construct(){
    parent::__construct();    
        $this->load->library(array('session','cart'));
        $this->load->library('');
        $this->load->helper('url');
        $this->load->database();    
    }
    
    
    public function product_list(){
        
        $this->db->select('*');
        $this->db->from('product');
        $this->db->order_by('product_id','desc');
        $rs = $this->db->get();
        return $rs->result_array();
    }
    
    public function product_byId($pid){
        
        $this->db->select('*');
        $this->db->from('product');
        $this->db->where('product_id',$pid);
        $rs = $this->db->get();
        return $rs->row_array();
    }

}
?>

Controller  Shoppingcart.php 



<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Shoppingcart extends CI_Controller {
    public function __construct(){
        parent :: __construct();
        $this->load->model('Shoppingcart_model',"sc");
    }
    public function index()
    {
        $data['datas'] = $this->sc->product_list();
        $this->load->view('shoppingcart',$data);
    }
    
    
    public function ajax_cart(){
        
        $action = $this->input->post('action');
        if($action == 'add'){
            $pid    = $this->input->post('p_id');
            $qty    = $this->input->post('quantity');
            
            $product = $this->sc->product_byId($pid);
            $pdata = array(
                'id'      => $product['product_id'],
                'qty'     => $qty,
                'price'   => $product['product_price'],
                'name'    => $product['product_title'],
                "image"   =>$product['product_image'],
            );
        
            $this->cart->insert($pdata);
        }
        
        if($action == 'delete' ){
            $rowid = $this->input->post('rowid');
            $pdata = array(
            'rowid' => $rowid,
            'qty'   => 0,
            );
            $this->cart->update($pdata);
        }
        
        if($action == 'empty' ){
            $this->cart->destroy();
        }
?>

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <img src="cart.png" style="width:40px" > <?php echo count(@$this->cart->contents()); ?> - Items<span class="caret"></span></a>
          <ul class="dropdown-menu dropdown-cart" role="menu">
          <?php 
          if($this->cart->contents()){
          foreach($this->cart->contents() as $data){
          ?>
              <li>
                  <span class="item">
                    <span class="item-left" style="color:#0000" >
                        <img src="images/<?php echo $data['image']; ?>" alt="" style="width:60px;" />
                        <span class="item-info" style="color:#000;" >
                            <span><?php echo $data['name']; ?></span>
                            <span>Quantity : <?php echo $data['qty']; ?></span>
                            <span>Price : <?php echo $data['price']; ?> INR</span>
                            <span>Subtotal : <?php echo $data['subtotal']; ?> INR</span>
                        </span>
                    </span>
                    <span class="item-right">
                        <button class="btn btn-xs btn-danger pull-right" onclick="remove_cart('<?php echo $data['rowid']; ?>')" >x</button>
                    </span>
                </span>
              </li>
          <?php }  } ?>
              <li class="divider"></li>
              <li><a class="text-center" href="javascript:void(0)" onclick="empty_cart();" >Empty Cart</a></li>


<?php        
    }    
        
}

?>

view shoppingcart.php


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Shopping Cart in CodeIgniter and jquery </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  
/* Set height of the grid so .sidenav can be 100% (adjust if needed) */
    .row.content {height: 1500px}
    
    /* Set gray background color and 100% height */
    .sidenav {
      background-color: #f1f1f1;
      height: 100%;
    }
    
    /* Set black background color, white text and some padding */
    footer {
      background-color: #555;
      color: white;
      padding: 15px;
    }
    
    /* On small screens, set height to 'auto' for sidenav and grid */
    @media screen and (max-width: 767px) {
      .sidenav {
        height: auto;
        padding: 15px;
      }
      .row.content {height: auto;} 
    }

ul.dropdown-cart{
    min-width:250px;
}
ul.dropdown-cart li .item{
    display:block;
    padding:3px 10px;
    margin: 3px 0;
}
ul.dropdown-cart li .item:hover{
    background-color:#f3f3f3;
}
ul.dropdown-cart li .item:after{
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}

ul.dropdown-cart li .item-left{
    float:left;
}
ul.dropdown-cart li .item-left img,
ul.dropdown-cart li .item-left span.item-info{
    float:left;
}
ul.dropdown-cart li .item-left span.item-info{
    margin-left:10px;   
}
ul.dropdown-cart li .item-left span.item-info span{
    display:block;
}
ul.dropdown-cart li .item-right{
    float:right;
}
ul.dropdown-cart li .item-right button{
    margin-top:14px;
}

.navbar-default .navbar-nav > li > a {
    color: #000;
    font-size:30px;
}
.navbar {
    margin-bottom: 0px; 

}
  
  </style>
</head>
<body>


<div class="container-fluid">
  <div class="row">
  
  <div class="col-sm-12" ><>
  <nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
  <div class="navbar-header">
     
      <a class="navbar-brand" href="#">Add To Cart Example</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class=" navbar" >      
      <ul class="nav navbar-nav navbar-right" style="margin-right:5%;" >
        <li class="dropdown cart_data"  >
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <img src="cart.png" style="width:40px" > 0 - Items<span class="caret"></span></a>
      
      
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
  </div>
   </div> 
  
 <div class="row content" style="padding-top:2%;" >  
   <!---- <div class="col-sm-3 sidenav">
      
    </div>--->

    <div class="col-sm-12">
    <br /><br />
    <br /><br />
      <h2>add to cart in php using session and jquery</h2>
      <hr/>
      
      <div class="container">
    <div class="row">
    
    <?php 
    foreach($datas as $data){
    ?>
        <div class="col-md-3">
              <div class="thumbnail">
                <img src="<?php echo base_url(); ?>images/<?php echo $data['product_image']; ?>" alt="" class="img-responsive">
                <div class="caption">
                  <h4 class="pull-right"><?php echo $data['product_price']; ?></h4>
                  <h4><a href="#"><?php echo $data['product_title']; ?></a></h4>
                  <p><?php echo $data['product_desc']; ?></p>
                </div>
                <div class="ratings" style="text-align:center;" >
                  Quantity:  <input type="number" class="quantity<?php echo $data['product_id']; ?>" min="1"  style="max-width:50px;" value="1" >
                </div>
                <br />
                <div class="space-ten"></div>
                <div class="btn-ground text-center">
                    
                    <button type="button" class="btn btn-primary" onclick="add_cart('<?php echo $data['product_id']; ?>')" ><i class="fa fa-shopping-cart"></i> Add To Cart</button>
                   
                </div>
                <div class="space-ten"></div>
              </div>
            </div>
    <?php } ?>
            
            
                       
    </div>
</div>

      
      
    </div>
  </div>
</div>

<script>

function add_cart(p_id=""){
    
    var quantity = $(".quantity"+p_id).val();
    $.ajax({
        type:"post",
        url:"<?php echo base_url(); ?>shoppingcart/ajax_cart",
        data:{action:'add',p_id:p_id,quantity:quantity},
        success:function(result){
            $('.cart_data').html(result);
        
        }
    });
}


add_cart();


function remove_cart(rowid){
    $.ajax({
        type:"post",
        url:"<?php echo base_url(); ?>shoppingcart/ajax_cart",
        data:{action:'delete',rowid:rowid},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}

function empty_cart(){
    $.ajax({
        type:"post",
        url:"<?php echo base_url(); ?>shoppingcart/ajax_cart",
        data:{action:'empty'},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}

</script>

</body>
</html>

smileysmileysmileysmiley

Demo

Download Hare

Total Coments (1)

  1. greeni
    • 21 Nov 2017

    error,waste of time