Cart.php 9.4 KB

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