diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index cb7ca91..a9bd1ef 100755 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -6,6 +6,8 @@ use common\models\Blog; use common\models\Slider; use common\modules\product\models\Category; use common\models\Page; +use common\modules\product\models\Product; +use common\modules\product\models\ProductVariant; use Yii; use yii\base\InvalidParamException; use yii\web\BadRequestHttpException; @@ -71,7 +73,18 @@ class SiteController extends Controller ], ]; } - + + public function beforeAction($action) + { + if ($this->action->id == 'sku') + { + $this->enableCsrfValidation = false; + } + + return parent::beforeAction($action); + + } + /** * Displays homepage. * @@ -251,4 +264,27 @@ class SiteController extends Controller 'model' => $model, ]); } + + public function actionSku() + { + $sku = Yii::$app->request->post('sku'); + if(empty($sku)) throw new NotFoundHttpException('Не задан артикул'); + + $variant = ProductVariant::find() + ->where([ + 'sku' => $sku, + ]) + ->with('product')->one(); + if(empty($variant)) + { + throw new NotFoundHttpException('Артикул не найден'); + } else + { + return $this->redirect([ + 'catalog/product', + 'product' => $variant->product->alias, + 'variant' => $variant->sku, + ]); + } + } } diff --git a/frontend/views/catalog/products.php b/frontend/views/catalog/products.php index 15593eb..ec71adb 100755 --- a/frontend/views/catalog/products.php +++ b/frontend/views/catalog/products.php @@ -57,8 +57,9 @@ $this->params['breadcrumbs'][] = $this->title;