Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Cart extends MY_Controller {
  4.     private $id;
  5.     public function __construct() {
  6.         parent::__construct();
  7.         $is_login = $this->session->userdata('is_login');
  8.         $this->id = $this->session->userdata('id');
  9.         if (!$is_login) {   // Jika ternyata belum ada session
  10.             redirect(base_url());
  11.             return;
  12.         }
  13.     }
  14.  
  15.     public function index() {
  16.         $data['title']      = 'Keranjang Belanja';
  17.         $data['content']    = $this->cart->select([
  18.             'cart.id', 'cart.qty', 'cart.subtotal',
  19.             'product.title', 'product.image', 'product.price'
  20.         ])
  21.             ->join('product')
  22.             ->where('cart.id_user', $this->id)
  23.             ->get();
  24.         $data['page']       = 'pages/cart/index';
  25.  
  26.         return $this->view($data);
  27.     }
  28.  
  29.     /**
  30.      * Menambah produk beserta kuantitasnya di home
  31.      */
  32.     public function add() {
  33.         if (!$_POST || $this->input->post('qty') < 1) {
  34.             $this->session->set_flashdata('error', 'Kuantitas tidak boleh kosong');
  35.             redirect(base_url());
  36.         } else {
  37.             $input              = (object) $this->input->post(null, true);
  38.             // Mengambil data produk yang dipilih, untuk mendapatkan price
  39.             $this->cart->table  = 'product';
  40.             $product            = $this->cart->where('id', $input->id_product)->first();
  41.             // Ambil cart untuk dicek apakah user sudah pesan
  42.             $this->cart->table  = 'cart';
  43.             $cart               = $this->cart->where('id_user', $this->id)->where('id_product', $input->id_product)->first();
  44.             $subtotal           = $product->price * $input->qty;
  45.             if ($cart) {    // Jika ternyata user sudah pesan, maka update cart
  46.                 $data = [
  47.                     'qty'       => $cart->qty + $input->qty,
  48.                     'subtotal'  => $cart->subtotal + $subtotal
  49.                 ];
  50.                 if ($this->cart->where('id', $cart->id)->update($data)) {   // Jika update berhasil
  51.                     $this->session->set_flashdata('success', 'Produk berhasil ditambahkan');
  52.                 } else {
  53.                     $this->session->set_flashdata('error', 'Oops! Terjadi kesalahan');
  54.                 }
  55.                 redirect(base_url());
  56.             }
  57.             // --- Insert cart baru ---
  58.             $data = [
  59.                 'id_user'       => $this->id,
  60.                 'id_product'    => $input->id_product,
  61.                 'qty'           => $input->qty,
  62.                 'subtotal'      => $subtotal
  63.             ];
  64.             if ($this->cart->create($data)) {   // Jika insert berhasil
  65.                 $this->session->set_flashdata('success', 'Produk berhasil ditambahkan');
  66.             } else {
  67.                 $this->session->set_flashdata('error', 'Maaf, terjadi kesalahan');
  68.             }
  69.             redirect(base_url());
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * Update kuantitas di keranjang belanja
  75.      */
  76.     public function update($id) {
  77.         if (!$_POST || $this->input->post('qty') < 1) {
  78.             $this->session->set_flashdata('error', 'Kuantitas tidak boleh kosong');
  79.             redirect(base_url('cart/index'));
  80.         }
  81.         $data['content']    = $this->cart->where('id', $id)->first();   // Mengambil data dari cart
  82.         if (!$data['content']) {
  83.             $this->session->set_flashdata('warning', 'Data tidak ditemukan');
  84.             redirect(base_url('cart/index'));
  85.         }
  86.         // Mengambil data produk yang dipilih, untuk mendapatkan price
  87.         $this->cart->table  = 'product';
  88.         $product            = $this->cart->where('id', $data['content']->id_product)->first();
  89.         // Menghitung subtotal baru
  90.         $data['input']      = (object) $this->input->post(null, true);
  91.         $subtotal           = $data['input']->qty * $product->price;
  92.         // Update data
  93.         $cart = [
  94.             'qty'       => $data['input']->qty,
  95.             'subtotal'  => $subtotal
  96.         ];
  97.         $this->cart->table  = 'cart';
  98.         if ($this->cart->where('id', $id)->update($cart)) {   // Jika update berhasil
  99.             $this->session->set_flashdata('success', 'Kuantitas berhasil diubah');
  100.         } else {
  101.             $this->session->set_flashdata('error', 'Maaf, terjadi kesalahan');
  102.         }
  103.         redirect(base_url('cart/index'));
  104.     }
  105.  
  106.     /**
  107.      * Delete suatu cart di halaman cart
  108.      */
  109.     public function delete($id) {
  110.         if (!$_POST) {
  111.             // Jika diakses tidak dengan menggunakan method post, kembalikan ke home (forbidden)
  112.             redirect(base_url('cart/index'));
  113.         }
  114.         if (!$this->cart->where('id', $id)->first()) {  // Jika data tidak ditemukan
  115.             $this->session->set_flashdata('warning', 'Maaf data tidak ditemukan');
  116.             redirect(base_url('cart/index'));
  117.         }
  118.         if ($this->cart->where('id', $id)->delete()) {  // // Lakukan delete & Jika delete berhasil
  119.             $this->session->set_flashdata('success', 'Cart berhasil dihapus');
  120.         } else {
  121.             $this->session->set_flashdata('error', 'Maaf, terjadi suatu kesalahan');
  122.         }
  123.         redirect(base_url('cart/index'));
  124.     }
  125. }
  126.