Cart.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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;
  10. use Yii;
  11. /**
  12. * Cart services. 购物车service, 执行购物车部分对应的方法。
  13. *
  14. * @property \fecshop\services\cart\Coupon $coupon coupon sub-service of cart
  15. * @property \fecshop\services\cart\Info $info info sub-service of cart
  16. * @property \fecshop\services\cart\Quote $quote quote sub-service of cart
  17. * @property \fecshop\services\cart\QuoteItem $quoteItem quoteItem sub-service of cart
  18. *
  19. * @method getCartInfo($activeProduct, $shippingMethod = '', $country = '', $region = '*') see [[\fecshop\services\Cart::actionGetCartInfo()]]
  20. * @method mergeCartAfterUserLogin() see [[\fecshop\services\Cart::actionmergeCartAfterUserLogin()]]
  21. *
  22. * @author Terry Zhao <2358269014@qq.com>
  23. * @since 1.0
  24. */
  25. class Cart extends Service
  26. {
  27. /**
  28. * 将某个产品加入到购物车中
  29. * @param array $item
  30. * example:
  31. * $item = [
  32. * 'product_id' => 22222,
  33. * 'custom_option_sku' => ['color'=>'red','size'=>'l'],
  34. * 'qty' => 22,
  35. * ];
  36. * 注意: $item['custom_option_sku'] 除了为上面的数组格式,还可以为字符串
  37. * 为字符串的时候,字符串标示的就是产品的custom option sku
  38. * @return bool true if add product to cart successfully, false otherwise
  39. */
  40. protected function actionAddProductToCart($item)
  41. {
  42. $product = Yii::$service->product->getByPrimaryKey($item['product_id']);
  43. // 根据传递的值,得到 custom_option_sku 的值
  44. if (isset($item['custom_option_sku']) && !empty($item['custom_option_sku'])) {
  45. if (is_array($item['custom_option_sku'])) {
  46. $custom_option_sku = Yii::$service->cart->info->getCustomOptionSku($item, $product);
  47. if (!$custom_option_sku) {
  48. Yii::$service->helper->errors->add('product custom_option_sku is not exist');
  49. return false;
  50. }
  51. $item['custom_option_sku'] = $custom_option_sku;
  52. }
  53. }
  54. $item['sku'] = $product['sku'];
  55. $item['qty'] = $this->getCartQty($product['package_number'], $item['qty']);
  56. // 开始加入购物车
  57. // service 里面不允许有事务,请在调用层使用事务。
  58. $beforeEventName = 'event_add_to_cart_before';
  59. $afterEventName = 'event_add_to_cart_after';
  60. /**
  61. * 此处属于 fecshop 自造的简单事件 event ,比较简洁
  62. * 详情参看:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_event.html
  63. */
  64. Yii::$service->event->trigger($beforeEventName, $item); // 触发事件 - 加购物车前事件
  65. if (!Yii::$service->cart->quoteItem->addItem($item, $product)) {
  66. return false;
  67. }
  68. Yii::$service->event->trigger($afterEventName, $item); // 触发事件 - 加购物车后事件
  69. return true;
  70. }
  71. /**
  72. * @param int $package_number 打包销售的个数,这个是产品编辑的时候,如果某个商品想打包作为销售单位,填写的值
  73. * @param int $addQty 加入购物车的产品个数。
  74. * @return int 得到在购物车个数变动数,根据产品的打包销售数进行改变
  75. */
  76. public function getCartQty($package_number, $addQty)
  77. {
  78. if ($package_number >= 2) {
  79. return (int)($addQty * $package_number);
  80. } else {
  81. return $addQty;
  82. }
  83. }
  84. /**
  85. * 得到购物车中产品的个数,详情参看调用的函数注释
  86. */
  87. protected function actionGetCartItemQty()
  88. {
  89. return Yii::$service->cart->quote->getCartItemCount();
  90. }
  91. /**
  92. * @param $shipping_method | String 货运方式code
  93. * @param $country | String 国家code
  94. * @param $region | String 省市code
  95. * 得到购物车中的信息。详情参看调用的函数注释
  96. */
  97. protected function actionGetCartInfo($activeProduct = true, $shipping_method = '', $country = '', $region = '*')
  98. {
  99. return Yii::$service->cart->quote->getCartInfo($activeProduct, $shipping_method, $country, $region);
  100. }
  101. /**
  102. * @param $item_id | Int 购物车产品表的id字段
  103. * 通过item id 将购物车中的某个产品的个数加一
  104. */
  105. protected function actionAddOneItem($item_id)
  106. {
  107. $status = Yii::$service->cart->quoteItem->addOneItem($item_id);
  108. if (!$status) {
  109. return false;
  110. }
  111. Yii::$service->cart->quote->computeCartInfo();
  112. return true;
  113. }
  114. /**
  115. * @param $item_id | Int 购物车产品表的id字段
  116. * 通过item id 将购物车中的某个产品的个数减一
  117. */
  118. protected function actionLessOneItem($item_id)
  119. {
  120. $status = Yii::$service->cart->quoteItem->lessOneItem($item_id);
  121. if (!$status) {
  122. return false;
  123. }
  124. Yii::$service->cart->quote->computeCartInfo();
  125. return true;
  126. }
  127. /**
  128. * @param $item_id | Int 购物车产品表的id字段
  129. * 通过item id 删除购物车中的某个产品
  130. */
  131. protected function actionRemoveItem($item_id)
  132. {
  133. $status = Yii::$service->cart->quoteItem->removeItem($item_id);
  134. if (!$status) {
  135. return false;
  136. }
  137. Yii::$service->cart->quote->computeCartInfo();
  138. return true;
  139. }
  140. /**
  141. * @param $item_id | Int 购物车产品表的id字段
  142. * 通过item id 将购物车中的某个产品的个数加一
  143. */
  144. protected function actionSelectOneItem($item_id, $checked)
  145. {
  146. $status = Yii::$service->cart->quoteItem->selectOneItem($item_id, $checked);
  147. if (!$status) {
  148. return false;
  149. }
  150. Yii::$service->cart->quote->computeCartInfo();
  151. return true;
  152. }
  153. /**
  154. * @param $item_id | Int 购物车产品表的id字段
  155. * 通过item id 将购物车中的某个产品的个数加一
  156. */
  157. protected function actionSelectAllItem($checked)
  158. {
  159. $status = Yii::$service->cart->quoteItem->selectAllItem($checked);
  160. if (!$status) {
  161. return false;
  162. }
  163. Yii::$service->cart->quote->computeCartInfo();
  164. return true;
  165. }
  166. /**
  167. * 购物车合并:对应的是用户登录前后购物车的合并
  168. * 1. 用户未登录账号,把一部分产品加入购物车
  169. * 2. 当用户登录账号的时候,账号对应的购物车信息和用户未登录前的购物车产品信息进行合并的操作
  170. * 在用户登录账户的时候,会执行该方法。
  171. */
  172. protected function actionMergeCartAfterUserLogin()
  173. {
  174. Yii::$service->cart->quote->mergeCartAfterUserLogin();
  175. }
  176. /**
  177. * @param $address|array
  178. * @param $shipping_method | String 发货方式
  179. * @param $payment_method | String 支付方式
  180. * 此函数对应的是保存游客用户的购物车数据。
  181. * 保存购物车中的货运地址保存购物车中的货运地址(姓名,电话,邮编,地址等),货运方式,支付方式等信息。
  182. * 详细参看相应函数
  183. */
  184. protected function actionUpdateGuestCart($address, $shipping_method, $payment_method)
  185. {
  186. return Yii::$service->cart->quote->updateGuestCart($address, $shipping_method, $payment_method);
  187. }
  188. /**
  189. * @param $address_id | Int
  190. * @param $shipping_method | String 货运方式
  191. * @param $payment_method | String 支付方式
  192. * 此函数对应的是登录用户的购物车数据的更新。
  193. */
  194. protected function actionUpdateLoginCart($address_id, $shipping_method, $payment_method)
  195. {
  196. return Yii::$service->cart->quote->updateLoginCart($address_id, $shipping_method, $payment_method);
  197. }
  198. /**
  199. * 清空购物车中的产品和优惠券
  200. * 在生成订单的时候被调用.
  201. * 清空cart item active的产品,而对于noActive的购物车产品保留
  202. */
  203. protected function actionClearCartProductAndCoupon()
  204. {
  205. Yii::$service->cart->quoteItem->removeNoActiveItemsByCartId();
  206. // 清空cart中的优惠券
  207. $cart = Yii::$service->cart->quote->getCurrentCart();
  208. if (!$cart['cart_id']) {
  209. Yii::$service->helper->errors->add('current cart is empty');
  210. return false;
  211. }
  212. // 如果购物车中存在优惠券,则清空优惠券。
  213. if ($cart->coupon_code) {
  214. $cart->coupon_code = null;
  215. $cart->save();
  216. }
  217. return true;
  218. }
  219. /**
  220. * 完全与当前购物车脱节,执行该函数后,如果产品添加购物车,会创建新的cart_id
  221. * 目前仅仅在登录用户退出账号的时候使用。
  222. * 该操作仅仅remove掉session保存的cart_id,并没有删除购物车的数据。
  223. */
  224. protected function actionClearCart()
  225. {
  226. Yii::$service->cart->quote->clearCart();
  227. }
  228. /** 该函数被遗弃
  229. * add cart items by pending order Id
  230. * 1. check if the order is exist ,and belong to current customer.
  231. * 2. get all item sku and custom option.
  232. * 3. add to cart like in product page ,click add to cart button.
  233. */
  234. protected function actionAddItemsByPendingOrder($order_id)
  235. {
  236. }
  237. }