重构结账页total计算

This commit is contained in:
Edward Yang 2022-12-05 17:34:04 +08:00
parent 55423be548
commit fc496aa4dd
6 changed files with 29 additions and 11 deletions

View File

@ -191,7 +191,7 @@ class CheckoutService
'shipping_methods' => $shipments,
'payment_methods' => $payments,
'carts' => $carts,
'totals' => $totalService->getTotals(),
'totals' => $totalService->getTotals($this),
];
return hook_filter('checkout.data', $data);

View File

@ -83,9 +83,10 @@ class TotalService
/**
* @param CheckoutService $checkout
* @return array
*/
public function getTotals(): array
public function getTotals(CheckoutService $checkout): array
{
foreach (self::TOTAL_CODES as $code) {
$serviceName = Str::studly($code) . 'Service';
@ -93,7 +94,7 @@ class TotalService
if (!class_exists($service) || !method_exists($service, 'getTotal')) {
continue;
}
$service::getTotal($this);
$service::getTotal($checkout);
}
return $this->totals;

View File

@ -11,12 +11,17 @@
namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
use Beike\Shop\Services\CheckoutService;
class OrderTotalService
{
public static function getTotal(TotalService $totalService)
/**
* @param CheckoutService $checkout
* @return array
*/
public static function getTotal(CheckoutService $checkout)
{
$totalService = $checkout->totalService;
$amount = $totalService->amount;
$totalData = [
'code' => 'order_total',

View File

@ -12,18 +12,19 @@
namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
use Illuminate\Support\Str;
use Beike\Shop\Services\CheckoutService;
class ShippingService
{
/**
* @param TotalService $totalService
* @param CheckoutService $checkout
* @return array|null
* @throws \Exception
*/
public static function getTotal(TotalService $totalService): ?array
public static function getTotal(CheckoutService $checkout): ?array
{
$totalService = $checkout->totalService;
$shippingMethod = $totalService->shippingMethod;
if (empty($shippingMethod)) {
return null;

View File

@ -13,11 +13,17 @@
namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
use Beike\Shop\Services\CheckoutService;
class SubtotalService
{
public static function getTotal(TotalService $totalService)
/**
* @param CheckoutService $checkout
* @return array
*/
public static function getTotal(CheckoutService $checkout)
{
$totalService = $checkout->totalService;
$amount = self::getAmount($totalService);
$totalData = [
'code' => 'sub_total',

View File

@ -12,13 +12,18 @@
namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\CheckoutService;
use Beike\Admin\Repositories\TaxRateRepo;
use Beike\Shop\Services\TotalService;
class TaxService
{
public static function getTotal(TotalService $totalService): ?array
/**
* @param CheckoutService $checkout
* @return array|null
*/
public static function getTotal(CheckoutService $checkout): ?array
{
$totalService = $checkout->totalService;
$taxEnabled = system_setting('base.tax', false);
if (!$taxEnabled) {
return null;