1
<?php
2
/**
3
* Based on http://www.webforcecart.com/ for updates to this script
4
*/
5
class Cart_Library {
6
public $total_price;
7
public $items = array();
8
public $item_prices = array();
9
public $item_quantities = array();
10
public $item_info = array();
11
12
public function get_cart_data() {
13
$cart_data = array();
14
foreach ($this->items as $tmp_item) {
15
$item = array();
16
$item['id'] = $tmp_item;
17
$item['quantity'] = $this->item_quantities[$tmp_item];
18
$item['price'] = $this->item_prices[$tmp_item];
19
$item['info'] = $this->item_info[$tmp_item];
20
$item['subtotal_price'] = $item['quantity'] * $item['price'];
21
$cart_data[] = $item;
22
}
23
return $cart_data;
24
}
25
26
27
public function add_item($id, $quantity, $price, $info) {
28
if (array_key_exists($id, $this->item_quantities) && $this->item_quantities[$id] > 0) {
29
// If the item is already in the cart, just increase the quantity
30
$this->item_quantities[$id] = $quantity + $this->item_quantities[$id];
31
$this->_update_total_price();
32
} else {
33
$this->items[] = $id;
34
$this->item_quantities[$id] = $quantity;
35
$this->item_prices[$id] = $price;
36
$this->item_info[$id] = $info;
37
}
38
$this->_update_total_price();
39
}
40
41
42
public function edit_item($id, $new_quantity) {
43
if ($new_quantity < 1) {
44
$this->del_item($id);
45
} else {
46
$this->item_quantities[$id] = $new_quantity;
47
}
48
$this->_update_total_price();
49
}
50
51
52
public function remove_item($id) {
53
$new_items = array();
54
$this->item_quantities[$id] = 0;
55
foreach ($this->items as $item_id) {
56
if ($item_id != $id) {
57
$new_items[] = $item_id;
58
}
59
}
60
$this->items = $new_items;
61
$this->_update_total_price();
62
}
63
64
65
public function empty_cart() {
66
$this->items = array();
67
$this->item_prices = array();
68
$this->item_quantities = array();
69
$this->item_info = array();
70
}
71
72
73
private function _update_total_price() {
74
$this->total_price = 0;
75
if (count($this->items) > 0) {
76
foreach ($this->items as $item) {
77
$this->total_price = $this->total_price + ($this->item_prices[$item] * $this->item_quantities[$item]);
78
}
79
}
80
}
81
82
}
83
84
/* End of file: ./system/libraries/cart/cart_library.php */
85
Page URI: http://www.infopotato.com/index.php/code/library/cart/
