FreightHandler.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace addons\TinyShop\common\components\marketing;
  3. use Yii;
  4. use yii\web\UnprocessableEntityHttpException;
  5. use addons\TinyShop\common\components\delivery\PickupDelivery;
  6. use addons\TinyShop\common\components\delivery\LocalDelivery;
  7. use addons\TinyShop\common\forms\PreviewForm;
  8. use addons\TinyShop\common\components\PreviewInterface;
  9. /**
  10. * 运费计算
  11. *
  12. * 如果是自提不计算运费
  13. *
  14. * Class FreightHandler
  15. * @package addons\TinyShop\common\components
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class FreightHandler extends PreviewInterface
  19. {
  20. /**
  21. * @param PreviewForm $form
  22. * @return PreviewForm
  23. * @throws UnprocessableEntityHttpException
  24. */
  25. public function execute(PreviewForm $form): PreviewForm
  26. {
  27. $form->shipping_money = 0;
  28. if ($form->freight == true && $form->is_full_mail == false && $form->address) {
  29. try {
  30. $form->shipping_money = Yii::$app->tinyShopService->expressFee->getPrice(
  31. $form->defaultProducts,
  32. $form->fullProductIds,
  33. $form->company_id,
  34. $form->merchant_id,
  35. $form->address,
  36. $form->config['logistics_select']
  37. );
  38. } catch (\Exception $e) {
  39. // 下单才开始报错
  40. if ($this->isNewRecord) {
  41. throw new UnprocessableEntityHttpException($e->getMessage());
  42. }
  43. return $form;
  44. }
  45. }
  46. // 成功触发
  47. return $this->success($form);
  48. }
  49. /**
  50. * 排斥营销
  51. *
  52. * @return array
  53. */
  54. public function rejectNames()
  55. {
  56. return [
  57. PickupDelivery::getName(), // 自提
  58. LocalDelivery::getName(), // 同城配送
  59. FullMailHandler::getName(), // 满包邮
  60. ];
  61. }
  62. /**
  63. * 营销名称
  64. *
  65. * @return string
  66. */
  67. public static function getName(): string
  68. {
  69. return 'freight';
  70. }
  71. }
粤ICP备19079148号