Cart.php
5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
class Cart{
private static $ins = null;
private $items = array();
final protected function __construct() {
}
final protected function __clone() {
}
public static function getIns() {
if (!(self::$ins instanceof self)) {
self::$ins = new self();
}
return self::$ins;
}
public static function getCart($type='cart') {
if($type=='cart'){
$safebox = Safebox::getInstance();
$user = $safebox->get('user');
if(isset($user['name']) && $user['name']!=''){
$model = new Model('cart');
$obj = $model->where('user_id='.$user['id'])->find();
if(!isset($obj['cart'])){
$sessionCart = Session::get("tiny_cart");
if ($sessionCart instanceof self && $sessionCart->getCnt() > 0){
$model->data(array('user_id'=>$user['id'],'cart' =>serialize(Session::get("tiny_cart"))))->insert();
}else{
$model->data(array('user_id'=>$user['id'],'cart' =>serialize(self::getIns())))->insert();
}
$obj = $model->where('user_id='.$user['id'])->find();
}else{
$sessionCart = Session::get("tiny_cart");
if ($sessionCart instanceof self && $sessionCart->getCnt() > 0){
$cart = unserialize($obj['cart']);
foreach ($sessionCart->items as $key => $item) {
if (isset($cart->items[$key])){
$cart->items[$key] += $item;
}else{
$cart->items[$key] = $item;
}
}
$model->data(array('cart' =>serialize($cart)))->where('user_id='.$user['id'])->update();
Session::set("tiny_cart",null);
$obj = $model->where('user_id='.$user['id'])->find();
}
}
// else{
// $model->data(array('user_id'=>$user['id'],'cart' =>serialize(Session::get("tiny_cart"))))->insert();
// $obj = $model->where('user_id='.$user['id'])->find();
// }
return unserialize($obj['cart']);
}else{
if(Session::get("tiny_cart")==null || !(Session::get("tiny_cart") instanceof self)) {
Session::set("tiny_cart",self::getIns());
}
return Session::get("tiny_cart");
}
}else{
if(Session::get("tiny_goods")==null || !(Session::get("tiny_goods") instanceof self)) {
Session::set("tiny_goods",self::getIns());
}
return Session::get("tiny_goods");
}
}
public function addItem($id,$num=1) {
if ($this->hasItem($id)) {
$this->incNum($id,$num);
return;
}
$this->items[$id] = $num;
$this->updateDBSelf();
}
public function hasItem($id) {
return isset($this->items[$id]);
}
public function delItem($id) {
unset($this->items[$id]);
$this->updateDBSelf();
}
public function modNum($id,$num=1) {
if (!$this->hasItem($id)) {
return false;
}
$this->items[$id] = $num;
$this->updateDBSelf();
}
public function incNum($id,$num=1) {
if ($this->hasItem($id)) {
$this->items[$id] += $num;
}
$this->updateDBSelf();
}
public function decNum($id,$num=1) {
if ($this->hasItem($id)) {
$this->items[$id] -= $num;
}
if ($this->items[$id] <1) {
$this->delItem($id);
}
$this->updateDBSelf();
}
public function getCnt() {
return count($this->items);
}
public function getNum(){
if ($this->getCnt() == 0) {
return 0;
}
$sum = 0;
foreach ($this->items as $item) {
$sum += $item;
}
return $sum;
}
public function all() {
$products = array();
if($this->getCnt()>0){
$model = new Model("products as pr");
$ids = array_keys($this->items);
$ids = trim(implode(",", $ids),',');
if($ids!=''){
$prom = new Prom();
$items= $model->fields("pr.*,go.img,go.name,go.prom_id,go.point")->join("left join goods as go on pr.goods_id = go.id ")->where("pr.id in($ids)")->findAll();
foreach ($items as $item) {
$num = $this->items[$item['id']];
if($num > $item['store_nums']){
$num = $item['store_nums'];
$this->modNum($item['id'],$num);
}
if($num<=0){
$this->delItem($item['id']);
}else{
$item['goods_nums']=$num;
$prom_goods = $prom->prom_goods($item);
$amount = sprintf("%01.2f",$prom_goods['real_price']*$num);
$sell_total = $item['sell_price']*$num;
$products[$item['id']] = array('id'=>$item['id'],'goods_id'=>$item['goods_id'],'name'=>$item['name'],'img'=>$item['img'],'num'=>$num,'store_nums'=>$item['store_nums'],'price'=>$item['sell_price'],'prom_id'=>$item['prom_id'],'real_price'=>$prom_goods['real_price'],'sell_price'=>$item['sell_price'],'spec'=>unserialize($item['spec']),'amount'=>$amount,'prom'=>$prom_goods['note'],'weight'=>$item['weight'],'point'=>$item['point'],'sell_total'=>$sell_total,"prom_goods"=>$prom_goods,'goods_type'=>$item['goods_type'],'virtual_extend'=>$item['virtual_extend']);
}
}
}
}
return $products;
}
function updateDBSelf()
{
$safebox = Safebox::getInstance();
$user = $safebox->get('user');
if(isset($user['name']) && $user['name']!=''){
$model = new Model('cart');
$model->data(array('cart' =>serialize($this)))->where('user_id='.$user['id'])->update();
}
}
public function clear() {
$this->items = array();
$this->updateDBSelf();
}
}