重构配送方式,一个运费插件支持多个配送方式

This commit is contained in:
Edward Yang 2022-12-01 11:48:58 +08:00
parent 04c3385ef1
commit 58223f5ea8
3 changed files with 70 additions and 4 deletions

View File

@ -0,0 +1,46 @@
<?php
/**
* ShippingMethodService.php
*
* @copyright 2022 beikeshop.com - All Rights Reserved
* @link https://beikeshop.com
* @author Edward Yang <yangjin@guangda.work>
* @created 2022-12-01 10:54:46
* @modified 2022-12-01 10:54:46
*/
namespace Beike\Services;
use Illuminate\Support\Str;
use Beike\Repositories\PluginRepo;
class ShippingMethodService
{
/**
* 获取配送方式, 二维数组, 一个配送插件对应多个配送方式
*
* @param $currentCart
* @return array
* @throws \Exception
*/
public static function getShippingMethods($currentCart): array
{
$shippingPlugins = PluginRepo::getShippingMethods();
$shippingMethods = [];
foreach ($shippingPlugins as $shippingPlugin) {
$pluginCode = $shippingPlugin->code;
$pluginName = Str::studly($pluginCode);
$className = "Plugin\\{$pluginName}\\Bootstrap";
if (!method_exists($className, 'getQuotes')) {
throw new \Exception("请在插件 {$className} 实现方法: public function getQuotes(\$currentCart)");
}
$quotes = (new $className)->getQuotes($currentCart, $shippingPlugin);
if ($quotes) {
$shippingMethods[$pluginCode] = $quotes;
}
}
return $shippingMethods;
}
}

View File

@ -21,9 +21,9 @@ use Beike\Repositories\PluginRepo;
use Beike\Repositories\AddressRepo;
use Beike\Repositories\CountryRepo;
use Beike\Services\StateMachineService;
use Beike\Services\ShippingMethodService;
use Beike\Shop\Http\Resources\Account\AddressResource;
use Beike\Shop\Http\Resources\Checkout\PaymentMethodItem;
use Beike\Shop\Http\Resources\Checkout\ShippingMethodItem;
class CheckoutService
{
@ -159,6 +159,7 @@ class CheckoutService
* 获取结账页数据
*
* @return array
* @throws \Exception
*/
public function checkoutData(): array
{
@ -166,7 +167,7 @@ class CheckoutService
$currentCart = $this->cart;
$addresses = AddressRepo::listByCustomer($customer);
$shipments = ShippingMethodItem::collection(PluginRepo::getShippingMethods())->jsonSerialize();
$shipments = ShippingMethodService::getShippingMethods($currentCart);
$payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize();
$cartList = CartService::list($customer, true);

View File

@ -11,12 +11,31 @@
namespace Plugin\FlatShipping;
use Beike\Shop\Http\Resources\Checkout\ShippingMethodItem;
class Bootstrap
{
/**
* @return float
* 获取固定运费方式
*
* @param $currentCart
* @param $shippingPlugin
* @return array
*/
public function getShippingFee($totalService)
public function getQuotes($currentCart, $shippingPlugin): array
{
$quotes['flat_shipping.0'] = (new ShippingMethodItem($shippingPlugin))->jsonSerialize();
return $quotes;
}
/**
* 计算固定运费
*
* @param $totalService
* @return float|int
*/
public function getShippingFee($totalService): float|int
{
$amount = $totalService->amount;
$shippingType = plugin_setting('flat_shipping.type', 'fixed');