cart.model.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * 购物车模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class cartModel extends Model {
  11. /**
  12. * 购物车商品总金额
  13. */
  14. private $cart_all_price = 0;
  15. /**
  16. * 购物车商品总数
  17. */
  18. private $cart_goods_num = 0;
  19. public function __construct() {
  20. parent::__construct('cart');
  21. }
  22. /**
  23. * 取属性值魔术方法
  24. *
  25. * @param string $name
  26. */
  27. public function __get($name) {
  28. return $this->$name;
  29. }
  30. /**
  31. * 检查购物车内商品是否存在
  32. *
  33. * @param
  34. */
  35. public function checkCart($condition = array()) {
  36. return $this->where($condition)->find();
  37. }
  38. /**
  39. * 取得 单条购物车信息
  40. * @param unknown $condition
  41. * @param string $field
  42. */
  43. public function getCartInfo($condition = array(), $field = '*') {
  44. return $this->field($field)->where($condition)->find();
  45. }
  46. /**
  47. * 将商品添加到购物车中
  48. *
  49. * @param array $data 商品数据信息
  50. * @param string $save_type 保存类型,可选值 db,cookie
  51. * @param int $quantity 购物数量
  52. */
  53. public function addCart($data = array(), $save_type = '', $quantity = null) {
  54. $method = '_addCart'.ucfirst($save_type);
  55. $insert = $this->$method($data,$quantity);
  56. //更改购物车总商品数和总金额,传递数组参数只是给DB使用
  57. $this->getCartNum($save_type,array('buyer_id'=>$data['buyer_id']));
  58. return $insert;
  59. }
  60. /**
  61. * 添加数据库购物车
  62. *
  63. * @param unknown_type $goods_info
  64. * @param unknown_type $quantity
  65. * @return unknown
  66. */
  67. private function _addCartDb($goods_info = array(),$quantity) {
  68. //验证购物车商品是否已经存在
  69. $condition = array();
  70. $condition['goods_id'] = $goods_info['goods_id'];
  71. $condition['buyer_id'] = $goods_info['buyer_id'];
  72. if (isset($goods_info['bl_id'])) {
  73. $condition['bl_id'] = $goods_info['bl_id'];
  74. } else {
  75. $condition['bl_id'] = 0;
  76. }
  77. $check_cart = $this->checkCart($condition);
  78. if (!empty($check_cart)) return true;
  79. $array = array();
  80. $array['buyer_id'] = $goods_info['buyer_id'];
  81. $array['store_id'] = $goods_info['store_id'];
  82. $array['goods_id'] = $goods_info['goods_id'];
  83. $array['goods_name'] = $goods_info['goods_name'];
  84. $array['goods_price'] = $goods_info['goods_price'];
  85. $array['goods_num'] = $quantity;
  86. $array['goods_image'] = $goods_info['goods_image'];
  87. $array['store_name'] = $goods_info['store_name'];
  88. $array['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  89. return $this->insert($array);
  90. }
  91. /**
  92. * 添加到cookie购物车,最多保存5个商品
  93. *
  94. * @param unknown_type $goods_info
  95. * @param unknown_type $quantity
  96. * @return unknown
  97. */
  98. private function _addCartCookie($goods_info = array(), $quantity = null) {
  99. //去除斜杠
  100. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  101. $cart_str = base64_decode(decrypt($cart_str));
  102. $cart_array = @unserialize($cart_str);
  103. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  104. if (count($cart_array) >= 5) return false;
  105. if (in_array($goods_info['goods_id'],array_keys($cart_array))) return true;
  106. $cart_array[$goods_info['goods_id']] = array(
  107. 'store_id' => $goods_info['store_id'],
  108. 'goods_id' => $goods_info['goods_id'],
  109. 'goods_name' => $goods_info['goods_name'],
  110. 'goods_price' => $goods_info['goods_price'],
  111. 'goods_image' => $goods_info['goods_image'],
  112. 'goods_num' => $quantity
  113. );
  114. setNcCookie('cart',encrypt(base64_encode(serialize($cart_array))),24*3600);
  115. return true;
  116. }
  117. /**
  118. * 更新购物车
  119. *
  120. * @param array $param 商品信息
  121. */
  122. public function editCart($data,$condition) {
  123. $result = $this->where($condition)->update($data);
  124. if ($result) {
  125. $this->getCartNum('db',array('buyer_id'=>$condition['buyer_id']));
  126. }
  127. return $result;
  128. }
  129. /**
  130. * 购物车列表
  131. *
  132. * @param string $type 存储类型 db,cookie
  133. * @param unknown_type $condition
  134. * @param int $limit
  135. */
  136. public function listCart($type, $condition = array(), $limit = '') {
  137. if ($type == 'db') {
  138. $cart_list = $this->where($condition)->limit($limit)->select();
  139. } elseif ($type == 'cookie') {
  140. //去除斜杠
  141. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  142. $cart_str = base64_decode(decrypt($cart_str));
  143. $cart_list = @unserialize($cart_str);
  144. }
  145. $cart_list = is_array($cart_list) ? $cart_list : array();
  146. //顺便设置购物车商品数和总金额
  147. $this->cart_goods_num = count($cart_list);
  148. $cart_all_price = 0;
  149. if(is_array($cart_list)) {
  150. foreach ($cart_list as $val) {
  151. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  152. }
  153. }
  154. $this->cart_all_price = ncPriceFormat($cart_all_price);
  155. return !is_array($cart_list) ? array() : $cart_list;
  156. }
  157. /**
  158. * 删除购物车商品
  159. *
  160. * @param string $type 存储类型 db,cookie
  161. * @param unknown_type $condition
  162. */
  163. public function delCart($type, $condition = array()) {
  164. if ($type == 'db') {
  165. $result = $this->where($condition)->delete();
  166. } elseif ($type == 'cookie') {
  167. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  168. $cart_str = base64_decode(decrypt($cart_str));
  169. $cart_array = @unserialize($cart_str);
  170. if (key_exists($condition['goods_id'],(array)$cart_array)) {
  171. unset($cart_array[$condition['goods_id']]);
  172. }
  173. setNcCookie('cart',encrypt(base64_encode(serialize($cart_array))),24*3600);
  174. $result = true;
  175. }
  176. //重新计算购物车商品数和总金额
  177. if ($result) {
  178. $this->getCartNum($type,array('buyer_id'=>$condition['buyer_id']));
  179. }
  180. return $result;
  181. }
  182. /**
  183. * 清空购物车
  184. *
  185. * @param string $type 存储类型 db,cookie
  186. * @param unknown_type $condition
  187. */
  188. public function clearCart($type, $condition = array()) {
  189. if ($type == 'cookie') {
  190. setNcCookie('cart','',-3600);
  191. } else if ($type == 'db') {
  192. //数据库暂无浅清空操作
  193. }
  194. }
  195. /**
  196. * 计算购物车总商品数和总金额
  197. * @param string $type 购物车信息保存类型 db,cookie
  198. * @param array $condition 只有登录后操作购物车表时才会用到该参数
  199. */
  200. public function getCartNum($type, $condition = array()) {
  201. if ($type == 'db') {
  202. $cart_all_price = 0;
  203. $cart_goods = $this->listCart('db',$condition);
  204. $this->cart_goods_num = count($cart_goods);
  205. if(!empty($cart_goods) && is_array($cart_goods)) {
  206. foreach ($cart_goods as $val) {
  207. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  208. }
  209. }
  210. $this->cart_all_price = ncPriceFormat($cart_all_price);
  211. } elseif ($type == 'cookie') {
  212. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  213. $cart_str = base64_decode(decrypt($cart_str));
  214. $cart_array = @unserialize($cart_str);
  215. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  216. $this->cart_goods_num = count($cart_array);
  217. $cart_all_price = 0;
  218. foreach ($cart_array as $v){
  219. $cart_all_price += floatval($v['goods_price'])*intval($v['goods_num']);
  220. }
  221. $this->cart_all_price = $cart_all_price;
  222. }
  223. @setNcCookie('cart_goods_num',$this->cart_goods_num,2*3600);
  224. return $this->cart_goods_num;
  225. }
  226. /**
  227. * 登录之后,把登录前购物车内的商品加到购物车表
  228. *
  229. */
  230. public function mergecart($member_info = array(), $store_id = null){
  231. if (!$member_info['member_id']) return;
  232. // $save_type = C('cache.type') != 'file' ? 'cache' : 'cookie';
  233. $save_type = 'cookie';
  234. $cart_new_list = $this->listCart($save_type);
  235. if (empty($cart_new_list)) return;
  236. //取出当前DB购物车已有信息
  237. $cart_cur_list = $this->listCart('db',array('buyer_id'=>$member_info['member_id']));
  238. //数据库购物车已经有的商品,不再添加
  239. if (!empty($cart_cur_list) && is_array($cart_cur_list) && is_array($cart_new_list)) {
  240. foreach ($cart_new_list as $k=>$v){
  241. if (!is_numeric($k) || in_array($k,array_keys($cart_cur_list))){
  242. unset($cart_new_list[$k]);
  243. }
  244. }
  245. }
  246. //查询在购物车中,不是店铺自己的商品,未禁售,上架,有库存的商品,并加入DB购物车
  247. $model_goods = Model('goods');
  248. $condition = array();
  249. if (!empty($_SESSION['store_id'])) {
  250. $condition['store_id'] = array('neq',$store_id);
  251. }
  252. $condition['goods_id'] = array('in',array_keys($cart_new_list));
  253. $goods_list = $model_goods->getGoodsOnlineList($condition);
  254. if (!empty($goods_list)){
  255. foreach ($goods_list as $goods_info){
  256. $goods_info['buyer_id'] = $member_info['member_id'];
  257. $this->addCart($goods_info,'db',$cart_new_list[$goods_info['goods_id']]['goods_num']);
  258. }
  259. }
  260. //最后清空登录前购物车内容
  261. $this->clearCart($save_type);
  262. }
  263. }