Info.php 5.1 KB

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