From 2a2067204a10b271dce4371d483855d20dbb193d Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 26 Apr 2023 11:12:36 +0800 Subject: [PATCH] Add hook for total service. --- beike/Shop/Services/TotalService.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/beike/Shop/Services/TotalService.php b/beike/Shop/Services/TotalService.php index cd98af87..b1030322 100644 --- a/beike/Shop/Services/TotalService.php +++ b/beike/Shop/Services/TotalService.php @@ -17,7 +17,7 @@ use Illuminate\Support\Str; class TotalService { - public const TOTAL_CODES = [ + private const TOTAL_CODES = [ 'subtotal', 'tax', 'shipping', @@ -92,16 +92,31 @@ class TotalService */ public function getTotals(CheckoutService $checkout): array { - foreach (self::TOTAL_CODES as $code) { - $serviceName = Str::studly($code) . 'Service'; - $service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}"; + $maps = $this->getTotalClassMaps(); + foreach ($maps as $service) { if (! class_exists($service) || ! method_exists($service, 'getTotal')) { continue; } $service::getTotal($checkout); } - return $this->totals; + return hook_filter('service.total.totals', $this->totals); + } + + /** + * total 与类名 映射 + * + * @return mixed + */ + private function getTotalClassMaps() + { + $maps = []; + foreach (self::TOTAL_CODES as $code) { + $serviceName = Str::studly($code) . 'Service'; + $maps[$code] = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}"; + } + + return hook_filter('service.total.maps', $maps); } /**