wyyl/beike/Shop/Services/TotalServices/CustomerDiscountService.php

48 lines
1.2 KiB
PHP

<?php
/**
* CustomerDiscountService.php
*
* @copyright 2023 beikeshop.com - All Rights Reserved
* @link https://beikeshop.com
* @author TL <mengwb@guangda.work>
* @created 2023-02-07 18:49:15
* @modified 2023-02-07 18:49:15
*/
namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\CheckoutService;
class CustomerDiscountService
{
/**
* @param CheckoutService $checkout
* @return array
*/
public static function getTotal(CheckoutService $checkout)
{
$totalService = $checkout->totalService;
$discountFactor = current_customer()->customerGroup->discount_factor;
if ($discountFactor <= 0) {
return;
}
if ($discountFactor > 1) {
$discountFactor = 1;
}
$amount = $totalService->getSubTotal() * $discountFactor / 100;
$totalData = [
'code' => 'customer_discount',
'title' => trans('shop/carts.customer_discount'),
'amount' => -$amount,
'amount_format' => currency_format(-$amount),
];
$totalService->amount += $totalData['amount'];
$totalService->totals[] = $totalData;
return $totalData;
}
}