Add hook for total service.

This commit is contained in:
Edward Yang 2023-04-26 11:12:36 +08:00
parent d1fed8b618
commit 2a2067204a
1 changed files with 20 additions and 5 deletions

View File

@ -17,7 +17,7 @@ use Illuminate\Support\Str;
class TotalService class TotalService
{ {
public const TOTAL_CODES = [ private const TOTAL_CODES = [
'subtotal', 'subtotal',
'tax', 'tax',
'shipping', 'shipping',
@ -92,16 +92,31 @@ class TotalService
*/ */
public function getTotals(CheckoutService $checkout): array public function getTotals(CheckoutService $checkout): array
{ {
foreach (self::TOTAL_CODES as $code) { $maps = $this->getTotalClassMaps();
$serviceName = Str::studly($code) . 'Service'; foreach ($maps as $service) {
$service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}";
if (! class_exists($service) || ! method_exists($service, 'getTotal')) { if (! class_exists($service) || ! method_exists($service, 'getTotal')) {
continue; continue;
} }
$service::getTotal($checkout); $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);
} }
/** /**