Commit b6da636096ba5641aa299b6a0ccbed176bf6aef1
1 parent
0b5518db
-Order products ready
Showing
3 changed files
with
78 additions
and
26 deletions
Show diff stats
controllers/OrderController.php
@@ -115,6 +115,27 @@ | @@ -115,6 +115,27 @@ | ||
115 | $model = new Order(); | 115 | $model = new Order(); |
116 | 116 | ||
117 | if ($model->load(Yii::$app->request->post()) && $model->save()) { | 117 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
118 | + if (!empty(\Yii::$app->request->post('Product'))) { | ||
119 | + foreach (\Yii::$app->request->post('Product') as $id => $count) { | ||
120 | + /** | ||
121 | + * @var Variant $variant | ||
122 | + */ | ||
123 | + $variant = Variant::findOne($id); | ||
124 | + if ($variant) { | ||
125 | + $orderProduct = new OrderProduct( | ||
126 | + [ | ||
127 | + 'variant_id' => $variant->id, | ||
128 | + 'order_id' => $model->id, | ||
129 | + 'sku' => $variant->sku, | ||
130 | + 'price' => $variant->price, | ||
131 | + 'count' => $count, | ||
132 | + ] | ||
133 | + ); | ||
134 | + $orderProduct->save(); | ||
135 | + } | ||
136 | + } | ||
137 | + } | ||
138 | + | ||
118 | return $this->redirect( | 139 | return $this->redirect( |
119 | [ | 140 | [ |
120 | 'view', | 141 | 'view', |
controllers/OrderProductController.php
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | namespace artbox\order\controllers; | 3 | namespace artbox\order\controllers; |
4 | 4 | ||
5 | use artbox\order\models\OrderProductSearch; | 5 | use artbox\order\models\OrderProductSearch; |
6 | + use common\models\Variant; | ||
6 | use Yii; | 7 | use Yii; |
7 | use artbox\order\models\OrderProduct; | 8 | use artbox\order\models\OrderProduct; |
8 | use yii\helpers\Html; | 9 | use yii\helpers\Html; |
@@ -10,6 +11,7 @@ | @@ -10,6 +11,7 @@ | ||
10 | use yii\web\NotFoundHttpException; | 11 | use yii\web\NotFoundHttpException; |
11 | use yii\filters\VerbFilter; | 12 | use yii\filters\VerbFilter; |
12 | use yii\filters\AccessControl; | 13 | use yii\filters\AccessControl; |
14 | + use yii\web\Response; | ||
13 | 15 | ||
14 | /** | 16 | /** |
15 | * OrderProductController implements the CRUD actions for OrderProduct model. | 17 | * OrderProductController implements the CRUD actions for OrderProduct model. |
@@ -173,31 +175,47 @@ | @@ -173,31 +175,47 @@ | ||
173 | } | 175 | } |
174 | } | 176 | } |
175 | 177 | ||
176 | - public function actionAdd(int $variant_id, int $count = 1) | 178 | + public function actionAdd($variant_id, $count) |
177 | { | 179 | { |
178 | - | ||
179 | - $result = '<div class="row"> | ||
180 | - <div class="col-md-8">'; | ||
181 | - | ||
182 | - $result .= '</div> | ||
183 | - <div class="col-md-3">'; | ||
184 | - $result .= Html::textInput( | ||
185 | - 'Product[11994]', | ||
186 | - 1, | ||
187 | - [ | ||
188 | - 'class' => 'form-control', | ||
189 | - ] | ||
190 | - ); | ||
191 | - $result .= '</div> | ||
192 | -<div class="col-md-1">'; | ||
193 | - $result .= Html::a( | ||
194 | - '<i class="fa fa-trash"></i>', | ||
195 | - '#', | ||
196 | - [ | ||
197 | - 'class' => 'btn btn-primary', | ||
198 | - ] | ||
199 | - ); | ||
200 | - $result .= '</div> | ||
201 | -</div>'; | 180 | + \Yii::$app->response->format = Response::FORMAT_JSON; |
181 | + | ||
182 | + /** | ||
183 | + * @var Variant $variant | ||
184 | + */ | ||
185 | + $variant = Variant::find() | ||
186 | + ->where([ 'id' => $variant_id ]) | ||
187 | + ->with('product.lang') | ||
188 | + ->one(); | ||
189 | + | ||
190 | + if ($variant) { | ||
191 | + $result = '<div id="' . $variant->id . '" class="row"><div class="col-md-8">'; | ||
192 | + $result .= $variant->product->lang->title . ' (' . $variant->sku . ')'; | ||
193 | + $result .= '</div><div class="col-md-3">'; | ||
194 | + $result .= Html::input( | ||
195 | + 'number', | ||
196 | + 'Product[' . $variant->id . ']', | ||
197 | + empty($count) ? 1 : $count, | ||
198 | + [ | ||
199 | + 'class' => 'form-control count-input', | ||
200 | + ] | ||
201 | + ); | ||
202 | + $result .= '</div><div class="col-md-1">'; | ||
203 | + $result .= Html::a( | ||
204 | + '<i class="fa fa-trash"></i>', | ||
205 | + '#', | ||
206 | + [ | ||
207 | + 'class' => 'btn btn-primary delete-product-row', | ||
208 | + ] | ||
209 | + ); | ||
210 | + $result .= '</div></div>'; | ||
211 | + } else { | ||
212 | + $result = ''; | ||
213 | + } | ||
214 | + | ||
215 | + return [ | ||
216 | + 'row' => $result, | ||
217 | + 'id' => empty($variant) ? 0 : $variant->id, | ||
218 | + 'count' => empty($count) ? 1 : intval($count), | ||
219 | + ]; | ||
202 | } | 220 | } |
203 | } | 221 | } |
views/order/_form.php
@@ -30,10 +30,23 @@ $(document).on('click', '#create-add', function(e) { | @@ -30,10 +30,23 @@ $(document).on('click', '#create-add', function(e) { | ||
30 | count: count | 30 | count: count |
31 | }, | 31 | }, |
32 | success: function(data) { | 32 | success: function(data) { |
33 | - console.log(data); | 33 | + if (data.id != 0) { |
34 | + var row = $('#products #' + data.id); | ||
35 | + if (row.length !== 0) { | ||
36 | + row.find('.count-input').val(function(i, oldVal) { | ||
37 | + return parseInt(oldVal, 10) + data.count; | ||
38 | + }); | ||
39 | + } else { | ||
40 | + $('#products').append(data.row); | ||
41 | + } | ||
42 | + } | ||
34 | } | 43 | } |
35 | }); | 44 | }); |
36 | }); | 45 | }); |
46 | +$(document).on('click', '.delete-product-row', function(e) { | ||
47 | + e.preventDefault(); | ||
48 | + $(this).parent().parent().remove(); | ||
49 | +}) | ||
37 | JS; | 50 | JS; |
38 | 51 | ||
39 | $this->registerJs($js, View::POS_READY); | 52 | $this->registerJs($js, View::POS_READY); |