获取购物车商品总额

This commit is contained in:
Edward Yang 2023-02-08 18:00:05 +08:00
parent a55d234e94
commit 0f0ba924ce
2 changed files with 13 additions and 9 deletions

View File

@ -102,4 +102,16 @@ class TotalService
return $this->totals; return $this->totals;
} }
/**
* 获取当前购物车商品总额
*
* @return mixed
*/
public function getSubTotal(): mixed
{
$carts = $this->cartProducts;
return collect($carts)->sum('subtotal');
}
} }

View File

@ -13,7 +13,6 @@
namespace Beike\Shop\Services\TotalServices; namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\CheckoutService; use Beike\Shop\Services\CheckoutService;
use Beike\Shop\Services\TotalService;
class SubtotalService class SubtotalService
{ {
@ -24,7 +23,7 @@ class SubtotalService
public static function getTotal(CheckoutService $checkout) public static function getTotal(CheckoutService $checkout)
{ {
$totalService = $checkout->totalService; $totalService = $checkout->totalService;
$amount = self::getAmount($totalService); $amount = $totalService->getSubTotal();
$totalData = [ $totalData = [
'code' => 'sub_total', 'code' => 'sub_total',
'title' => trans('shop/carts.product_total'), 'title' => trans('shop/carts.product_total'),
@ -37,11 +36,4 @@ class SubtotalService
return $totalData; return $totalData;
} }
public static function getAmount(TotalService $totalService)
{
$carts = $totalService->cartProducts;
return collect($carts)->sum('subtotal');
}
} }