SingleEnoughReduceDiscount.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/4/29
  8. * Time: 13:58
  9. */
  10. namespace app\frontend\modules\cart\discount;
  11. use app\frontend\modules\cart\models\CartGoods;
  12. class SingleEnoughReduceDiscount extends BaseCartDiscount
  13. {
  14. protected $code = 'singleEnoughReduce';
  15. protected $name = '单品满减优惠';
  16. /**
  17. * 获取金额
  18. * @return float|int
  19. * @throws \app\common\exceptions\AppException
  20. */
  21. protected function _getAmount()
  22. {
  23. if(!$this->getShopGoodsPrice()){
  24. return 0;
  25. }
  26. // (订单商品成交金额/订单中同种商品总成交金额 ) * 订单单品满减金额
  27. return ($this->cartGoods->getPriceBefore($this->getCode()) / $this->getShopGoodsPrice()) * $this->singleEnoughReduce();
  28. }
  29. /**
  30. * 商品的单品优惠满减金额
  31. * @return int
  32. */
  33. public function singleEnoughReduce()
  34. {
  35. if(is_null($this->cartGoods->goods()->hasOneSale)){
  36. return 0;
  37. }
  38. return $this->cartGoods->goods()->hasOneSale->getEnoughReductionAmount($this->getShopGoodsPrice());
  39. }
  40. /**
  41. * 店铺中同商品的价格总和
  42. * @return float
  43. */
  44. protected function getShopGoodsPrice()
  45. {
  46. return $this->cartGoods->shop->carts->where('goods_id', $this->cartGoods->goods_id)->sum(function (CartGoods $preCartGoods) {
  47. return $preCartGoods->getPriceBefore($this->getCode());
  48. });
  49. }
  50. }