Cart.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. // $Id:$
  3. /**
  4. * 小梦科技资源nanodreamtech.com
  5. *
  6. * ==========================================================================
  7. * @link https://www.nanodreamtech.com/
  8. * @copyright Copyright (c) 2017 liofis.com.
  9. * @license https://www.nanodreamtech.com/license.html License
  10. * ==========================================================================
  11. *
  12. * @author fish
  13. *
  14. */
  15. namespace Lib;
  16. class Cart {
  17. private $data = array();
  18. public function get_cart_goods($goods_id, $sku_str) {
  19. $key = (int)$goods_id . ':';
  20. //$qty = 1
  21. if ($sku_str) {
  22. $key.= base64_encode($sku_str) . ':';
  23. } else {
  24. $key.= ':';//xx
  25. }
  26. $key = 'cart.' . $key;
  27. $s = session($key);
  28. return $s['quantity'];
  29. }
  30. public function get_wecart_goods($goods_id, $sku_str,$token)
  31. {
  32. $key = (int)$goods_id . ':';
  33. if ($sku_str) {
  34. $key.= base64_encode($sku_str) . ':';
  35. } else {
  36. $key.= ':';//xx
  37. }
  38. $key = 'cart.' . $key;
  39. $s_arr = M('car')->field('format_data')->where(array(
  40. 'token' => $token,
  41. 'carkey' => $key
  42. ))->find();
  43. $tmp_format_data = unserialize($s_arr['format_data']);
  44. return $tmp_format_data['quantity'];
  45. }
  46. //加入购物车
  47. //public function add($goods_id, $qty = 1, $option) {
  48. public function add($goods_id, $format_data = array() , $option) {
  49. $key = (int)$goods_id . ':';
  50. //$qty = 1
  51. $qty = $format_data['quantity'];
  52. if ($option) {
  53. $key.= base64_encode($option) . ':';
  54. } else {
  55. $key.= ':';
  56. }
  57. $cart = session('cart');
  58. $hashids = new \Lib\Hashids(C('PWD_KEY') , C('URL_ID'));
  59. foreach ($cart as $kk => $val) {
  60. $kk = 'cart.' . $kk;
  61. $val['singledel'] = 0;
  62. session($kk, $val);
  63. }
  64. if ((int)$qty && ((int)$qty > 0)) {
  65. $key = 'cart.' . $key;
  66. $s = session($key);
  67. //pin_type: pin, dan
  68. //
  69. //合并购物车商品
  70. if (!empty($s)) {
  71. if($format_data['singledel'] == 1)
  72. {
  73. $format_data['old_quantity'] = $s['quantity'];
  74. }else{
  75. $format_data['quantity']+= $s['quantity'];
  76. }
  77. }
  78. session($key, $format_data);
  79. }
  80. $this->data = array();
  81. }
  82. public function addwecar($token, $goods_id, $format_data = array() , $option) {
  83. $key = (int)$goods_id . ':';
  84. $qty = $format_data['quantity'];
  85. if ($option) {
  86. $key.= base64_encode($option) . ':';
  87. } else {
  88. $key.= ':';
  89. }
  90. if( $format_data['is_just_addcar'] == 0 )
  91. {
  92. $all_cart = M('car')->where("token = '{$token}' and carkey like 'cart.%' ")->select();
  93. if(!empty($all_cart))
  94. {
  95. foreach($all_cart as $val)
  96. {
  97. $tmp_format_data = unserialize($val['format_data']);
  98. $tmp_format_data['singledel'] = 0;
  99. M('car')->where( array('id' => $val['id']) )->save( array('format_data' => serialize($tmp_format_data) ) );
  100. }
  101. }
  102. }
  103. $s_arr = M('car')->field('format_data')->where(array(
  104. 'token' => $token,
  105. 'carkey' => 'cart.'.$key
  106. ))->find();
  107. //`carkey` = 'cart.27::'
  108. //$format_data_array['is_just_addcar'] = 1;
  109. //var_dump( M('car')->getLastSql() );die();
  110. if ((int)$qty && ((int)$qty > 0)) {
  111. $key = 'cart.' . $key;
  112. $s = array();
  113. if (!empty($s_arr)) {
  114. $s = unserialize($s_arr['format_data']);
  115. }
  116. if (!empty($s)) {
  117. if( $format_data['is_just_addcar'] == 1 )
  118. {
  119. $format_data['quantity']+= $s['quantity'];
  120. }
  121. }
  122. }
  123. if (!empty($s_arr)) {
  124. M('car')->where(array(
  125. 'token' => $token,
  126. 'carkey' => $key
  127. ))->save(array(
  128. 'modifytime' => time(),
  129. 'format_data' => serialize($format_data)
  130. ));
  131. } else {
  132. M('car')->add(array(
  133. 'token' => $token,
  134. 'carkey' => $key,
  135. 'modifytime' => time(),
  136. 'format_data' => serialize($format_data)
  137. ));
  138. }
  139. $this->data = array();
  140. }
  141. public function add_activitycar($token, $goods_id, $format_data = array() , $option) {
  142. $this->removeActivityAllcar($token);
  143. $key = (int)$goods_id . ':';
  144. $qty = $format_data['quantity'];
  145. if ($option) {
  146. $key.= base64_encode($option) . ':';
  147. } else {
  148. $key.= ':';
  149. }
  150. $key.= "buy_type:" . $option['buy_type'];
  151. if ((int)$qty && ((int)$qty > 0)) {
  152. $key = 'cart_activity.' . $key;
  153. $s_arr = M('car')->where(array(
  154. 'token' => $token,
  155. 'carkey' => $key
  156. ))->find();
  157. $s = array();
  158. if (!empty($s_arr)) {
  159. $s = unserialize($s_arr['format_data']);
  160. }
  161. if (!empty($s)) {
  162. $format_data['quantity']+= $s['quantity'];
  163. }
  164. if (!empty($s_arr)) {
  165. M('car')->where(array(
  166. 'token' => $token,
  167. 'carkey' => $key
  168. ))->save(array(
  169. 'format_data' => serialize($format_data)
  170. ));
  171. } else {
  172. M('car')->add(array(
  173. 'token' => $token,
  174. 'carkey' => $key,
  175. 'format_data' => serialize($format_data)
  176. ));
  177. }
  178. }
  179. $this->data = array();
  180. }
  181. public function add_activity($goods_id, $format_data = array() , $option) {
  182. $this->removeActivityAll(); //活动商品只能单独购买
  183. $key = (int)$goods_id . ':';
  184. //$qty = 1
  185. $qty = $format_data['quantity'];
  186. if ($option) {
  187. $key.= base64_encode($option) . ':';
  188. } else {
  189. $key.= ':';
  190. }
  191. $key.= "buy_type:" . $option['buy_type'];
  192. if ((int)$qty && ((int)$qty > 0)) {
  193. $key = 'cart_activity.' . $key;
  194. $s = session($key);
  195. //pin_type: pin, dan
  196. //
  197. //合并购物车商品
  198. if (!empty($s)) {
  199. $format_data['quantity']+= $s['quantity'];
  200. }
  201. session($key, $format_data);
  202. }
  203. $this->data = array();
  204. }
  205. public function removeActivityAll() {
  206. $s = session('cart_activity');
  207. if (isset($s)) {
  208. foreach ($s as $k => $v) {
  209. $key = 'cart_activity.' . $k; //重新给$key赋值
  210. session($key, null);
  211. }
  212. }
  213. }
  214. public function removeActivityAllcar($token) {
  215. M('car')->where(" token ='{$token}' and carkey like 'cart_activity.%' ")->delete();
  216. }
  217. //删除所有购物车商品
  218. public function removeAll() {
  219. $s = session('cart');
  220. if (isset($s)) {
  221. foreach ($s as $k => $v) {
  222. $key = 'cart.' . $k; //重新给$key赋值
  223. session($key, null);
  224. }
  225. }
  226. }
  227. //清空商品
  228. public function removedancar($token) {
  229. //$key = 'cart.' . $key; //重新给$key赋值
  230. //M('car')->where( array('token' => $token, 'carkey' => $key) )->delete();
  231. M('car')->where(" token ='{$token}' and carkey like 'cart.%' ")->delete();
  232. //session($key, null);
  233. }
  234. //删除商品
  235. public function removecar($key,$token) {
  236. $key = 'cart.' . $key; //重新给$key赋值
  237. M('car')->where( array('token' => $token, 'carkey' => $key) )->delete();
  238. //session($key, null);
  239. }
  240. //删除商品
  241. public function remove($key) {
  242. $key = 'cart.' . $key; //重新给$key赋值
  243. session($key, null);
  244. }
  245. //更新购物车
  246. public function update($key, $qty) {
  247. $ckey = 'cart.' . $key;
  248. if ((int)$qty && ((int)$qty > 0)) {
  249. $arr = session($ckey);
  250. $arr['quantity'] = (int)$qty;
  251. session($ckey, $arr);
  252. } else {
  253. $this->remove($key);
  254. }
  255. }
  256. //获取购物车全部商品
  257. public function get_all_goodswecar($buy_type = 'dan', $token,$is_pay_need = 1) {
  258. if (!($this->data)) {
  259. if ($buy_type == 'dan') {
  260. $cart = M('car')->where("token = '{$token}' and carkey like 'cart.%' ")->order('modifytime desc')->select();
  261. } else {
  262. $cart = M('car')->where("token = '{$token}' and carkey like 'cart_activity.%' ")->select();
  263. }
  264. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  265. $member_id = $weprogram_token['member_id'];
  266. $goods_model = D('Home/Goods');
  267. foreach ($cart as $key => $val_uns) {
  268. $val = unserialize( $val_uns['format_data'] );
  269. if($buy_type =='dan' && $is_pay_need == 1)
  270. {
  271. if(isset($val['singledel']) && $val['singledel'] == 0)
  272. {
  273. continue;
  274. }
  275. }else if($buy_type == 'dan' && $is_pay_need == 0)
  276. {
  277. //判断是否支持自提,如果支持自提,那么就不要剔除购物车列表
  278. //$val['goods_id'] pick_just
  279. $pick_just = M('goods')->field('pick_just')->where( array('goods_id' => $val['goods_id']) )->find();
  280. if($pick_just['pick_just'] > 0)
  281. {
  282. continue;
  283. }else {
  284. }
  285. }
  286. //$pin_type = $val['pin_type'];
  287. $quantity = $val['quantity'];
  288. //$quantity
  289. $goods = explode(':', $key);
  290. $goods_id = $val['goods_id'];
  291. $stock = true;
  292. // Options sku_str
  293. $options = $val['sku_str'];
  294. $goods_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods p LEFT JOIN " . C('DB_PREFIX') . "goods_description pd ON (p.goods_id = pd.goods_id) WHERE p.goods_id = " . (int)$goods_id . " AND p.status = 1");
  295. if ($goods_query) {
  296. $option_price = null;
  297. $option_weight = 0;
  298. $option_data = array();
  299. $max_quantity = $goods_query[0]['quantity'];
  300. if (!empty($options)) {
  301. //$options "36_39"//option_value_id $goods_id
  302. $goods_option_mult_value = M('goods_option_mult_value')->where(array(
  303. 'rela_goodsoption_valueid' => $options,
  304. 'goods_id' => $goods_id
  305. ))->find();
  306. //["pin_price"]=> string(4) "0.02" ["dan_price"]=> string(4) "0.02"
  307. $options_arr = array();
  308. $option_value_id_arr = explode('_', $options);
  309. foreach ($option_value_id_arr as $id_val) {
  310. $goods_option_value = M('goods_option_value')->where(array(
  311. 'option_value_id' => $id_val,
  312. 'goods_id' => $goods_id
  313. ))->find();
  314. $options_arr[$goods_option_value['goods_option_id']] = $goods_option_value['goods_option_value_id'];
  315. }
  316. }
  317. foreach ($options_arr as $goods_option_id => $option_value) {
  318. $option_query = M()->query("SELECT po.goods_option_id, po.option_id, o.name FROM " . C('DB_PREFIX') . "goods_option po LEFT JOIN `" . C('DB_PREFIX') . "option` o ON (po.option_id = o.option_id)
  319. WHERE po.goods_option_id = '" . (int)$goods_option_id . "' AND po.goods_id = " . (int)$goods_id);
  320. if ($option_query) {
  321. $option_value_query = M()->query("SELECT pov.option_value_id,
  322. ov.value_name, pov.quantity, pov.subtract, pov.price, pov.price_prefix,pov.weight, pov.weight_prefix FROM " . C('DB_PREFIX') . "goods_option_value pov LEFT JOIN " . C('DB_PREFIX') . "option_value ov ON (pov.option_value_id = ov.option_value_id) WHERE pov.goods_option_value_id = '" . (int)$option_value . "' AND pov.goods_option_id = " . (int)$goods_option_id);
  323. if ($option_value_query) {
  324. $max_quantity = $goods_option_mult_value['quantity'];
  325. //根据商品类型获取不同价格 begin
  326. if ($buy_type == 'pin') {
  327. $option_price = $goods_option_mult_value['pin_price'];
  328. } else {
  329. $option_price = $goods_option_mult_value['dan_price'];
  330. }
  331. //根据商品类型获取不同价格 begin
  332. $option_weight = $goods_option_mult_value['weight'];
  333. if ($option_value_query[0]['subtract'] && (!$option_value_query[0]['quantity'] || ($option_value_query[0]['quantity'] < $quantity))) {
  334. $stock = false;
  335. }
  336. $option_data[] = array(
  337. 'goods_option_id' => $goods_option_id,
  338. 'goods_option_value_id' => $option_value,
  339. 'option_id' => $option_query[0]['option_id'],
  340. 'option_value_id' => $option_value_query[0]['option_value_id'],
  341. 'name' => $option_query[0]['name'],
  342. 'value' => $option_value_query[0]['value_name'],
  343. // 'type' => $option_query[0]['type'],
  344. 'quantity' => $quantity,
  345. 'subtract' => $option_value_query[0]['subtract'],
  346. 'price' => $option_price,
  347. //'price_prefix' => $option_value_query[0]['price_prefix'],
  348. 'weight' => $option_weight,
  349. //'weight_prefix' => $option_value_query[0]['weight_prefix']
  350. );
  351. }
  352. }
  353. }
  354. $header_disc = 100;
  355. $shop_price = $goods_query[0]['price'];
  356. $goods_query[0]['price'] = $goods_query[0]['danprice'];
  357. $thumb_image = C('SITE_URL').resize($goods_query[0]['image'], C('goods_cart_thumb_width') , C('goods_cart_thumb_height'));
  358. if (!empty($goods_query[0]['image']['fan_image'])) {
  359. $thumb_image = C('SITE_URL').resize($goods_query[0]['fan_image'], C('goods_cart_thumb_width') , C('goods_cart_thumb_height'));
  360. }
  361. $store_info = M('seller')->field('s_true_name,s_logo')->where('s_id='.$goods_query[0]['store_id'])->find();
  362. $s_logo = C('SITE_URL').'/Uploads/image/'.$store_info['s_logo'];
  363. //$goods_query['price']
  364. if ( !is_null($option_price)) {
  365. $goods_query[0]['price'] = $option_price;
  366. } else {
  367. //根据商品类型获取不同价格 begin
  368. if ($buy_type == 'pin') {//判断类型是否是积分商品
  369. if($goods_query[0]['type'] == 'integral')
  370. {
  371. $intgral_goods_info = M('intgral_goods')->field('score')->where( array('goods_id' => $goods_id) )->find();
  372. $goods_query[0]['price'] = $intgral_goods_info['score'];
  373. }else{
  374. $pin_goods = M('pin_goods')->field('pin_price')->where(array(
  375. 'goods_id' => $goods_id
  376. ))->find();
  377. $goods_query[0]['price'] = $pin_goods['pin_price'];
  378. }
  379. }
  380. //根据商品类型获取不同价格 begin
  381. }
  382. if (!empty($option_weight)) {
  383. $goods_query[0]['weight'] = $option_weight;
  384. }
  385. //拼团才会有pin_id
  386. $pin_id = 0;
  387. if ($buy_type == 'pin' && isset($val['pin_id'])) {
  388. $pin_id = $val['pin_id'];
  389. }
  390. $price = $goods_query[0]['price'];
  391. //判断是否有团长折扣
  392. if( $buy_type == 'pin' && $pin_id == 0 && $goods_query[0]['head_disc'] != 100)
  393. {
  394. //&& $goods_query[0]['head_disc'] != 100
  395. $price = round(( $price * intval($goods_query[0]['head_disc']) )/100,2);
  396. //$goods_query[0]['price'] = $price;
  397. $header_disc = intval($goods_query[0]['head_disc']);
  398. }
  399. //判断是否会员折扣
  400. $level_info = $goods_model->get_member_level_info($member_id, $goods_id);
  401. //$result['member_discount'] = 100;
  402. if($level_info['member_discount'] > 0 && $level_info['member_discount'] != 100)
  403. {
  404. $price = round(( $price * $level_info['member_discount'])/100,2);
  405. }
  406. //拼团 end
  407. $this->data[$key] = array(
  408. 'key' => $val_uns['carkey'],
  409. 'goods_id' => $goods_query[0]['goods_id'] ,
  410. 'name' => $goods_query[0]['name'],
  411. 'seller_name' => $store_info['s_true_name'],
  412. 'seller_logo' => $s_logo,
  413. 'weight' => $option_weight,
  414. 'singledel' => $val['singledel'],
  415. //$val['singledel']
  416. 'header_disc' => $header_disc,
  417. 'member_disc' => $level_info['member_discount'],
  418. 'level_name' => $level_info['level_name'],
  419. 'pin_id' => $pin_id,
  420. 'shipping' => $goods_query[0]['shipping'],
  421. 'goods_freight' => $goods_query[0]['goods_freight'],
  422. 'transport_id' => $goods_query[0]['transport_id'],
  423. 'image' => $thumb_image,
  424. 'quantity' => $quantity,
  425. 'max_quantity' => $max_quantity,
  426. 'shop_price' => $shop_price,
  427. 'price' => $goods_query[0]['price'],
  428. 'total' => ($price) * $quantity,
  429. //'model' => $goods_query[0]['model'],
  430. 'option' => $option_data,
  431. 'sku_str' => $val['sku_str'],
  432. );
  433. } else {
  434. $this->removecar($key,$token);
  435. }
  436. }
  437. }
  438. return $this->data;
  439. }
  440. //获取购物车全部商品
  441. public function get_all_goods($buy_type = 'dan',$singledel =0) {
  442. if (!($this->data)) {
  443. if ($buy_type == 'dan') {
  444. $cart = session('cart');
  445. } else {
  446. $cart = session('cart_activity');
  447. }
  448. $hashids = new \Lib\Hashids(C('PWD_KEY') , C('URL_ID'));
  449. foreach ($cart as $key => $val) {
  450. if($buy_type =='dan' && $singledel == 1)
  451. {
  452. if($val['singledel'] == 0)
  453. {
  454. continue;
  455. }
  456. }
  457. //$pin_type = $val['pin_type'];
  458. $quantity = $val['quantity'];
  459. //$quantity
  460. $goods = explode(':', $key);
  461. $goods_id = $goods[0];
  462. $stock = true;
  463. // Options
  464. if (!empty($goods[1])) {
  465. $options = base64_decode($goods[1]);
  466. } else {
  467. $options = array();
  468. }
  469. $goods_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods p LEFT JOIN " . C('DB_PREFIX') . "goods_description pd ON (p.goods_id = pd.goods_id) WHERE p.goods_id = " . (int)$goods_id . " AND p.status = 1");
  470. if ($goods_query) {
  471. $option_price = -1;
  472. $option_weight = 0;
  473. $option_data = array();
  474. if (!empty($options)) {
  475. //$options "36_39"//option_value_id $goods_id
  476. $goods_option_mult_value = M('goods_option_mult_value')->where(array(
  477. 'rela_goodsoption_valueid' => $options,
  478. 'goods_id' => $goods_id
  479. ))->find();
  480. $options_arr = array();
  481. $option_value_id_arr = explode('_', $options);
  482. foreach ($option_value_id_arr as $id_val) {
  483. $goods_option_value = M('goods_option_value')->where(array(
  484. 'option_value_id' => $id_val,
  485. 'goods_id' => $goods_id
  486. ))->find();
  487. $options_arr[$goods_option_value['goods_option_id']] = $goods_option_value['goods_option_value_id'];
  488. }
  489. }
  490. foreach ($options_arr as $goods_option_id => $option_value) {
  491. $option_query = M()->query("SELECT po.goods_option_id, po.option_id, o.name FROM " . C('DB_PREFIX') . "goods_option po LEFT JOIN `" . C('DB_PREFIX') . "option` o ON (po.option_id = o.option_id)
  492. WHERE po.goods_option_id = '" . (int)$goods_option_id . "' AND po.goods_id = " . (int)$goods_id);
  493. if ($option_query) {
  494. $option_value_query = M()->query("SELECT pov.option_value_id,
  495. ov.value_name, pov.quantity, pov.subtract, pov.price, pov.price_prefix,pov.weight, pov.weight_prefix FROM " . C('DB_PREFIX') . "goods_option_value pov LEFT JOIN " . C('DB_PREFIX') . "option_value ov ON (pov.option_value_id = ov.option_value_id) WHERE pov.goods_option_value_id = '" . (int)$option_value . "' AND pov.goods_option_id = " . (int)$goods_option_id);
  496. if ($option_value_query) {
  497. //根据商品类型获取不同价格 begin
  498. if ($buy_type == 'pin') {
  499. $option_price = $goods_option_mult_value['pin_price'];
  500. } else {
  501. $option_price = $goods_option_mult_value['dan_price'];
  502. }
  503. //根据商品类型获取不同价格 begin
  504. $option_weight = $goods_option_mult_value['weight'];
  505. if ($option_value_query[0]['subtract'] && (!$option_value_query[0]['quantity'] || ($option_value_query[0]['quantity'] < $quantity))) {
  506. $stock = false;
  507. }
  508. $option_data[] = array(
  509. 'goods_option_id' => $goods_option_id,
  510. 'goods_option_value_id' => $option_value,
  511. 'option_id' => $option_query[0]['option_id'],
  512. 'option_value_id' => $option_value_query[0]['option_value_id'],
  513. 'name' => $option_query[0]['name'],
  514. 'value' => $option_value_query[0]['value_name'],
  515. 'type' => $option_query[0]['type'],
  516. 'quantity' => $quantity,
  517. 'subtract' => $option_value_query[0]['subtract'],
  518. 'price' => $option_price,
  519. 'price_prefix' => $option_value_query[0]['price_prefix'],
  520. 'weight' => $option_weight,
  521. 'weight_prefix' => $option_value_query[0]['weight_prefix']
  522. );
  523. }
  524. }
  525. }
  526. $header_disc = 100;
  527. $thumb_image = resize($goods_query[0]['image'], C('goods_cart_thumb_width') , C('goods_cart_thumb_height'));
  528. if (!empty($goods_query[0]['image']['fan_image'])) {
  529. // $thumb_image = resize($goods_query[0]['fan_image'], C('goods_cart_thumb_width') , C('goods_cart_thumb_height'));
  530. }
  531. $goods_query[0]['price'] = $goods_query[0]['danprice'];
  532. if (floatval($option_price) >= 0) {
  533. $goods_query[0]['price'] = $option_price;
  534. } else {
  535. //根据商品类型获取不同价格 begin
  536. if ($buy_type == 'pin') {
  537. if($goods_query[0]['type'] == 'integral')
  538. {
  539. $intgral_goods_info = M('intgral_goods')->field('score')->where( array('goods_id' => $goods_id) )->find();
  540. $goods_query[0]['price'] = $intgral_goods_info['score'];
  541. }else{
  542. $pin_goods = M('pin_goods')->field('pin_price')->where(array(
  543. 'goods_id' => $goods_id
  544. ))->find();
  545. $goods_query[0]['price'] = $pin_goods['pin_price'];
  546. }
  547. }
  548. //根据商品类型获取不同价格 begin
  549. }
  550. if (!empty($option_weight)) {
  551. $goods_query[0]['weight'] = $option_weight;
  552. }
  553. //拼团才会有pin_id
  554. $pin_id = 0;
  555. if ($buy_type == 'pin' && isset($val['pin_id'])) {
  556. $pin_id = $val['pin_id'];
  557. }
  558. //拼团 end
  559. $price = $goods_query[0]['price'];
  560. $origin_price = $price;
  561. if( $buy_type == 'pin' && $pin_id == 0 && $goods_query[0]['head_disc'] != 100)
  562. {
  563. //&& $goods_query[0]['head_disc'] != 100
  564. $price = round(( $price * intval($goods_query[0]['head_disc']) )/100,2);
  565. $goods_query[0]['price'] = $price;
  566. $header_disc = intval($goods_query[0]['head_disc']);
  567. }
  568. $this->data[$key] = array(
  569. 'key' => $key,
  570. 'goods_id' => $goods_query[0]['goods_id'] ,
  571. 'name' => $goods_query[0]['name'],
  572. 'weight' => $option_weight,
  573. 'header_disc' => $header_disc,
  574. 'pin_id' => $pin_id,
  575. 'shipping' => $goods_query[0]['shipping'],
  576. 'goods_freight' => $goods_query[0]['goods_freight'],
  577. 'transport_id' => $goods_query[0]['transport_id'],
  578. 'image' => $thumb_image,
  579. 'quantity' => $quantity,
  580. 'price' => $origin_price,
  581. 'total' => ($goods_query[0]['price']) * $quantity,
  582. 'model' => $goods_query[0]['model'],
  583. 'option' => $option_data,
  584. );
  585. } else {
  586. $this->remove($key);
  587. }
  588. }
  589. }
  590. return $this->data;
  591. }
  592. public function count_goodscar($token) {
  593. $quantity = 0;
  594. $cart = M('car')->where( "token = '{$token}' and carkey like 'car.%' " )->select();
  595. foreach ($cart as $key => $val) {
  596. $format_data = unserialize($val['format_data']);
  597. $quantity += $format_data['quantity'];
  598. }
  599. return $quantity;
  600. }
  601. public function count_activitycar($token) {
  602. $quantity = 0;
  603. $cart = M('car')->where( "token = '{$token}' and carkey like 'cart_activity.%' " )->select();
  604. foreach ($cart as $key => $val) {
  605. $format_data = unserialize($val['format_data']);
  606. $quantity += $format_data['quantity'];
  607. }
  608. return $quantity;
  609. }
  610. //计算商品总数
  611. public function count_goods() {
  612. $goods_total = 0;
  613. $quantity = 0;
  614. $cart = session('cart');
  615. $hashids = new \Lib\Hashids(C('PWD_KEY') , C('URL_ID'));
  616. foreach ($cart as $key => $val) {
  617. //$pin_type = $val['pin_type'];
  618. $quantity+= $val['quantity'];
  619. }
  620. return $quantity;
  621. }
  622. //得到商品数量
  623. public function get_goods_quantity($goods_id) {
  624. return M('goods')->where(array(
  625. 'goods_id' => $goods_id
  626. ))->getField('quantity');
  627. }
  628. //取得商品重量
  629. public function getWeight() {
  630. $weight = 0;
  631. $w = new LibWeight();
  632. foreach ($this->get_all_goods() as $product) {
  633. if ($product['shipping']) {
  634. $weight+= $w->convert($product['weight'], $product['weight_class_id'], C('WEIGHT_ID'));
  635. }
  636. }
  637. return $weight;
  638. }
  639. //是否需要派送,下载类商品不需要配送
  640. public function has_shipping() {
  641. $shipping = false;
  642. foreach ($this->get_all_goods() as $product) {
  643. if ($product['shipping'] == 2) {
  644. $shipping = true;
  645. break;
  646. }
  647. }
  648. return $shipping;
  649. }
  650. //购物车是否为空
  651. public function has_goodswecar($buy_type = 'dan', $token) {
  652. if ($buy_type == 'dan') {
  653. $s = M('car')->where("token = '{$token}' and carkey like 'cart.%' ")->select();
  654. } else if ($buy_type == 'pin') {
  655. $s = M('car')->where("token = '{$token}' and carkey like 'cart_activity.%' ")->select();
  656. }
  657. return count($s);
  658. }
  659. //购物车是否为空
  660. public function has_goods($buy_type = 'dan') {
  661. if ($buy_type == 'dan') {
  662. $s = session('cart');
  663. } else if ($buy_type == 'pin') {
  664. $s = session('cart_activity');
  665. }
  666. return count($s);
  667. }
  668. }