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
|
|
076b9c96
Administrator
big commti
|
20
|
public function actionMain()
|
0fc3bf10
Yarik
Quickbuy modal
|
21
22
23
24
25
26
27
28
29
30
|
{
$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
|
*/
|
5181a91a
Administrator
big commti
|
50
|
if( !$product->price > 0) {
|
0fc3bf10
Yarik
Quickbuy modal
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/**
* Добавляем сообщение об ошибке
*/
\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);
|
37415bee
Administrator
big commti
|
73
74
75
76
|
if(isset($product->image)){
$productV[ $index ][ 'img' ] = \common\components\artboximage\ArtboxImageHelper::getImageSrc($product->image->imageUrl, 'list');
}
|
0fc3bf10
Yarik
Quickbuy modal
|
77
|
}
|
d09f430f
Administrator
big commti
|
78
|
}
|
0fc3bf10
Yarik
Quickbuy modal
|
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
|
/**
* Сохраняем заказ
*/
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
|
104
|
'variants' => $productV,
|
0fc3bf10
Yarik
Quickbuy modal
|
105
|
],
|
d09f430f
Administrator
big commti
|
106
|
]);
|
0fc3bf10
Yarik
Quickbuy modal
|
107
108
109
110
111
112
113
114
115
116
117
118
119
|
//$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
|
120
|
}
|
b905419d
Administrator
big commti
|
121
|
|
0fc3bf10
Yarik
Quickbuy modal
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
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()) {
|
5181a91a
Administrator
big commti
|
142
|
if( $product_variant->price <= 0) {
|
0fc3bf10
Yarik
Quickbuy modal
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
$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('Товар не найден или не удалось загрузить данные о покупателе.');
}
}
|
b905419d
Administrator
big commti
|
167
168
169
170
171
172
173
174
|
public function actionSuccess()
{
return $this->render('success');
}
|
0fc3bf10
Yarik
Quickbuy modal
|
175
176
177
178
179
|
public function actionQuickBasket()
{
$response = \Yii::$app->response;
$request = \Yii::$app->request;
$response->format = $response::FORMAT_JSON;
|
7f87d6f9
Yarik
Modals.
|
180
|
/**
|
0fc3bf10
Yarik
Quickbuy modal
|
181
|
* @var array $data
|
7f87d6f9
Yarik
Modals.
|
182
|
*/
|
0fc3bf10
Yarik
Quickbuy modal
|
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
$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);
|
5181a91a
Administrator
big commti
|
197
|
if(!empty($product_variant) && $product_variant->price > 0) {
|
0fc3bf10
Yarik
Quickbuy modal
|
198
199
200
201
202
203
204
205
206
207
208
209
|
$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.
|
210
|
} else {
|
0fc3bf10
Yarik
Quickbuy modal
|
211
212
213
214
215
216
217
218
|
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.
|
219
|
return [
|
0fc3bf10
Yarik
Quickbuy modal
|
220
|
'result' => 'Спасибо за заказ! Наши менеджеры свяжутся с Вами в ближайшее время.',
|
7f87d6f9
Yarik
Modals.
|
221
|
];
|
0fc3bf10
Yarik
Quickbuy modal
|
222
223
|
} else {
throw new InvalidConfigException('Товар не найден или не удалось загрузить данные о покупателе.');
|
7f87d6f9
Yarik
Modals.
|
224
|
}
|
7f87d6f9
Yarik
Modals.
|
225
|
}
|
7f87d6f9
Yarik
Modals.
|
226
|
}
|