diff --git a/controllers/OrderController.php b/controllers/OrderController.php index d64925c..30e3a58 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -115,6 +115,27 @@ $model = new Order(); if ($model->load(Yii::$app->request->post()) && $model->save()) { + if (!empty(\Yii::$app->request->post('Product'))) { + foreach (\Yii::$app->request->post('Product') as $id => $count) { + /** + * @var Variant $variant + */ + $variant = Variant::findOne($id); + if ($variant) { + $orderProduct = new OrderProduct( + [ + 'variant_id' => $variant->id, + 'order_id' => $model->id, + 'sku' => $variant->sku, + 'price' => $variant->price, + 'count' => $count, + ] + ); + $orderProduct->save(); + } + } + } + return $this->redirect( [ 'view', diff --git a/controllers/OrderProductController.php b/controllers/OrderProductController.php index 10ded5f..04d2ceb 100755 --- a/controllers/OrderProductController.php +++ b/controllers/OrderProductController.php @@ -3,6 +3,7 @@ namespace artbox\order\controllers; use artbox\order\models\OrderProductSearch; + use common\models\Variant; use Yii; use artbox\order\models\OrderProduct; use yii\helpers\Html; @@ -10,6 +11,7 @@ use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\filters\AccessControl; + use yii\web\Response; /** * OrderProductController implements the CRUD actions for OrderProduct model. @@ -173,31 +175,47 @@ } } - public function actionAdd(int $variant_id, int $count = 1) + public function actionAdd($variant_id, $count) { - - $result = '
-
'; - - $result .= '
-
'; - $result .= Html::textInput( - 'Product[11994]', - 1, - [ - 'class' => 'form-control', - ] - ); - $result .= '
-
'; - $result .= Html::a( - '', - '#', - [ - 'class' => 'btn btn-primary', - ] - ); - $result .= '
-
'; + \Yii::$app->response->format = Response::FORMAT_JSON; + + /** + * @var Variant $variant + */ + $variant = Variant::find() + ->where([ 'id' => $variant_id ]) + ->with('product.lang') + ->one(); + + if ($variant) { + $result = '
'; + $result .= $variant->product->lang->title . ' (' . $variant->sku . ')'; + $result .= '
'; + $result .= Html::input( + 'number', + 'Product[' . $variant->id . ']', + empty($count) ? 1 : $count, + [ + 'class' => 'form-control count-input', + ] + ); + $result .= '
'; + $result .= Html::a( + '', + '#', + [ + 'class' => 'btn btn-primary delete-product-row', + ] + ); + $result .= '
'; + } else { + $result = ''; + } + + return [ + 'row' => $result, + 'id' => empty($variant) ? 0 : $variant->id, + 'count' => empty($count) ? 1 : intval($count), + ]; } } diff --git a/views/order/_form.php b/views/order/_form.php index fc8a815..69e4d30 100755 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -30,10 +30,23 @@ $(document).on('click', '#create-add', function(e) { count: count }, success: function(data) { - console.log(data); + if (data.id != 0) { + var row = $('#products #' + data.id); + if (row.length !== 0) { + row.find('.count-input').val(function(i, oldVal) { + return parseInt(oldVal, 10) + data.count; + }); + } else { + $('#products').append(data.row); + } + } } }); }); +$(document).on('click', '.delete-product-row', function(e) { + e.preventDefault(); + $(this).parent().parent().remove(); +}) JS; $this->registerJs($js, View::POS_READY); -- libgit2 0.21.4