Info.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * Info sub-service of cart service.
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Info extends Service
  18. {
  19. /**
  20. * 单个sku加入购物车的最大个数。
  21. */
  22. public $maxCountAddToCart = 100;
  23. // 上架状态产品加入购物车时,
  24. // 如果addToCartCheckSkuQty设置为true,则需要检查产品qty是否>购买qty,
  25. // 如果设置为false,则不需要,也就是说产品库存qty小于购买qty,也是可以加入购物车的。
  26. public $addToCartCheckSkuQty = false;
  27. /**
  28. * Checks several conditions before you add product to cart
  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. * @param \fecshop\models\mongodb\Product $product
  37. * @return bool true if can add product to cart, false otherwise
  38. */
  39. public function checkProductBeforeAdd($item, $product)
  40. {
  41. // 加入购物车的产品个数不能小于设置的最小购买数量
  42. $qty = $item['qty'];
  43. $min_sales_qty = $product['min_sales_qty'];
  44. if (($min_sales_qty > 0) && ($min_sales_qty > $qty)) {
  45. Yii::$service->helper->errors->add('The minimum number of shopping carts for this item is {min_sales_qty}', ['min_sales_qty' => $min_sales_qty]);
  46. return false;
  47. }
  48. // 验证产品是否是激活状态
  49. if ($product['status'] != Yii::$service->product->getEnableStatus()) {
  50. Yii::$service->helper->errors->add('product is not active');
  51. return false;
  52. }
  53. // 加入购物车的产品个数超出 购物车中产品的最大个数。
  54. if ($qty > $this->maxCountAddToCart) {
  55. Yii::$service->helper->errors->add('The number of products added to the shopping cart can not exceed {max_count_add_to_cart}', ['max_count_add_to_cart' => $this->maxCountAddToCart]);
  56. return false;
  57. }
  58. // 验证提交产品数据
  59. // 验证产品是否存在
  60. if (!$product['sku']) {
  61. Yii::$service->helper->errors->add('The product is not exist');
  62. return false;
  63. }
  64. // 验证库存 是否库存满足
  65. $product_id = $item['product_id'];
  66. $custom_option_sku = $item['custom_option_sku'];
  67. if ($this->addToCartCheckSkuQty) {
  68. // 验证:1.上架状态, 2.库存个数是否大于购买个数
  69. // 该验证方式是默认验证方式
  70. if (!Yii::$service->product->stock->productIsInStock($product, $qty, $custom_option_sku)) {
  71. Yii::$service->helper->errors->add('The qty of product in stock is less than your purchase qty');
  72. return false;
  73. }
  74. } else {
  75. // 验证:1.上架状态
  76. if (!Yii::$service->product->stock->checkOnShelfStatus($product['is_in_stock'])) {
  77. Yii::$service->helper->errors->add('The product is out of stock');
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. /**
  84. * @param $item | Array , 数据格式为:
  85. * [
  86. * 'product_id' => xxxxx
  87. * 'qty' => 55,
  88. * 'custom_option_sku' => [
  89. * 'color' => 'red',
  90. * 'size' => 'L',
  91. * ]
  92. * ]
  93. * @param $product | Product Model , 产品的model对象
  94. * $product['custom_option'] 的数据格式如下:
  95. * [
  96. * "black-s-s2-s3": [
  97. * "my_color": "black",
  98. * "my_size": "S",
  99. * "my_size2": "S2",
  100. * "my_size3": "S3",
  101. * "sku": "black-s-s2-s3",
  102. * "qty": NumberInt(99999),
  103. * "price": 0,
  104. * "image": "/2/01/20161024170457_10036.jpg"
  105. * ],
  106. * ]
  107. * @return string 得到 custom option 部分对应的sku
  108. * 当用户在产品加入购物车的时候选择了颜色尺码等属性,通过这个函数,可以得到这些属性对应的custom_option_sku的值。
  109. * 如果不存在,则返回为空。
  110. */
  111. public function getCustomOptionSku($item, $product)
  112. {
  113. $custom_option_arr = $item['custom_option_sku'];
  114. if ($custom_option_arr) {
  115. $product_custom_option = $product['custom_option'];
  116. $co_sku = Yii::$service->product->info->getProductCOSku($custom_option_arr, $product_custom_option);
  117. if ($co_sku) {
  118. return $co_sku;
  119. }
  120. }
  121. return '';
  122. }
  123. }