保存totals
This commit is contained in:
parent
9da2cd8234
commit
badabe39cb
|
|
@ -29,4 +29,9 @@ class Order extends Base
|
|||
{
|
||||
return $this->hasMany(OrderProduct::class);
|
||||
}
|
||||
|
||||
public function orderTotals(): HasMany
|
||||
{
|
||||
return $this->hasMany(OrderTotal::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* OrderTotal.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-07-28 11:26:47
|
||||
* @modified 2022-07-28 11:26:47
|
||||
*/
|
||||
|
||||
namespace Beike\Models;
|
||||
|
||||
class OrderTotal extends Base
|
||||
{
|
||||
protected $fillable = [
|
||||
'order_id', 'code', 'value', 'title', 'reference'
|
||||
];
|
||||
}
|
||||
|
|
@ -103,16 +103,18 @@ class OrderRepo
|
|||
public static function create(array $data): Order
|
||||
{
|
||||
$customer = $data['customer'] ?? null;
|
||||
$current = $data['checkout']['current'] ?? [];
|
||||
$carts = $data['checkout']['carts'] ?? [];
|
||||
$current = $data['current'] ?? [];
|
||||
$carts = $data['carts'] ?? [];
|
||||
$totals = $data['totals'] ?? [];
|
||||
|
||||
$shippingAddressId = $current['shipping_address_id'] ?? 0;
|
||||
$paymentAddressId = $current['payment_address_id'] ?? 0;
|
||||
|
||||
$shippingAddress = AddressRepo::find($shippingAddressId);
|
||||
$paymentAddress = AddressRepo::find($paymentAddressId);
|
||||
|
||||
$shippingMethodCode = $data['shipping_method_code'] ?? '';
|
||||
$paymentMethodCode = $data['payment_method_code'] ?? '';
|
||||
$shippingMethodCode = $current['shipping_method_code'] ?? '';
|
||||
$paymentMethodCode = $current['payment_method_code'] ?? '';
|
||||
|
||||
$order = new Order([
|
||||
'number' => self::generateOrderNumber(),
|
||||
|
|
@ -155,6 +157,7 @@ class OrderRepo
|
|||
$order->saveOrFail();
|
||||
|
||||
OrderProductRepo::create($order, $carts['carts']);
|
||||
OrderTotalRepo::createTotals($order, $totals);
|
||||
// OrderHistoryRepo::create($order);
|
||||
|
||||
return $order;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* OrderTotalRepo.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-07-28 10:30:03
|
||||
* @modified 2022-07-28 10:30:03
|
||||
*/
|
||||
|
||||
namespace Beike\Repositories;
|
||||
|
||||
use Beike\Models\Order;
|
||||
|
||||
class OrderTotalRepo
|
||||
{
|
||||
public static function createTotals(Order $order, array $totals)
|
||||
{
|
||||
$items = [];
|
||||
foreach ($totals as $total) {
|
||||
$items[] = [
|
||||
'code' => $total['code'],
|
||||
'value' => $total['amount'],
|
||||
'title' => $total['title'],
|
||||
'reference' => json_encode($total['reference'] ?? ''),
|
||||
];
|
||||
}
|
||||
$order->orderTotals()->createMany($items);
|
||||
}
|
||||
}
|
||||
|
|
@ -40,13 +40,11 @@ class CheckoutController extends Controller
|
|||
/**
|
||||
* 确认提交订单
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Order
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function confirm(Request $request): Order
|
||||
public function confirm(): Order
|
||||
{
|
||||
$data = $request->all();
|
||||
return (new CheckoutService)->confirm($data);
|
||||
return (new CheckoutService)->confirm();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,13 +75,16 @@ class CheckoutService
|
|||
* 确认提交订单
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function confirm($data): \Beike\Models\Order
|
||||
public function confirm(): \Beike\Models\Order
|
||||
{
|
||||
$customer = current_customer();
|
||||
$data['customer'] = $customer;
|
||||
$data['checkout'] = self::checkoutData();
|
||||
$order = OrderRepo::create($data);
|
||||
$checkoutData = self::checkoutData();
|
||||
$checkoutData['customer'] = $customer;
|
||||
|
||||
$order = OrderRepo::create($checkoutData);
|
||||
|
||||
CartRepo::clearSelectedCartProducts($customer);
|
||||
|
||||
// Notification::endmail();
|
||||
// Notification::sendsms();
|
||||
return $order;
|
||||
|
|
|
|||
Loading…
Reference in New Issue