Quote.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\cart;
  10. use fecshop\services\Service;
  11. use Yii;
  12. /**
  13. * Cart service.
  14. *
  15. * @method createCart() create cart
  16. * @see \fecshop\services\cart\Quote::actionCreateCart()
  17. * @method setCartId($cart_id) set cart id
  18. * @see \fecshop\services\cart\Quote::actionSetCartId()
  19. *
  20. * @author Terry Zhao <2358269014@qq.com>
  21. * @since 1.0
  22. */
  23. class Quote extends Service
  24. {
  25. /**
  26. * 购物车的个数计算,是否仅仅计算active状态的产品个数总和,如果设置false,则将购物车中所有的产品个数累加。
  27. */
  28. const SESSION_CART_ID = 'current_session_cart_id';
  29. protected $_cart_id;
  30. protected $_cart;
  31. protected $_shipping_cost;
  32. protected $_cartModelName = '\fecshop\models\mysqldb\Cart';
  33. protected $_cartModel;
  34. /**
  35. * 存储购物车的信息。
  36. */
  37. protected $cartInfo;
  38. public function init()
  39. {
  40. parent::init();
  41. list($this->_cartModelName, $this->_cartModel) = Yii::mapGet($this->_cartModelName);
  42. }
  43. /**
  44. * @return int|null get cart id, or null if user does not do any cart operation
  45. * @see \fecshop\services\cart\Quote::createCart()
  46. * @see \fecshop\services\cart\Quote::mergeCartAfterUserLogin()
  47. */
  48. public function getCartId()
  49. {
  50. if (!$this->_cart_id) {
  51. if (Yii::$service->store->isAppserver()) {
  52. // appserver 必须登陆用户才能加入购物车
  53. if (Yii::$app->user->isGuest) {
  54. Yii::$service->helper->errors->add('appserver get cart id must login account');
  55. return false;
  56. }
  57. $customerId = Yii::$app->user->getId();
  58. $cart = $this->getCartByCustomerId($customerId);
  59. if ($cart && $cart['cart_id']) {
  60. $this->setCartId($cart['cart_id']);
  61. }
  62. } else {
  63. $cart_id = Yii::$service->session->get(self::SESSION_CART_ID);
  64. if (! $cart_id) {
  65. if (! Yii::$app->user->isGuest) {
  66. $customerId = Yii::$app->user->getId();
  67. $cart = $this->getCartByCustomerId($customerId);
  68. if ($cart && $cart['cart_id']) {
  69. $this->setCartId($cart['cart_id']);
  70. }
  71. }
  72. } else {
  73. $this->_cart_id = $cart_id;
  74. }
  75. }
  76. }
  77. return $this->_cart_id;
  78. }
  79. /**
  80. * @param $address|array 地址信息数组,详细参看下面函数显示的字段。
  81. * @param $shipping_method | String 货运方式
  82. * @param $payment_method | String 支付方式
  83. * @param bool
  84. * 更新游客购物车信息,用户下次下单 或者 重新下单,可以不需要重新填写货运地址信息。
  85. */
  86. public function updateGuestCart($address, $shipping_method, $payment_method)
  87. {
  88. $cart = $this->getCurrentCart();
  89. if ($cart) {
  90. $cart->customer_firstname = $address['first_name'];
  91. $cart->customer_lastname = $address['last_name'];
  92. $cart->customer_email = $address['email'];
  93. $cart->customer_telephone = $address['telephone'];
  94. $cart->customer_address_street1 = $address['street1'];
  95. $cart->customer_address_street2 = $address['street2'];
  96. $cart->customer_address_country = $address['country'];
  97. $cart->customer_address_city = $address['city'];
  98. $cart->customer_address_state = $address['state'];
  99. $cart->customer_address_zip = $address['zip'];
  100. $cart->shipping_method = $shipping_method;
  101. $cart->payment_method = $payment_method;
  102. return $cart->save();
  103. }
  104. }
  105. /**
  106. * @param $address_id | int 用户customer address id
  107. * @param $shipping_method 货运方式
  108. * @param $payment_method 支付方式
  109. * @param bool
  110. * 登录用户的cart信息,进行更新,更新cart的$address_id,$shipping_method,$payment_method。
  111. * 用途:对于登录用户,create new address(在下单页面),新创建的address会被保存,
  112. * 然后需要把address_id更新到cart中。
  113. * 对于 shipping_method 和 payment_method,保存到cart中,下次进入下单页面,会被记录
  114. * 下次登录用户进行下单,进入下单页面,会自动填写。
  115. */
  116. public function updateLoginCart($address_id, $shipping_method, $payment_method)
  117. {
  118. $cart = $this->getCurrentCart();
  119. if ($cart && $address_id) {
  120. $cart->customer_address_id = $address_id;
  121. $cart->shipping_method = $shipping_method;
  122. $cart->payment_method = $payment_method;
  123. return $cart->save();
  124. }
  125. }
  126. /**
  127. * @return object
  128. * 得到当前的cart,如果当前的cart不存在,则返回为空
  129. * 注意:这是getCurrentCart() 和 getCart()两个函数的区别,getCart()函数在没有cart_id的时候会创建cart。
  130. */
  131. public function getCurrentCart()
  132. {
  133. if (!$this->_cart) {
  134. $cart_id = $this->getCartId();
  135. if ($cart_id) {
  136. $one = $this->_cartModel->findOne(['cart_id' => $cart_id]);
  137. if ($one['cart_id']) {
  138. $this->_cart = $one;
  139. }
  140. }
  141. }
  142. return $this->_cart;
  143. }
  144. /**
  145. * 如果当前的Cart不存在,则创建Cart
  146. * 如果当前的cart存在,则查询,如果查询得到cart,则返回,如果查询不到,则重新创建
  147. * 设置$this->_cart 为 上面新建或者查询得到的cart对象。
  148. */
  149. public function getCart()
  150. {
  151. if (!$this->_cart) {
  152. $cart_id = $this->getCartId();
  153. if (!$cart_id) {
  154. $this->createCart();
  155. } else {
  156. $one = $this->_cartModel->findOne(['cart_id' => $cart_id]);
  157. if ($one['cart_id']) {
  158. $this->_cart = $one;
  159. } else {
  160. // 如果上面查询为空,则创建cart
  161. $this->createCart();
  162. }
  163. }
  164. }
  165. return $this->_cart;
  166. }
  167. /**
  168. * @param $cart | $this->_cartModel Object
  169. * 设置$this->_cart 为 当前传递的$cart对象。
  170. */
  171. public function setCart($cart)
  172. {
  173. $this->_cart = $cart;
  174. }
  175. /**
  176. * @return $items_count | Int , 得到购物车中产品的个数。头部的ajax请求一般访问这个.
  177. * 目前是通过表查询获取的。
  178. */
  179. public function getCartItemCount()
  180. {
  181. $items_count = 0;
  182. if ($cart_id = $this->getCartId()) {
  183. if ($cart_id) {
  184. $cart = $this->getCart();
  185. //$one = $this->_cartModel->findOne(['cart_id' => $cart_id]);
  186. if (isset($cart['items_count']) && $cart['items_count']) {
  187. $items_count = $cart['items_count'];
  188. }
  189. }
  190. }
  191. return $items_count;
  192. }
  193. /**
  194. * @param $item_qty | Int
  195. * 当$active_item_qty为null时,从cart items表中查询产品总数。
  196. * 当$item_qty 不等于null时,代表已经知道购物车中active产品的个数,不需要去cart_item表中查询,譬如清空购物车操作,直接就知道产品个数肯定为零。
  197. * 当购物车的产品变动后,会调用该函数,更新cart表的产品总数
  198. */
  199. public function computeCartInfo($active_item_qty = null)
  200. {
  201. $items_all_count = 0;
  202. if ($active_item_qty === null) {
  203. $active_item_qty = Yii::$service->cart->quoteItem->getActiveItemQty();
  204. }
  205. $items_all_count = Yii::$service->cart->quoteItem->getItemAllQty();
  206. $cart = $this->getCart();
  207. $cart->items_all_count = $items_all_count;
  208. $cart->items_count = $active_item_qty;
  209. $cart->save();
  210. return true;
  211. }
  212. /**
  213. * @param int $cart_id cart id
  214. * 设置cart_id类变量以及session中记录当前cartId的值
  215. * Cart的session的超时时间由session组件决定。
  216. */
  217. protected function actionSetCartId($cart_id)
  218. {
  219. $this->_cart_id = $cart_id;
  220. if (!Yii::$service->store->isAppserver()) {
  221. Yii::$service->session->set(self::SESSION_CART_ID, $cart_id);
  222. }
  223. }
  224. /**
  225. * 删除掉active状态的购物车产品
  226. * 对于active的产品,在支付成功后,这些产品从购物车清楚
  227. * 而对于noActive产品,这些产品并没有支付,因而在购物车中保留。
  228. */
  229. protected function actionClearCart()
  230. {
  231. //Yii::$service->session->remove(self::SESSION_CART_ID);
  232. Yii::$service->cart->quoteItem->removeNoActiveItemsByCartId();
  233. }
  234. /**
  235. * 初始化创建cart信息,
  236. * 在用户的第一个产品加入购物车时,会在数据库中创建购物车.
  237. */
  238. protected function actionCreateCart()
  239. {
  240. $myCart = new $this->_cartModelName;
  241. $myCart->store = Yii::$service->store->currentStore;
  242. $myCart->created_at = time();
  243. $myCart->updated_at = time();
  244. if (!Yii::$app->user->isGuest) {
  245. $identity = Yii::$app->user->identity;
  246. $id = $identity['id'];
  247. $firstname = $identity['firstname'];
  248. $lastname = $identity['lastname'];
  249. $email = $identity['email'];
  250. $myCart->customer_id = $id;
  251. $myCart->customer_email = $email;
  252. $myCart->customer_firstname = $firstname;
  253. $myCart->customer_lastname = $lastname;
  254. $myCart->customer_is_guest = 2;
  255. } else {
  256. $myCart->customer_is_guest = 1;
  257. }
  258. $myCart->remote_ip = \fec\helpers\CFunc::get_real_ip();
  259. $myCart->app_name = Yii::$service->helper->getAppName();
  260. //if ($defaultShippingMethod = Yii::$service->shipping->getDefaultShippingMethod()) {
  261. // $myCart->shipping_method = $defaultShippingMethod;
  262. //}
  263. $myCart->save();
  264. $cart_id = $myCart['cart_id'];
  265. $this->setCartId($cart_id);
  266. $this->setCart($this->_cartModel->findOne($cart_id));
  267. }
  268. /** 该函数已经废弃
  269. * 购物车数据中是否含有address_id,address_id,是登录用户才会有的。
  270. */
  271. /*
  272. public function hasAddressId()
  273. {
  274. $cart = $this->getCart();
  275. $address_id = $cart['customer_address_id'];
  276. if ($address_id) {
  277. return true;
  278. }
  279. }
  280. */
  281. /**
  282. * 得到购物车中的用户地址信息.
  283. * @deprecated 该函数已经废弃
  284. */
  285. /*
  286. public function getCartAddress()
  287. {
  288. $email = '';
  289. $first_name = '';
  290. $last_name = '';
  291. if (!Yii::$app->user->isGuest) {
  292. $identity = Yii::$app->user->identity;
  293. $email = isset($identity['email']) ? $identity['email'] : '';
  294. $first_name = isset($identity['first_name']) ? $identity['first_name'] : '';
  295. $last_name = isset($identity['last_name']) ? $identity['last_name'] : '';
  296. }
  297. $cart = $this->getCurrentCart();
  298. $customer_email = isset($cart['customer_email']) ? $cart['customer_email'] : '';
  299. $customer_firstname = isset($cart['customer_firstname']) ? $cart['customer_firstname'] : '';
  300. $customer_lastname = isset($cart['customer_lastname']) ? $cart['customer_lastname'] : '';
  301. $customer_telephone = isset($cart['customer_telephone']) ? $cart['customer_telephone'] : '';
  302. $customer_address_country = isset($cart['customer_address_country']) ? $cart['customer_address_country'] : '';
  303. $customer_address_state = isset($cart['customer_address_state']) ? $cart['customer_address_state'] : '';
  304. $customer_address_city = isset($cart['customer_address_city']) ? $cart['customer_address_city'] : '';
  305. $customer_address_zip = isset($cart['customer_address_zip']) ? $cart['customer_address_zip'] : '';
  306. $customer_address_street1 = isset($cart['customer_address_street1']) ? $cart['customer_address_street1'] : '';
  307. $customer_address_street2 = isset($cart['customer_address_street2']) ? $cart['customer_address_street2'] : '';
  308. $customer_email = $customer_email ? $customer_email : $email;
  309. $customer_firstname = $customer_firstname ? $customer_firstname : $first_name;
  310. $customer_lastname = $customer_lastname ? $customer_lastname : $last_name;
  311. return [
  312. 'first_name' => $customer_firstname,
  313. 'last_name' => $customer_lastname,
  314. 'email' => $customer_email,
  315. 'telephone' => $customer_telephone,
  316. 'country' => $customer_address_country,
  317. 'state' => $customer_address_state,
  318. 'city' => $customer_address_city,
  319. 'zip' => $customer_address_zip,
  320. 'street1' => $customer_address_street1,
  321. 'street2' => $customer_address_street2,
  322. ];
  323. }
  324. */
  325. /**
  326. * @param $activeProduct | boolean , 是否只要active的产品
  327. * @param $shipping_method | String 传递的货运方式
  328. * @param $country | String 货运国家
  329. * @param $region | String 省市
  330. * @return bool OR array ,如果存在问题返回false,对于返回的数组的格式参看下面$this->cartInfo[$cartInfoKey] 部分的数组。
  331. * 返回当前购物车的信息。包括购物车对应的产品信息。
  332. * 对于可选参数,如果不填写,就是返回当前的购物车的数据。
  333. * 对于填写了参数,返回的是填写参数后的数据,这个一般是用户选择了了货运方式,国家等,然后
  334. * 实时的计算出来数据反馈给用户,但是,用户选择的数据并没有进入cart表
  335. */
  336. public function getCartInfo($activeProduct = true, $shipping_method = '', $country = '', $region = '*')
  337. {
  338. // 根据传递的参数的不同,购物车数据计算一次后,第二次调用,不会重新计算数据。
  339. $cartInfoKey = $shipping_method.'-shipping-'.$country.'-country-'.$region.'-region';
  340. if (!isset($this->cartInfo[$cartInfoKey])) {
  341. $cart_id = $this->getCartId();
  342. if (!$cart_id) {
  343. return false;
  344. }
  345. $cart = $this->getCart();
  346. // 购物车中所有的产品个数
  347. $items_all_count = $cart['items_all_count'];
  348. // 购物车中active状态的产品个数
  349. $items_count = $cart['items_count'];
  350. if ($items_count <=0 && $items_all_count <= 0) {
  351. return false;
  352. }
  353. $coupon_code = $cart['coupon_code'];
  354. $cart_product_info = Yii::$service->cart->quoteItem->getCartProductInfo($activeProduct);
  355. //var_dump($cart_product_info);
  356. if (is_array($cart_product_info)) {
  357. $product_weight = $cart_product_info['product_weight'];
  358. $product_volume_weight = $cart_product_info['product_volume_weight'];
  359. $product_volume = $cart_product_info['product_volume'];
  360. $product_final_weight = max($product_weight, $product_volume_weight);
  361. $products = $cart_product_info['products'];
  362. $product_total = $cart_product_info['product_total'];
  363. $base_product_total = $cart_product_info['base_product_total'];
  364. $product_qty_total = $cart_product_info['product_qty_total'];
  365. //if (!$shipping_method) {
  366. // $shipping_method = Yii::$service->shipping->getDefaultShippingMethod($country,$region,$product_weight);
  367. //}
  368. if (is_array($products) && !empty($products)) {
  369. $currShippingCost = 0;
  370. $baseShippingCost = 0;
  371. if ($shipping_method && $product_final_weight) {
  372. $shippingCost = $this->getShippingCost($shipping_method, $product_final_weight, $country, $region);
  373. $currShippingCost = $shippingCost['currCost'];
  374. $baseShippingCost = $shippingCost['baseCost'];
  375. }
  376. $couponCost = $this->getCouponCost($base_product_total, $coupon_code);
  377. $baseDiscountCost = $couponCost['baseCost'];
  378. $currDiscountCost = $couponCost['currCost'];
  379. $curr_grand_total = $product_total + $currShippingCost - $currDiscountCost;
  380. $base_grand_total = $base_product_total + $baseShippingCost - $baseDiscountCost;
  381. if (!$shipping_method) {
  382. $shipping_method = $cart['shipping_method'];
  383. }
  384. $this->cartInfo[$cartInfoKey] = [
  385. 'cart_id' => $cart_id,
  386. 'store' => $cart['store'], // store nme
  387. 'items_count' => $product_qty_total, // 因为购物车使用了active,因此生成订单的产品个数 = 购物车中active的产品的总个数(也就是在购物车页面用户勾选的产品的总数),而不是字段 $cart['items_count']
  388. 'coupon_code' => $coupon_code, // coupon卷码
  389. 'shipping_method' => $shipping_method,
  390. 'payment_method' => $cart['payment_method'],
  391. 'grand_total' => Yii::$service->helper->format->number_format($curr_grand_total), // 当前货币总金额
  392. 'shipping_cost' => Yii::$service->helper->format->number_format($currShippingCost), // 当前货币,运费
  393. 'coupon_cost' => Yii::$service->helper->format->number_format($currDiscountCost), // 当前货币,优惠券优惠金额
  394. 'product_total' => Yii::$service->helper->format->number_format($product_total), // 当前货币,购物车中产品的总金额
  395. 'base_grand_total' => Yii::$service->helper->format->number_format($base_grand_total), // 基础货币总金额
  396. 'base_shipping_cost'=> Yii::$service->helper->format->number_format($baseShippingCost), // 基础货币,运费
  397. 'base_coupon_cost' => Yii::$service->helper->format->number_format($baseDiscountCost), // 基础货币,优惠券优惠金额
  398. 'base_product_total'=> Yii::$service->helper->format->number_format($base_product_total), // 基础货币,购物车中产品的总金额
  399. 'products' => $products, //产品信息。
  400. 'product_weight' => Yii::$service->helper->format->number_format($product_weight), //产品的总重量。
  401. 'product_volume_weight' => Yii::$service->helper->format->number_format($product_volume_weight),
  402. 'product_volume' => Yii::$service->helper->format->number_format($product_volume),
  403. ];
  404. }
  405. }
  406. }
  407. return $this->cartInfo[$cartInfoKey];
  408. }
  409. /**
  410. * @param $shippingCost | Array ,example:
  411. * [
  412. * 'currCost' => 33.22, #当前货币的运费金额
  413. * 'baseCost' => 26.44, #基础货币的运费金额
  414. * ];
  415. * 设置快递运费金额。根据国家地址和产品重量等信息计算出来的运费
  416. */
  417. public function setShippingCost($shippingCost)
  418. {
  419. $this->_shipping_cost = $shippingCost;
  420. }
  421. /**
  422. * @param $shipping_method | String 货运方式
  423. * @param $weight | Float 产品重量
  424. * @param $country | String 国家
  425. * @param $region | String 省/市
  426. * @return $this->_shipping_cost | Array ,format:
  427. * [
  428. * 'currCost' => 33.22, #当前货币的运费金额
  429. * 'baseCost' => 26.44, #基础货币的运费金额
  430. * ];
  431. * 得到快递运费金额。
  432. */
  433. public function getShippingCost($shipping_method = '', $weight = '', $country = '', $region = '')
  434. {
  435. if (!$this->_shipping_cost) {
  436. $available_method = Yii::$service->shipping->getAvailableShippingMethods($country, $region, $weight);
  437. $shippingInfo = $available_method[$shipping_method];
  438. $shippingCost = Yii::$service->shipping->getShippingCost($shipping_method, $shippingInfo, $weight, $country, $region);
  439. $this->_shipping_cost = $shippingCost;
  440. }
  441. return $this->_shipping_cost;
  442. }
  443. /**
  444. * 得到优惠券的折扣金额.
  445. * @return array , example:
  446. * [
  447. * 'baseCost' => $base_discount_cost, # 基础货币的优惠金额
  448. * 'currCost' => $curr_discount_cost # 当前货币的优惠金额
  449. * ]
  450. */
  451. public function getCouponCost($base_product_total, $coupon_code)
  452. {
  453. $dc_discount = Yii::$service->cart->coupon->getDiscount($coupon_code, $base_product_total);
  454. return $dc_discount;
  455. }
  456. /**
  457. * @param $coupon_code | String
  458. * 设置购物车的优惠券
  459. */
  460. public function setCartCoupon($coupon_code)
  461. {
  462. $cart = $this->getCart();
  463. $cart->coupon_code = $coupon_code;
  464. $cart->save();
  465. return true;
  466. }
  467. /**
  468. * @param $coupon_code | String
  469. * 取消购物车的优惠券
  470. */
  471. public function cancelCartCoupon($coupon_code)
  472. {
  473. $cart = $this->getCart();
  474. $cart->coupon_code = null;
  475. $cart->save();
  476. return true;
  477. }
  478. /**
  479. * 当用户登录账号后,将用户未登录时的购物车和用户账号中保存
  480. * 的购物车信息进行合并。
  481. */
  482. public function mergeCartAfterUserLogin()
  483. {
  484. if (!Yii::$app->user->isGuest) {
  485. $identity = Yii::$app->user->identity;
  486. $customer_id = $identity['id'];
  487. $email = $identity->email;
  488. $customer_firstname = $identity->firstname;
  489. $customer_lastname = $identity->lastname;
  490. $customer_cart = $this->getCartByCustomerId($customer_id);
  491. $cart_id = $this->getCartId();
  492. if (!$customer_cart) {
  493. if ($cart_id) {
  494. $cart = $this->getCart();
  495. if ($cart) {
  496. $cart['customer_email'] = $email;
  497. $cart['customer_id'] = $customer_id;
  498. $cart['customer_firstname'] = $customer_firstname;
  499. $cart['customer_lastname'] = $customer_lastname;
  500. $cart['customer_is_guest'] = 2;
  501. $cart->save();
  502. }
  503. }
  504. } else {
  505. $cart = $this->getCart();
  506. if (!$cart || !$cart_id) {
  507. $cart_id = $customer_cart['cart_id'];
  508. $this->setCartId($cart_id);
  509. } else {
  510. // 将无用户产品(当前)和 购物车中的产品(登录用户对应的购物车)进行合并。
  511. $new_cart_id = $customer_cart['cart_id'];
  512. if ($cart['coupon_code']) {
  513. // 如果有优惠券则取消,以登录用户的购物车的优惠券为准。
  514. Yii::$service->cart->coupon->cancelCoupon($cart['coupon_code']);
  515. }
  516. // 将当前购物车产品表的cart_id 改成 登录用户对应的cart_id
  517. if ($new_cart_id && $cart_id && ($new_cart_id != $cart_id)) {
  518. Yii::$service->cart->quoteItem->updateCartId($new_cart_id, $cart_id);
  519. // 当前的购物车删除掉
  520. $cart->delete();
  521. // 设置当前的cart_id
  522. $this->setCartId($new_cart_id);
  523. // 设置当前的cart
  524. $this->setCart($customer_cart);
  525. // 重新计算购物车中产品的个数
  526. $this->computeCartInfo();
  527. }
  528. }
  529. }
  530. }
  531. }
  532. /**
  533. * @param $customer_id | int
  534. * @return $this->_cartModel Object。
  535. * 通过用户的customer_id,在cart表中找到对应的购物车
  536. */
  537. public function getCartByCustomerId($customer_id)
  538. {
  539. if ($customer_id) {
  540. $one = $this->_cartModel->findOne(['customer_id' => $customer_id]);
  541. if ($one['cart_id']) {
  542. return $one;
  543. }
  544. }
  545. }
  546. }