保存totals
This commit is contained in:
parent
9da2cd8234
commit
badabe39cb
|
|
@ -29,4 +29,9 @@ class Order extends Base
|
||||||
{
|
{
|
||||||
return $this->hasMany(OrderProduct::class);
|
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
|
public static function create(array $data): Order
|
||||||
{
|
{
|
||||||
$customer = $data['customer'] ?? null;
|
$customer = $data['customer'] ?? null;
|
||||||
$current = $data['checkout']['current'] ?? [];
|
$current = $data['current'] ?? [];
|
||||||
$carts = $data['checkout']['carts'] ?? [];
|
$carts = $data['carts'] ?? [];
|
||||||
|
$totals = $data['totals'] ?? [];
|
||||||
|
|
||||||
$shippingAddressId = $current['shipping_address_id'] ?? 0;
|
$shippingAddressId = $current['shipping_address_id'] ?? 0;
|
||||||
$paymentAddressId = $current['payment_address_id'] ?? 0;
|
$paymentAddressId = $current['payment_address_id'] ?? 0;
|
||||||
|
|
||||||
$shippingAddress = AddressRepo::find($shippingAddressId);
|
$shippingAddress = AddressRepo::find($shippingAddressId);
|
||||||
$paymentAddress = AddressRepo::find($paymentAddressId);
|
$paymentAddress = AddressRepo::find($paymentAddressId);
|
||||||
|
|
||||||
$shippingMethodCode = $data['shipping_method_code'] ?? '';
|
$shippingMethodCode = $current['shipping_method_code'] ?? '';
|
||||||
$paymentMethodCode = $data['payment_method_code'] ?? '';
|
$paymentMethodCode = $current['payment_method_code'] ?? '';
|
||||||
|
|
||||||
$order = new Order([
|
$order = new Order([
|
||||||
'number' => self::generateOrderNumber(),
|
'number' => self::generateOrderNumber(),
|
||||||
|
|
@ -155,6 +157,7 @@ class OrderRepo
|
||||||
$order->saveOrFail();
|
$order->saveOrFail();
|
||||||
|
|
||||||
OrderProductRepo::create($order, $carts['carts']);
|
OrderProductRepo::create($order, $carts['carts']);
|
||||||
|
OrderTotalRepo::createTotals($order, $totals);
|
||||||
// OrderHistoryRepo::create($order);
|
// OrderHistoryRepo::create($order);
|
||||||
|
|
||||||
return $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
|
* @return Order
|
||||||
* @throws \Throwable
|
* @throws \Throwable
|
||||||
*/
|
*/
|
||||||
public function confirm(Request $request): Order
|
public function confirm(): Order
|
||||||
{
|
{
|
||||||
$data = $request->all();
|
return (new CheckoutService)->confirm();
|
||||||
return (new CheckoutService)->confirm($data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,13 +75,16 @@ class CheckoutService
|
||||||
* 确认提交订单
|
* 确认提交订单
|
||||||
* @throws \Throwable
|
* @throws \Throwable
|
||||||
*/
|
*/
|
||||||
public function confirm($data): \Beike\Models\Order
|
public function confirm(): \Beike\Models\Order
|
||||||
{
|
{
|
||||||
$customer = current_customer();
|
$customer = current_customer();
|
||||||
$data['customer'] = $customer;
|
$checkoutData = self::checkoutData();
|
||||||
$data['checkout'] = self::checkoutData();
|
$checkoutData['customer'] = $customer;
|
||||||
$order = OrderRepo::create($data);
|
|
||||||
|
$order = OrderRepo::create($checkoutData);
|
||||||
|
|
||||||
CartRepo::clearSelectedCartProducts($customer);
|
CartRepo::clearSelectedCartProducts($customer);
|
||||||
|
|
||||||
// Notification::endmail();
|
// Notification::endmail();
|
||||||
// Notification::sendsms();
|
// Notification::sendsms();
|
||||||
return $order;
|
return $order;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue