add totals
This commit is contained in:
parent
21fb3e07ed
commit
acfad8a621
|
|
@ -145,14 +145,12 @@ 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(),
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -122,6 +122,7 @@ class CheckoutService
|
|||
|
||||
$cartList = CartService::list($customer, true);
|
||||
$carts = CartService::reloadData($cartList);
|
||||
$totalService = (new TotalService($cartList))->setShippingMethod($this->cart->shipping_method_code);
|
||||
|
||||
$data = [
|
||||
'current' => [
|
||||
|
|
@ -136,8 +137,10 @@ class CheckoutService
|
|||
'addresses' => AddressResource::collection($addresses),
|
||||
'shipping_methods' => $shipments,
|
||||
'payment_methods' => $payments,
|
||||
'carts' => $carts
|
||||
'carts' => $carts,
|
||||
'totals' => $totalService->getTotals(),
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,32 +19,48 @@ class TotalService
|
|||
'subtotal',
|
||||
'tax',
|
||||
'shipping',
|
||||
'total'
|
||||
'order_total'
|
||||
];
|
||||
|
||||
public array $carts;
|
||||
public array $totals;
|
||||
public float $amount = 0;
|
||||
public string $shippingMethod = '';
|
||||
|
||||
public function __construct($carts)
|
||||
{
|
||||
$this->carts = $carts;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置配送方式
|
||||
*/
|
||||
public function setShippingMethod($methodCode): TotalService
|
||||
{
|
||||
$this->shippingMethod = $methodCode;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTotals(): array
|
||||
{
|
||||
$totals = [];
|
||||
foreach (self::TOTAL_CODES as $code) {
|
||||
$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);
|
||||
$totalData = $service::getTotal($this);
|
||||
if ($totalData) {
|
||||
$this->amount += $totalData['amount'];
|
||||
$this->totals[] = $totalData;
|
||||
}
|
||||
}
|
||||
|
||||
return $totals;
|
||||
return $this->totals;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
/**
|
||||
* TotalService.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-07-27 17:49:15
|
||||
* @modified 2022-07-27 17:49:15
|
||||
*/
|
||||
|
||||
namespace Beike\Shop\Services\TotalServices;
|
||||
|
||||
use Beike\Shop\Services\TotalService;
|
||||
|
||||
class OrderTotalService
|
||||
{
|
||||
public static function getTotal(TotalService $totalService)
|
||||
{
|
||||
$amount = $totalService->amount;
|
||||
return [
|
||||
'code' => 'order_total',
|
||||
'title' => '应付总金额',
|
||||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -12,10 +12,22 @@
|
|||
|
||||
namespace Beike\Shop\Services\TotalServices;
|
||||
|
||||
use Beike\Shop\Services\TotalService;
|
||||
|
||||
class ShippingService
|
||||
{
|
||||
public static function getTotal()
|
||||
public static function getTotal(TotalService $totalService)
|
||||
{
|
||||
|
||||
$shippingMethod = $totalService->shippingMethod;
|
||||
if (empty($shippingMethod)) {
|
||||
return null;
|
||||
}
|
||||
$amount = 5;
|
||||
return [
|
||||
'code' => 'shipping',
|
||||
'title' => '运费',
|
||||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,14 +12,19 @@
|
|||
|
||||
namespace Beike\Shop\Services\TotalServices;
|
||||
|
||||
use Beike\Shop\Services\TotalService;
|
||||
|
||||
class SubtotalService
|
||||
{
|
||||
public static function getTotal($totalService)
|
||||
public static function getTotal(TotalService $totalService)
|
||||
{
|
||||
$carts = $totalService->carts;
|
||||
$amount = collect($carts)->sum('subtotal');
|
||||
return [
|
||||
'code' => 'sub_total',
|
||||
'value' => collect($carts)->sum('total')
|
||||
'title' => '商品总额',
|
||||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,10 +12,18 @@
|
|||
|
||||
namespace Beike\Shop\Services\TotalServices;
|
||||
|
||||
use Beike\Shop\Services\TotalService;
|
||||
|
||||
class TaxService
|
||||
{
|
||||
public static function getTotal()
|
||||
public static function getTotal(TotalService $totalService)
|
||||
{
|
||||
|
||||
$amount = $totalService->amount * 0.02;
|
||||
return [
|
||||
'code' => 'shipping',
|
||||
'title' => '运费',
|
||||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue