/
OnepageController.php
executable file
·140 lines (113 loc) · 4.21 KB
/
OnepageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Webkul\Shop\Http\Controllers;
use Illuminate\Support\Facades\Event;
use Webkul\Checkout\Facades\Cart;
use Webkul\MagicAI\Facades\MagicAI;
use Webkul\Sales\Repositories\OrderRepository;
class OnepageController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index()
{
Event::dispatch('checkout.load.index');
/**
* If guest checkout is not allowed then redirect back to the cart page
*/
if (
! auth()->guard('customer')->check()
&& ! core()->getConfigData('catalog.products.guest_checkout.allow_guest_checkout')
) {
return redirect()->route('shop.customer.session.index');
}
/**
* If user is suspended then redirect back to the cart page
*/
if (auth()->guard('customer')->user()?->is_suspended) {
session()->flash('warning', trans('shop::app.checkout.cart.suspended-account-message'));
return redirect()->route('shop.checkout.cart.index');
}
/**
* If cart has errors then redirect back to the cart page
*/
if (Cart::hasError()) {
return redirect()->route('shop.checkout.cart.index');
}
$cart = Cart::getCart();
/**
* If cart is has downloadable items and customer is not logged in
* then redirect back to the cart page
*/
if (
! auth()->guard('customer')->check()
&& (
$cart->hasDownloadableItems()
|| ! $cart->hasGuestCheckoutItems()
)
) {
return redirect()->route('shop.customer.session.index');
}
/**
* If cart minimum order amount is not satisfied then redirect back to the cart page
*/
$minimumOrderAmount = (float) core()->getConfigData('sales.order_settings.minimum_order.minimum_order_amount') ?: 0;
if (! $cart->checkMinimumOrder()) {
session()->flash('warning', trans('shop::app.checkout.cart.minimum-order-message', [
'amount' => core()->currency($minimumOrderAmount),
]));
return redirect()->back();
}
return view('shop::checkout.onepage.index', compact('cart'));
}
/**
* Order success page.
*
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
*/
public function success(OrderRepository $orderRepository)
{
if (! $order = $orderRepository->find(session('order_id'))) {
return redirect()->route('shop.checkout.cart.index');
}
if (
core()->getConfigData('general.magic_ai.settings.enabled')
&& core()->getConfigData('general.magic_ai.checkout_message.enabled')
&& ! empty(core()->getConfigData('general.magic_ai.checkout_message.prompt'))
) {
try {
$model = core()->getConfigData('general.magic_ai.checkout_message.model');
$response = MagicAI::setModel($model)
->setTemperature(0)
->setPrompt($this->getCheckoutPrompt($order))
->ask();
$order->checkout_message = $response;
} catch (\Exception $e) {
}
}
return view('shop::checkout.success', compact('order'));
}
/**
* Order success page.
*
* @param \Webkul\Sales\Contracts\Order $order
* @return string
*/
public function getCheckoutPrompt($order)
{
$prompt = core()->getConfigData('general.magic_ai.checkout_message.prompt');
$products = '';
foreach ($order->items as $item) {
$products .= "Name: $item->name\n";
$products .= "Qty: $item->qty_ordered\n";
$products .= 'Price: '.core()->formatPrice($item->total)."\n\n";
}
$prompt .= "\n\nProduct Details:\n $products";
$prompt .= "Customer Details:\n $order->customer_full_name \n\n";
$prompt .= "Current Locale:\n ".core()->getCurrentLocale()->name."\n\n";
$prompt .= "Store Name:\n".core()->getCurrentChannel()->name;
return $prompt;
}
}