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