Guest User

web2.7

a guest
Dec 26th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
  2. class Cart extends CI_Controller {
  3.     function __construct() {
  4.         parent::__construct();
  5.         $this->load->model('barang_model');    
  6.         $this->load->model('cart_model');      
  7.         $this->load->library("cart");      
  8.     }
  9.     public function index(){
  10.         $this->show();
  11.     }
  12.  
  13.     public function how_shop(){
  14.         $content= array(
  15.             'content'=> $this->load->view('cara_belanja', '', true)
  16.         );
  17.         $this->parser->parse('template', $content);
  18.     }
  19.     public function add($barang_id){
  20.         $rows= $this->barang_model->buy_select($barang_id)->row_array();
  21.         $data= array(
  22.             'id'=> $rows['id'],
  23.             'qty'=> 1,
  24.             'price'=> $rows['price'],
  25.             'name'=> $rows['name'],
  26.             'options'=> array()
  27.         );
  28.         $this->cart->insert($data);
  29.         $this->show();
  30.     }
  31.     public function edit(){
  32.         $data=array();
  33.         foreach ($this->cart->contents() as $items){
  34.             $data = array_merge(
  35.                     $data,
  36.                     array(
  37.                         array(
  38.                             'rowid'=> $this->input->post('rowid_'.$items['rowid']),
  39.                             'qty'=> $this->input->post('qty_'.$items['rowid'])
  40.                         )
  41.                     )
  42.             );
  43.         }
  44.         $this->cart->update($data);
  45.         $this->show(); 
  46.     }
  47.     public function show(){  
  48.         $cart= array(
  49.             'barangs'=> $this->cart->contents(),
  50.             'total'=> $this->cart->total()       
  51.         );
  52.         foreach($cart['barangs'] AS $keys['values']){ // foreach untuk perulangan data yang dinamis, berubah sesuai jumlah data yang ada
  53.             $cart['price'] = number_format($keys['values']['price'], 2, ',', '.');
  54.             $cart['subtotal'] = number_format($keys['values']['subtotal'], 2, ',', '.');
  55.         }
  56.         $cart['total'] = number_format($cart['total'], 2, ',', '.');
  57.        
  58.         $content= array(
  59.             'content'=> $this->parser->parse('cart_view', $cart, true)
  60.         );
  61.         $this->parser->parse('template', $content);
  62.     }
  63.     public function finished(){
  64.         /*
  65.         $this->cart->destroy();
  66.         redirect('barang/show');
  67.         */
  68.         if(!is_login()){
  69.             redirect('customer/form_login');
  70.         }else{
  71.             if(count($this->cart->contents())>0){
  72.                 $cart_number= $this->get_cart_number();
  73.                 $cart= array(
  74.                             'header' => array(
  75.                                         'cart_nomor' => $cart_number,
  76.                                         'cart_customer_id' => $this->session->userdata('customer_id'),
  77.                                         'cart_tanggal' => mdate("%Y-%m-%d %H:%i:%s", time())
  78.                             ),
  79.                             'detail' => array()
  80.                 );
  81.                 $i=0;
  82.                 foreach($this->cart->contents() AS $cart_detail){
  83.                     $cart['detail'][$i]['cart_nomor']=$cart_number;
  84.                     $cart['detail'][$i]['cart_barang_id']=$cart_detail['id'];
  85.                     $cart['detail'][$i]['cart_barang_qty']=$cart_detail['qty'];
  86.                     $cart['detail'][$i]['cart_barang_harga']=$cart_detail['price'];
  87.                     $i++;
  88.                 }
  89.                 $this->cart_model->insert($cart);
  90.                 $this->cart->destroy();
  91.                 $content= array(
  92.                             'content' => "<h2>Transaksi selesai, silakan lakukan pembayaran</h2>"
  93.                 );
  94.                 $this->parser->parse('template', $content);
  95.             }else{
  96.                 redirect();
  97.             }
  98.         }
  99.     }
  100.     private function get_cart_number(){
  101.         $customer_id= $this->session->userdata('customer_id');
  102.         $count_cart= $this->cart_model->select_where($customer_id)->num_rows()+1;
  103.         $cart_number= $customer_id.$count_cart;
  104.         return $cart_number;
  105.     }
  106. }
Add Comment
Please, Sign In to add comment