d09f430f
Administrator
big commti
|
1
|
<?php
|
0fc3bf10
Yarik
Quickbuy modal
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
namespace frontend\controllers;
use common\models\Customer;
use common\models\OrdersProducts;
use common\widgets\Mailer;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use common\models\Basket;
use common\modules\product\models\ProductVariant;
use common\models\Orders;
class OrderController extends Controller
|
d09f430f
Administrator
big commti
|
18
|
{
|
0fc3bf10
Yarik
Quickbuy modal
|
19
20
21
22
23
24
25
26
27
28
29
30
|
public function actionIndex()
{
$basket = \Yii::$app->basket;
$data = $basket->getData();
$models = $basket->findModels(array_keys($data));
return $this->render('index', [
'models' => $models,
'basket' => $basket,
]);
}
|
d09f430f
Administrator
big commti
|
31
|
/**
|
0fc3bf10
Yarik
Quickbuy modal
|
32
|
* @return string
|
d09f430f
Administrator
big commti
|
33
|
*/
|
0fc3bf10
Yarik
Quickbuy modal
|
34
35
36
37
38
39
40
41
42
43
44
45
46
|
public function actionSave()
{
$modelOrder = new Orders;
/**
* @var $basket Basket
*/
$basket = \Yii::$app->basket;
$productV = $basket->getData();
if(!empty( $productV ) && $modelOrder->load(Yii::$app->request->post()) && $modelOrder->save()) {
foreach($productV as $index => $row) {
$modelOrdersProducts = new OrdersProducts();
$mod_id = $index;
$product = ProductVariant::findOne($mod_id);
|
d09f430f
Administrator
big commti
|
47
|
/**
|
0fc3bf10
Yarik
Quickbuy modal
|
48
|
* Проверяем товар на наличие
|
d09f430f
Administrator
big commti
|
49
|
*/
|
0fc3bf10
Yarik
Quickbuy modal
|
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
|
if(!$product->stock > 0 || !$product->price > 0) {
/**
* Добавляем сообщение об ошибке
*/
\Yii::$app->getSession()
->setFlash('error', 'К сожалению товара ' . $product->name . ' нет в наличии');
$basket->delete($product->product_variant_id);
unset( $productV[ $index ] );
} else {
/**
* Удаляем товар с массива и сохраняем в заказ
*/
$modelOrdersProducts->order_id = $modelOrder->id;
$modelOrdersProducts->product_name = $product->product->name;
$modelOrdersProducts->name = $product->name;
$modelOrdersProducts->price = $productV[ $index ][ 'price' ];
$modelOrdersProducts->count = $productV[ $index ][ 'count' ];
$modelOrdersProducts->sum_cost = $productV[ $index ][ 'price' ] * $productV[ $index ][ 'count' ];
$modelOrdersProducts->mod_id = $mod_id;
$modelOrdersProducts->sku = $product->sku;
$modelOrdersProducts->validate();
$modelOrdersProducts->save();
$productV[ $index ] = ArrayHelper::toArray($modelOrdersProducts);
$productV[ $index ][ 'img' ] = \common\components\artboximage\ArtboxImageHelper::getImageSrc($product->image->imageUrl, 'list');
}
|
d09f430f
Administrator
big commti
|
75
|
}
|
0fc3bf10
Yarik
Quickbuy modal
|
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
|
/**
* Сохраняем заказ
*/
if(!Yii::$app->user->id && !empty( $modelOrder->email )) {
$modelUser = new Customer();
$modelUser->role = 'person';
$modelUser->username = $modelOrder->email;
$modelUser->name = $modelOrder->name;
$modelUser->phone = $modelOrder->phone;
$modelUser->password = Yii::$app->getSecurity()
->generateRandomString(10);
$modelUser->group_id = 2;
$modelUser->save();
}
$order = clone $modelOrder;
/**
* Чистим сессию корзины
*/
$modelOrder->clearBasket();
Mailer::widget([
'type' => 'order',
'subject' => 'Спасибо за покупку',
'email' => $modelOrder->email,
'params' => [
'order' => $order,
|
d09f430f
Administrator
big commti
|
101
|
'variants' => $productV,
|
0fc3bf10
Yarik
Quickbuy modal
|
102
|
],
|
d09f430f
Administrator
big commti
|
103
|
]);
|
0fc3bf10
Yarik
Quickbuy modal
|
104
105
106
107
108
109
110
111
112
113
114
115
116
|
//$text = "# zakaz: ". $order->id .". V blijayshee vremya menedjer svyajetsya s Vami. (044) 303 90 15";
//Yii::$app->sms->send($order->phone, $text);
Yii::$app->session[ 'order_data' ] = [ 'order' => $order,
'variants' => $productV,
];
return $this->redirect([ 'order/success' ]);
}
$data = $basket->getData();
$models = $basket->findModels(array_keys($data));
return $this->render('index', [
'models' => $models,
'basket' => $basket,
]);
|
d09f430f
Administrator
big commti
|
117
|
}
|
0fc3bf10
Yarik
Quickbuy modal
|
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
public function actionSuccess()
{
return $this->render('success');
}
public function actionQuick()
{
$response = \Yii::$app->response;
$request = \Yii::$app->request;
$response->format = $response::FORMAT_JSON;
$product_variant_id = (int) $request->post('product_variant_id');
$orders = new Orders([
'scenario' => Orders::SCENARIO_QUICK,
'name' => 'Покупка в 1 клик',
]);
if(!empty( $product_variant_id )) {
/**
* @var ProductVariant $product_variant
*/
$product_variant = ProductVariant::findOne($product_variant_id);
} else {
throw new InvalidParamException('Не указан товар');
}
if(!empty( $product_variant ) && $orders->load($request->post()) && $orders->save()) {
if($product_variant->stock <= 0 || $product_variant->price <= 0) {
$orders->delete();
return [
'error' => 'К сожалению товара ' . $product_variant->name . ' нет в наличии',
];
} else {
$order_product = new OrdersProducts([
'order_id' => $orders->id,
'product_name' => $product_variant->product->name,
'name' => $product_variant->name,
'price' => $product_variant->price,
'count' => 1,
'sum_cost' => $product_variant->price,
'mod_id' => $product_variant->product_variant_id,
'sku' => $product_variant->sku,
]);
$order_product->save();
return [
'result' => 'Спасибо за заказ! Наши менеджеры свяжутся с Вами в ближайшее время.',
];
}
} else {
throw new InvalidConfigException('Товар не найден или не удалось загрузить данные о покупателе.');
}
}
public function actionQuickBasket()
{
$response = \Yii::$app->response;
$request = \Yii::$app->request;
$response->format = $response::FORMAT_JSON;
|
7f87d6f9
Yarik
Modals.
|
174
|
/**
|
0fc3bf10
Yarik
Quickbuy modal
|
175
|
* @var array $data
|
7f87d6f9
Yarik
Modals.
|
176
|
*/
|
0fc3bf10
Yarik
Quickbuy modal
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
$data = \Yii::$app->basket->getData();
$orders = new Orders([
'scenario' => Orders::SCENARIO_QUICK,
'name' => 'Покупка в 1 клик',
]);
if(!empty( $data )) {
/**
* @var ProductVariant[] $product_variants
* @var OrdersProducts[] $order_products
*/
$product_variants = [];
$order_products = [];
foreach($data as $product_variant_id => $item) {
$product_variant = ProductVariant::findOne($product_variant_id);
if(!empty($product_variant) && $product_variant->stock > 0 && $product_variant->price > 0) {
$product_variants[$product_variant_id] = $product_variant;
$order_products[$product_variant_id] = new OrdersProducts([
'product_name' => $product_variant->product->name,
'name' => $product_variant->name,
'price' => $product_variant->price,
'count' => $item['count'],
'sum_cost' => $item['count'] * $product_variant->price,
'mod_id' => $product_variant->product_variant_id,
'sku' => $product_variant->sku,
]);
}
}
|
7f87d6f9
Yarik
Modals.
|
204
|
} else {
|
0fc3bf10
Yarik
Quickbuy modal
|
205
206
207
208
209
210
211
212
|
throw new InvalidParamException('Не указан товар');
}
if(!empty( $order_products ) && $orders->load($request->post()) && $orders->save()) {
foreach($order_products as $order_product) {
$order_product->order_id = $orders->id;
$order_product->save();
}
\Yii::$app->basket->clear();
|
7f87d6f9
Yarik
Modals.
|
213
|
return [
|
0fc3bf10
Yarik
Quickbuy modal
|
214
|
'result' => 'Спасибо за заказ! Наши менеджеры свяжутся с Вами в ближайшее время.',
|
7f87d6f9
Yarik
Modals.
|
215
|
];
|
0fc3bf10
Yarik
Quickbuy modal
|
216
217
|
} else {
throw new InvalidConfigException('Товар не найден или не удалось загрузить данные о покупателе.');
|
7f87d6f9
Yarik
Modals.
|
218
|
}
|
7f87d6f9
Yarik
Modals.
|
219
|
}
|
7f87d6f9
Yarik
Modals.
|
220
|
}
|