-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCart.php
More file actions
executable file
·87 lines (79 loc) · 2.41 KB
/
Copy pathCart.php
File metadata and controls
executable file
·87 lines (79 loc) · 2.41 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
<?php
// +----------------------------------------------------------------------
// | Buddy Framework
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://buddy.woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------
/**
* 购物车类
* @author xinqiyang
*
*/
class Cart {
var $cartname = '';
var $savetype = '';
var $data = array();
var $cookietime = 0;
var $cookiepath = '/';
var $cookiedomain = '';
function __construct($cartname = 'WSMJCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '') {
if ($savetype == 'session') {
if (!$session_id && $_COOKIE[$cartname.'_session_id']) {
session_id($_COOKIE[$cartname.'_session_id']);
} elseif($session_id){
session_id($session_id);
if (!$session_id && !$_COOKIE[$cartname.'_session_id']){
setcookie($cartname.'_session_id', session_id(), $cookietime + time(), $cookiepath, $cookiedomain);
}
}
}
$this->cartname = $cartname;
$this->savetype = $savetype;
$this->cookietime = $cookietime;
$this->cookiepath = $cookiepath;
$this->cookiedomain = $cookiedomain;
$this->readdata();
}
// 读取数据
function readdata() {
if ($this->savetype == 'session') {
if ($_SESSION[$this->cartname] && is_array($_SESSION[$this->cartname])){
$this->data = $_SESSION[$this->cartname];
}else{
$this->data = array();
}
} elseif ($this->savetype == 'cookie') {
if ($_COOKIE[$this->cartname]){
$this->data = unserialize($_COOKIE[$this->cartname]);
}else{
$this->data = array();
}
}
}
// 保存购物车数据
function save() {
if ($this->savetype == 'session') {
$_SESSION[$this->cartname] = $this->data;
} elseif ($this->savetype == 'cookie') {
if ($this->data){
setcookie($this->cartname, serialize($this->data), $this->cookietime + time(), $this->cookiepath, $this->cookiedomain);
}
}
}
// 返回商品某字段累加
function sum($field) {
$sum = 0;
if ($this->data){
foreach ($this->data AS $v){
if ($v[$field]){
$sum += $v[$field] + 0;
}
}
}
return $sum;
}
}