QuoteItem.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 services. 对购物车产品操作的具体实现部分。
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class QuoteItem extends Service
  18. {
  19. public $itemDefaultActiveStatus = 1;
  20. public $activeStatus = 1;
  21. public $noActiveStatus = 2;
  22. protected $_my_cart_item; // 购物车cart item 对象
  23. protected $_cart_product_info;
  24. protected $_itemModelName = '\fecshop\models\mysqldb\cart\Item';
  25. /**
  26. * @var \fecshop\models\mysqldb\cart\Item
  27. */
  28. protected $_itemModel;
  29. public function init()
  30. {
  31. parent::init();
  32. list($this->_itemModelName, $this->_itemModel) = Yii::mapGet($this->_itemModelName);
  33. }
  34. /**
  35. * 将某个产品加入到购物车中。
  36. * 在添加到 cart_item 表后,更新购物车中产品的总数。
  37. * @param array $item
  38. * @param Object $product , Product Model
  39. * @return mixed
  40. * example:
  41. * $item = [
  42. * 'product_id' => 22222,
  43. * 'custom_option_sku' => red-xxl,
  44. * 'qty' => 22,
  45. * 'sku' => 'xxxx',
  46. * ];
  47. */
  48. public function addItem($item, $product)
  49. {
  50. $cart_id = Yii::$service->cart->quote->getCartId();
  51. if (!$cart_id) {
  52. Yii::$service->cart->quote->createCart();
  53. $cart_id = Yii::$service->cart->quote->getCartId();
  54. }
  55. //
  56. if (!isset($item['product_id']) || empty($item['product_id'])) {
  57. Yii::$service->helper->errors->add('add to cart error, product id is empty');
  58. return false;
  59. }
  60. $where = [
  61. 'cart_id' => $cart_id,
  62. 'product_id' => $item['product_id'],
  63. ];
  64. if (isset($item['custom_option_sku']) && !empty($item['custom_option_sku'])) {
  65. $where['custom_option_sku'] = $item['custom_option_sku'];
  66. }
  67. if (isset($item['customsize']) && !empty($item['customsize'])) {
  68. $where['customsize'] = $item['customsize'];
  69. }
  70. /** @var \fecshop\models\mysqldb\cart\Item $item_one */
  71. $item_one = $this->_itemModel->find()->where($where)->one();
  72. if ($item_one['cart_id']) {
  73. // 检查产品满足加入购物车的条件
  74. $checkItem = $item;
  75. $checkItem['qty'] = $item['qty'] + $item_one['qty'];
  76. $productValidate = Yii::$service->cart->info->checkProductBeforeAdd($checkItem, $product);
  77. if (!$productValidate) {
  78. return false;
  79. }
  80. $item_one->active = $this->itemDefaultActiveStatus;
  81. $item_one->qty = $item['qty'] + $item_one['qty'];
  82. $item_one->save();
  83. // 重新计算购物车的数量
  84. Yii::$service->cart->quote->computeCartInfo();
  85. } else {
  86. // 检查产品满足加入购物车的条件
  87. $checkItem = $item;
  88. $productValidate = Yii::$service->cart->info->checkProductBeforeAdd($checkItem, $product);
  89. if (!$productValidate) {
  90. return false;
  91. }
  92. $item_one = new $this->_itemModelName;
  93. $item_one->store = Yii::$service->store->currentStore;
  94. $item_one->cart_id = $cart_id;
  95. $item_one->created_at = time();
  96. $item_one->updated_at = time();
  97. $item_one->product_id = $item['product_id'];
  98. $item_one->customsize = $item['customsize'];
  99. $item_one->qty = $item['qty'];
  100. $item_one->active = $this->itemDefaultActiveStatus;
  101. $item_one->custom_option_sku = ($item['custom_option_sku'] ? $item['custom_option_sku'] : '');
  102. $item_one->save();
  103. // 重新计算购物车的数量,并写入 sales_flat_cart 表存储
  104. Yii::$service->cart->quote->computeCartInfo();
  105. }
  106. $item['afterAddQty'] = $item_one->qty;
  107. $this->sendTraceAddToCartInfoByApi($item);
  108. return true;
  109. }
  110. /**
  111. * @param $item | Array, example:
  112. * $item = [
  113. * 'product_id' => 22222,
  114. * 'custom_option_sku' => red-xxl,
  115. * 'qty' => 22, // 添加购物车的产品个数
  116. * 'sku' => 'xxxx',
  117. * 'afterAddQty' => 33, // 添加后,该产品在sku中的个数,这个个数是为了计算购物车中产品的价格
  118. * ];
  119. * 将加入购物车的操作,加入trace
  120. */
  121. public function sendTraceAddToCartInfoByApi($item)
  122. {
  123. if (Yii::$service->page->trace->traceJsEnable) {
  124. $product_price_arr = Yii::$service->product->price->getCartPriceByProductId($item['product_id'], $item['afterAddQty'], $item['custom_option_sku'], 2);
  125. $base_product_price = isset($product_price_arr['base_price']) ? $product_price_arr['base_price'] : 0;
  126. // $price = $base_product_price * $item['qty'];
  127. $trace_cart_info = [
  128. [
  129. 'sku' => $item['sku'],
  130. 'price' => $base_product_price,
  131. 'qty' => $item['qty'],
  132. ]
  133. ];
  134. Yii::$service->page->trace->sendTraceAddToCartInfoByApi($trace_cart_info);
  135. }
  136. }
  137. /**
  138. * @param $item | Array, example:
  139. * $item = [
  140. * 'product_id' => 22222,
  141. * 'custom_option_sku' => red-xxl,
  142. * 'qty' => 22,
  143. * ];
  144. * @return boolean;
  145. * 将购物车中的某个产品更改个数,更改后的个数就是上面qty的值。
  146. * @deprecated 该函数已经被遗弃
  147. */
  148. /*
  149. public function changeItemQty($item)
  150. {
  151. $cart_id = Yii::$service->cart->quote->getCartId();
  152. // 查看是否存在此产品,如果存在,则更改
  153. $item_one = $this->_itemModel->find()->where([
  154. 'cart_id' => $cart_id,
  155. 'product_id' => $item['product_id'],
  156. 'custom_option_sku' => $item['custom_option_sku'],
  157. ])->one();
  158. if ($item_one['cart_id']) {
  159. $item_one->qty = $item['qty'];
  160. $item_one->save();
  161. // 重新计算购物车的数量
  162. Yii::$service->cart->quote->computeCartInfo();
  163. return true;
  164. } else {
  165. Yii::$service->helper->errors->add('This product is not available in the shopping cart');
  166. return false;
  167. }
  168. }
  169. */
  170. /**
  171. * 通过quoteItem表,计算得到所有产品的总数
  172. * 得到购物车中产品的总数,不要使用这个函数,这个函数的作用:
  173. * 在购物车中产品有变动后,使用这个函数得到产品总数,更新购物车中
  174. * 的产品总数。
  175. */
  176. public function getItemAllQty()
  177. {
  178. $cart_id = Yii::$service->cart->quote->getCartId();
  179. $item_qty = 0;
  180. if ($cart_id) {
  181. $data = $this->_itemModel->find()->asArray()->where([
  182. 'cart_id' => $cart_id,
  183. ])->all();
  184. if (is_array($data) && !empty($data)) {
  185. foreach ($data as $one) {
  186. $product_id = $one['product_id'];
  187. $productModel = Yii::$service->product->getByPrimaryKey($product_id);
  188. if ($productModel && isset($productModel['status']) && Yii::$service->product->isActive($productModel['status'])) {
  189. $item_qty += $one['qty'];
  190. }
  191. }
  192. }
  193. }
  194. return $item_qty;
  195. }
  196. /**
  197. * 通过quoteItem表,计算得到所有产品的总数
  198. * 得到购物车中产品的总数,不要使用这个函数,这个函数的作用:
  199. * 在购物车中产品有变动后,使用这个函数得到产品总数,更新购物车中
  200. * 的产品总数。
  201. */
  202. public function getActiveItemQty()
  203. {
  204. $cart_id = Yii::$service->cart->quote->getCartId();
  205. $item_qty = 0;
  206. if ($cart_id) {
  207. $data = $this->_itemModel->find()->asArray()->where([
  208. 'cart_id' => $cart_id,
  209. 'active' => $this->activeStatus,
  210. ])->all();
  211. if (is_array($data) && !empty($data)) {
  212. foreach ($data as $one) {
  213. $product_id = $one['product_id'];
  214. $productModel = Yii::$service->product->getByPrimaryKey($product_id);
  215. if ($productModel && isset($productModel['status']) && Yii::$service->product->isActive($productModel['status'])) {
  216. $item_qty += $one['qty'];
  217. }
  218. }
  219. }
  220. }
  221. return $item_qty;
  222. }
  223. /**
  224. * @param $activeProduct | boolean , 是否只要active的产品
  225. * @return array , foramt:
  226. * [
  227. * 'products' => $products, # 产品详细信息,详情参看代码中的$products。
  228. * 'product_total' => $product_total, # 产品的当前货币总额
  229. * 'base_product_total' => $base_product_total,# 产品的基础货币总额
  230. * 'product_weight'=> $product_weight, # 蟾皮的总重量、
  231. * ]
  232. * 得到当前购物车的产品信息,具体参看上面的example array。
  233. */
  234. public function getCartProductInfo($activeProduct = true)
  235. {
  236. $cart_id = Yii::$service->cart->quote->getCartId();
  237. $products = [];
  238. $product_total = 0;
  239. $product_weight = 0;
  240. $product_volume_weight = 0;
  241. $base_product_total = 0;
  242. $product_volume = 0;
  243. $product_qty_total = 0;
  244. $productPrimaryKey = Yii::$service->product->getPrimaryKey();
  245. if ($cart_id) {
  246. if (!isset($this->_cart_product_info[$cart_id])) {
  247. $data = $this->_itemModel->find()->where([
  248. 'cart_id' => $cart_id,
  249. ])->orderBy( ['active' => SORT_ASC, 'updated_at' => SORT_DESC]) // 加入按照active updated_at 进行排序
  250. ->all();
  251. if (is_array($data) && !empty($data)) {
  252. foreach ($data as $one) {
  253. $active = $one['active'];
  254. if ($activeProduct && ($active != $this->activeStatus)) {
  255. continue;
  256. }
  257. $product_id = $one['product_id'];
  258. $product_one = Yii::$service->product->getByPrimaryKey($product_id);
  259. if ($product_one[$productPrimaryKey]) {
  260. $qty = $one['qty'];
  261. $custom_option_sku = $one['custom_option_sku'];
  262. $customsize = $one['customsize'];
  263. $product_price_arr = Yii::$service->product->price->getCartPriceByProductId($product_id, $qty, $custom_option_sku, 2);
  264. $curr_product_price = isset($product_price_arr['curr_price']) ? $product_price_arr['curr_price'] : 0;
  265. $base_product_price = isset($product_price_arr['base_price']) ? $product_price_arr['base_price'] : 0;
  266. $product_price = isset($curr_product_price['value']) ? $curr_product_price['value'] : 0;
  267. $product_row_price = $product_price * $qty;
  268. $base_product_row_price = $base_product_price * $qty;
  269. $volume = Yii::$service->shipping->getVolume($product_one['long'], $product_one['width'], $product_one['high']);
  270. $p_pv = $volume * $qty;
  271. $p_wt = $product_one['weight'] * $qty;
  272. $p_vwt = $product_one['volume_weight'] * $qty;
  273. if ($active == $this->activeStatus) {
  274. $product_total += $product_row_price;
  275. $base_product_total += $base_product_row_price;
  276. $product_weight += $p_wt;
  277. $product_volume_weight += $p_vwt;
  278. $product_volume += $p_pv;
  279. $product_qty_total += $qty;
  280. }
  281. $productSpuOptions = $this->getProductSpuOptions($product_one);
  282. $products[] = [
  283. 'item_id' => $one['item_id'],
  284. 'active' => $active,
  285. 'product_id' => $product_id,
  286. 'sku' => $product_one['sku'],
  287. 'name' => Yii::$service->store->getStoreAttrVal($product_one['name'], 'name'),
  288. 'qty' => $qty,
  289. 'custom_option_sku' => $custom_option_sku,
  290. 'customsize' => $customsize,
  291. 'product_price' => $product_price,
  292. 'product_row_price' => $product_row_price,
  293. 'base_product_price' => $base_product_price,
  294. 'base_product_row_price'=> $base_product_row_price,
  295. 'product_name' => $product_one['name'],
  296. 'product_weight' => $product_one['weight'],
  297. 'product_row_weight'=> $p_wt,
  298. 'product_volume_weight' => $product_one['volume_weight'],
  299. 'product_row_volume_weight' => $p_vwt,
  300. 'product_volume' => $volume,
  301. 'product_row_volume' => $p_pv,
  302. 'product_url' => $product_one['url_key'],
  303. 'product_image' => $product_one['image'],
  304. 'custom_option' => $product_one['custom_option'],
  305. 'spu_options' => $productSpuOptions,
  306. ];
  307. }
  308. }
  309. $this->_cart_product_info[$cart_id] = [
  310. 'products' => $products,
  311. 'product_qty_total' => $product_qty_total,
  312. 'product_total' => $product_total,
  313. 'base_product_total' => $base_product_total,
  314. 'product_weight' => $product_weight,
  315. 'product_volume_weight' => $product_volume_weight,
  316. 'product_volume' => $product_volume,
  317. ];
  318. }
  319. }
  320. return $this->_cart_product_info[$cart_id];
  321. }
  322. }
  323. /**
  324. * @param $productOb | Object,类型:\fecshop\models\mongodb\Product
  325. * 得到产品的spu对应的属性以及值。
  326. * 概念 - spu options:当多个产品是同一个spu,但是不同的sku的时候,他们的产品表里面的
  327. * spu attr 的值是不同的,譬如对应鞋子,size 和 color 就是spu attr,对于同一款鞋子,他们
  328. * 是同一个spu,对于尺码,颜色不同的鞋子,是不同的sku,他们的spu attr 就是 color 和 size。
  329. */
  330. protected function getProductSpuOptions($productOb)
  331. {
  332. $custom_option_info_arr = [];
  333. $productPrimaryKey = Yii::$service->product->getPrimaryKey();
  334. $productAttrGroup = $productOb['attr_group'];
  335. if (isset($productOb['attr_group']) && !empty($productOb['attr_group'])) {
  336. $spuArr = Yii::$service->product->getSpuAttr($productAttrGroup);
  337. //var_dump($productOb['attr_group_info']);
  338. // mysql存储
  339. if (isset($productOb['attr_group_info']) && is_array($productOb['attr_group_info'])) {
  340. $attr_group_info = $productOb['attr_group_info'];
  341. if (is_array($spuArr) && !empty($spuArr)) {
  342. foreach ($spuArr as $spu_attr) {
  343. if (isset($attr_group_info[$spu_attr]) && !empty($attr_group_info[$spu_attr])) {
  344. // 进行翻译。
  345. $spu_attr_label = Yii::$service->page->translate->__($spu_attr);
  346. $spu_attr_val = Yii::$service->page->translate->__($attr_group_info[$spu_attr]);
  347. $custom_option_info_arr[$spu_attr_label] = $spu_attr_val;
  348. }
  349. }
  350. }
  351. } else { // mongodb类型
  352. Yii::$service->product->addGroupAttrs($productAttrGroup);
  353. $productOb = Yii::$service->product->getByPrimaryKey((string) $productOb[$productPrimaryKey ]);
  354. if (is_array($spuArr) && !empty($spuArr)) {
  355. foreach ($spuArr as $spu_attr) {
  356. if (isset($productOb[$spu_attr]) && !empty($productOb[$spu_attr])) {
  357. // 进行翻译。
  358. $spu_attr_label = Yii::$service->page->translate->__($spu_attr);
  359. $spu_attr_val = Yii::$service->page->translate->__($productOb[$spu_attr]);
  360. $custom_option_info_arr[$spu_attr_label] = $spu_attr_val;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. return $custom_option_info_arr;
  367. }
  368. /**
  369. * @param $item_id | Int , quoteItem表的id
  370. * @return bool
  371. * 将这个item_id对应的产品个数+1.
  372. */
  373. public function addOneItem($item_id)
  374. {
  375. $cart_id = Yii::$service->cart->quote->getCartId();
  376. if ($cart_id) {
  377. $one = $this->_itemModel->find()->where([
  378. 'cart_id' => $cart_id,
  379. 'item_id' => $item_id,
  380. ])->one();
  381. $product_id = $one['product_id'];
  382. if ($one['item_id'] && $product_id) {
  383. $product = Yii::$service->product->getByPrimaryKey($product_id);
  384. // 检查产品满足加入购物车的条件
  385. $checkItem = [
  386. 'product_id' => $one['product_id'],
  387. 'custom_option_sku' => $one['custom_option_sku'],
  388. 'qty' => $one['qty'] + 1,
  389. ];
  390. $productValidate = Yii::$service->cart->info->checkProductBeforeAdd($checkItem, $product);
  391. if (!$productValidate) {
  392. return false;
  393. }
  394. $changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
  395. $one['qty'] = $one['qty'] + $changeQty;
  396. $one->save();
  397. // 重新计算购物车的数量
  398. Yii::$service->cart->quote->computeCartInfo();
  399. $item = [
  400. 'product_id' => $product_id,
  401. 'custom_option_sku' => $one['custom_option_sku'],
  402. 'qty' => $changeQty,
  403. 'sku' => $product['sku'],
  404. 'afterAddQty' => $one['qty'],
  405. ];
  406. // 购物车数据加1
  407. $this->sendTraceAddToCartInfoByApi($item);
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. /**
  414. * @param $item_id | Int , quoteItem表的id
  415. * @return bool
  416. * 将这个item_id对应的产品个数-1.
  417. */
  418. public function lessOneItem($item_id)
  419. {
  420. $cart_id = Yii::$service->cart->quote->getCartId();
  421. if ($cart_id) {
  422. $one = $this->_itemModel->find()->where([
  423. 'cart_id' => $cart_id,
  424. 'item_id' => $item_id,
  425. ])->one();
  426. $product_id = $one['product_id'];
  427. $product = Yii::$service->product->getByPrimaryKey($one['product_id']);
  428. $changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
  429. $lessedQty = $one['qty'] - $changeQty;
  430. $min_sales_qty = 1;
  431. if ($product['min_sales_qty'] && $product['min_sales_qty'] >= 2) {
  432. $min_sales_qty = $product['min_sales_qty'];
  433. }
  434. if ($lessedQty < $min_sales_qty) {
  435. Yii::$service->helper->errors->add('product less buy qty is {min_sales_qty}', ['min_sales_qty' => $product['min_sales_qty']]);
  436. return false;
  437. }
  438. if ($one['item_id']) {
  439. if ($one['qty'] > 1) {
  440. $one['qty'] = $lessedQty;
  441. $one->save();
  442. // 重新计算购物车的数量
  443. Yii::$service->cart->quote->computeCartInfo();
  444. return true;
  445. }
  446. }
  447. }
  448. return false;
  449. }
  450. /**
  451. * @param $item_id | Int , quoteItem表的id
  452. * @return bool
  453. * 将这个item_id对应的产品删除
  454. */
  455. public function removeItem($item_id)
  456. {
  457. $cart_id = Yii::$service->cart->quote->getCartId();
  458. if ($cart_id) {
  459. $one = $this->_itemModel->find()->where([
  460. 'cart_id' => $cart_id,
  461. 'item_id' => $item_id,
  462. ])->one();
  463. if ($one['item_id']) {
  464. $one->delete();
  465. // 重新计算购物车的数量
  466. Yii::$service->cart->quote->computeCartInfo();
  467. return true;
  468. }
  469. }
  470. return false;
  471. }
  472. /**
  473. * @param $item_id | Int , quoteItem表的id
  474. * @return bool
  475. * 将这个item_id对应的产品个数+1.
  476. */
  477. public function selectOneItem($item_id, $checked)
  478. {
  479. $cart_id = Yii::$service->cart->quote->getCartId();
  480. if ($cart_id) {
  481. $one = $this->_itemModel->find()->where([
  482. 'cart_id' => $cart_id,
  483. 'item_id' => $item_id,
  484. ])->one();
  485. $product_id = $one['product_id'];
  486. if ($one['item_id'] && $product_id) {
  487. //$product = Yii::$service->product->getByPrimaryKey($product_id);
  488. //$changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
  489. //$one['qty'] = $one['qty'] + $changeQty;
  490. if ($checked == true) {
  491. $one->active = $this->activeStatus;
  492. } else {
  493. $one->active = $this->noActiveStatus;
  494. }
  495. $one->save();
  496. // 重新计算购物车的数量
  497. Yii::$service->cart->quote->computeCartInfo();
  498. return true;
  499. }
  500. }
  501. return false;
  502. }
  503. /**
  504. * @param $item_id | Int , quoteItem表的id
  505. * @return bool
  506. * 将这个item_id对应的产品个数+1.
  507. */
  508. public function selectAllItem($checked)
  509. {
  510. $cart_id = Yii::$service->cart->quote->getCartId();
  511. if ($cart_id) {
  512. $active = $this->noActiveStatus;
  513. if ($checked == true) {
  514. $active = $this->activeStatus;
  515. }
  516. $updateCount = $this->_itemModel->updateAll(
  517. ['active' => $active],
  518. ['cart_id' => $cart_id]
  519. );
  520. if ($updateCount > 0) {
  521. Yii::$service->cart->quote->computeCartInfo();
  522. }
  523. return true;
  524. }
  525. return false;
  526. }
  527. /**
  528. * @param $cart_id | int 购物车id
  529. * 删除购物车中的所有的active产品。对于noActive产品保留
  530. * 注意:清空购物车并不是清空所有信息,仅仅是清空用户购物车中的产品。
  531. * 另外,购物车的数目更改后,需要更新cart中产品个数的信息。
  532. */
  533. public function removeNoActiveItemsByCartId($cart_id = '')
  534. {
  535. if (!$cart_id) {
  536. $cart_id = Yii::$service->cart->quote->getCartId();
  537. }
  538. if ($cart_id) {
  539. $columns = $this->_itemModel->deleteAll([
  540. 'cart_id' => $cart_id,
  541. 'active' => $this->activeStatus,
  542. ]);
  543. if ($columns > 0) {
  544. // 重新计算购物车的数量
  545. Yii::$service->cart->quote->computeCartInfo();
  546. return true;
  547. }
  548. }
  549. }
  550. /** 废弃,改为 removeNoActiveItemsByCartId(),因为购物车改为勾选下单方式。
  551. * @param $cart_id | int 购物车id
  552. * 删除购物车中的所有产品。
  553. * 注意:清空购物车并不是清空所有信息,仅仅是清空用户购物车中的产品。
  554. * 另外,购物车的数目更改后,需要更新cart中产品个数的信息。
  555. */
  556. public function removeItemByCartId($cart_id = '')
  557. {
  558. if (!$cart_id) {
  559. $cart_id = Yii::$service->cart->quote->getCartId();
  560. }
  561. if ($cart_id) {
  562. $items = $this->_itemModel->deleteAll([
  563. 'cart_id' => $cart_id,
  564. //'item_id' => $item_id,
  565. ]);
  566. // 重新计算购物车的数量
  567. Yii::$service->cart->quote->computeCartInfo(0);
  568. }
  569. return true;
  570. }
  571. /**
  572. * @param $new_cart_id | int 更新后的cart_id
  573. * @param $cart_id | int 更新前的cart_id
  574. * 删除购物车中的所有产品。
  575. * 这里仅仅更改cart表的cart_id, 而不会做其他任何事情。
  576. */
  577. public function updateCartId($new_cart_id, $cart_id)
  578. {
  579. if ($cart_id && $new_cart_id) {
  580. $this->_itemModel->updateAll(
  581. ['cart_id' => $new_cart_id], // $attributes
  582. ['cart_id' => $cart_id] // $condition
  583. );
  584. // 重新计算购物车的数量
  585. //Yii::$service->cart->quote->computeCartInfo();
  586. return true;
  587. }
  588. return false;
  589. }
  590. }