Commit d08b44b4b1e038a4b84e9c1c6834eee6e6a9b5fc
1 parent
b2a90d20
-Find by sku
Showing
2 changed files
with
40 additions
and
3 deletions
Show diff stats
frontend/controllers/SiteController.php
... | ... | @@ -6,6 +6,8 @@ use common\models\Blog; |
6 | 6 | use common\models\Slider; |
7 | 7 | use common\modules\product\models\Category; |
8 | 8 | use common\models\Page; |
9 | +use common\modules\product\models\Product; | |
10 | +use common\modules\product\models\ProductVariant; | |
9 | 11 | use Yii; |
10 | 12 | use yii\base\InvalidParamException; |
11 | 13 | use yii\web\BadRequestHttpException; |
... | ... | @@ -71,7 +73,18 @@ class SiteController extends Controller |
71 | 73 | ], |
72 | 74 | ]; |
73 | 75 | } |
74 | - | |
76 | + | |
77 | + public function beforeAction($action) | |
78 | + { | |
79 | + if ($this->action->id == 'sku') | |
80 | + { | |
81 | + $this->enableCsrfValidation = false; | |
82 | + } | |
83 | + | |
84 | + return parent::beforeAction($action); | |
85 | + | |
86 | + } | |
87 | + | |
75 | 88 | /** |
76 | 89 | * Displays homepage. |
77 | 90 | * |
... | ... | @@ -251,4 +264,27 @@ class SiteController extends Controller |
251 | 264 | 'model' => $model, |
252 | 265 | ]); |
253 | 266 | } |
267 | + | |
268 | + public function actionSku() | |
269 | + { | |
270 | + $sku = Yii::$app->request->post('sku'); | |
271 | + if(empty($sku)) throw new NotFoundHttpException('Не задан артикул'); | |
272 | + | |
273 | + $variant = ProductVariant::find() | |
274 | + ->where([ | |
275 | + 'sku' => $sku, | |
276 | + ]) | |
277 | + ->with('product')->one(); | |
278 | + if(empty($variant)) | |
279 | + { | |
280 | + throw new NotFoundHttpException('Артикул не найден'); | |
281 | + } else | |
282 | + { | |
283 | + return $this->redirect([ | |
284 | + 'catalog/product', | |
285 | + 'product' => $variant->product->alias, | |
286 | + 'variant' => $variant->sku, | |
287 | + ]); | |
288 | + } | |
289 | + } | |
254 | 290 | } | ... | ... |
frontend/views/catalog/products.php
... | ... | @@ -57,8 +57,9 @@ $this->params['breadcrumbs'][] = $this->title; |
57 | 57 | </div> |
58 | 58 | </div> |
59 | 59 | <div class="title">Фильтры</div> |
60 | - <div><input type="text" placeholder="Артикул"></div> | |
61 | - | |
60 | + <form method="post" action="/site/sku"> | |
61 | + <div><input name="sku" type="text" placeholder="Артикул"></div> | |
62 | + </form> | |
62 | 63 | <?= FilterWidget::widget([ |
63 | 64 | 'category'=>$category, |
64 | 65 | 'groups'=> $groups, | ... | ... |