diff --git a/beike/Shop/Services/CartService.php b/beike/Shop/Services/CartService.php index ce6e6a01..dd7e7965 100644 --- a/beike/Shop/Services/CartService.php +++ b/beike/Shop/Services/CartService.php @@ -145,13 +145,14 @@ class CartService $selected = collect($carts)->where('selected', 1); $quantity = $selected->sum('quantity'); $amount = $selected->sum('subtotal'); + $totalService = new TotalService($carts); $data = [ 'carts' => $carts, 'quantity' => $quantity, 'amount' => $amount, 'amount_format' => currency_format($amount), - 'totals' => TotalService::getTotals(), + 'totals' => $totalService->getTotals(), ]; return $data; } diff --git a/beike/Shop/Services/TotalService.php b/beike/Shop/Services/TotalService.php index c6501b92..d282916d 100644 --- a/beike/Shop/Services/TotalService.php +++ b/beike/Shop/Services/TotalService.php @@ -11,7 +11,6 @@ namespace Beike\Shop\Services; -use \Beike\Shop\Services\TotalServices; use Illuminate\Support\Str; class TotalService @@ -23,16 +22,27 @@ class TotalService 'total' ]; + public array $carts; + public array $totals; + + public function __construct($carts) + { + $this->carts = $carts; + } + /** * @return array */ - public static function getTotals(): array + public function getTotals(): array { $totals = []; foreach (self::TOTAL_CODES as $code) { - $serviceName = Str::studly($code); - $service = "TotalServices\{$serviceName}"; - $totals[] = $service::getTotal(); + $serviceName = Str::studly($code) . 'Service'; + $service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}"; + if (!class_exists($service) || !method_exists($service, 'getTotal')) { + continue; + } + $this->totals[] = $service::getTotal($this); } return $totals; diff --git a/beike/Shop/Services/TotalServices/SubtotalService.php b/beike/Shop/Services/TotalServices/SubtotalService.php index bfa36841..c3d400dc 100644 --- a/beike/Shop/Services/TotalServices/SubtotalService.php +++ b/beike/Shop/Services/TotalServices/SubtotalService.php @@ -14,8 +14,12 @@ namespace Beike\Shop\Services\TotalServices; class SubtotalService { - public static function getTotal() + public static function getTotal($totalService) { - + $carts = $totalService->carts; + return [ + 'code' => 'sub_total', + 'value' => collect($carts)->sum('total') + ]; } }