/** * @inheritdoc * @return string * @throws InvalidConfigException * @throws \yii\web\ServerErrorHttpException */ public function run() { parent::run(); if (null === $this->category_group_id) { throw new InvalidConfigException("ProductsWidget.category_group_id should be set"); } $products = Product::filteredProducts($this->category_group_id, $this->values_by_property_id, $this->selected_category_id, $this->force_sorting, $this->limit, false, $this->force_limit, $this->additional_filters); return $this->render($this->viewFile, ['products' => $products, 'itemView' => $this->itemView, 'values_by_property_id' => $this->values_by_property_id, 'category_group_id' => $this->category_group_id, 'selected_category_id' => $this->selected_category_id]); }
/** * Products listing by category with filtration support. * * @return string * @throws \Exception * @throws NotFoundHttpException * @throws ServerErrorHttpException */ public function actionList() { $request = Yii::$app->request; if (null === $request->get('category_group_id')) { throw new NotFoundHttpException(); } if (null === ($object = Object::getForClass(Product::className()))) { throw new ServerErrorHttpException('Object not found.'); } $category_group_id = intval($request->get('category_group_id', 0)); $title_append = $request->get('title_append', ''); if (!empty($title_append)) { $title_append = is_array($title_append) ? implode(' ', $title_append) : $title_append; unset($_GET['title_append']); } $title_prepend = $request->get("title_prepend", ""); if (!empty($title_prepend)) { $title_prepend = is_array($title_prepend) ? implode(" ", $title_prepend) : $title_prepend; unset($_GET["title_prepend"]); } $values_by_property_id = $request->get('properties', []); if (!is_array($values_by_property_id)) { $values_by_property_id = [$values_by_property_id]; } if (Yii::$app->request->isPost && isset($_POST['properties'])) { if (is_array($_POST['properties'])) { foreach ($_POST['properties'] as $key => $value) { if (isset($values_by_property_id[$key])) { $values_by_property_id[$key] = array_unique(ArrayHelper::merge($values_by_property_id[$key], $value)); } else { $values_by_property_id[$key] = array_unique($value); } } } } $selected_category_ids = $request->get('categories', []); if (!is_array($selected_category_ids)) { $selected_category_ids = [$selected_category_ids]; } if (null !== ($selected_category_id = $request->get('last_category_id'))) { $selected_category_id = intval($selected_category_id); } $result = Product::filteredProducts($category_group_id, $values_by_property_id, $selected_category_id, false, null, true, false); /** @var Pagination $pages */ $pages = $result['pages']; if (Yii::$app->response->is_prefiltered_page) { $pages->route = '/' . Yii::$app->request->pathInfo; $pages->params = []; } $allSorts = $result['allSorts']; $products = $result['products']; // throw 404 if we are at filtered page without any products if (!Yii::$app->request->isAjax && !empty($values_by_property_id) && empty($products)) { throw new EmptyFilterHttpException(); } if (null !== ($selected_category = $selected_category_id)) { if ($selected_category_id > 0) { if (null !== ($selected_category = Category::findById($selected_category_id, null))) { if (!empty($selected_category->meta_description)) { $this->view->registerMetaTag(['name' => 'description', 'content' => ContentBlockHelper::compileContentString($selected_category->meta_description, Product::className() . ":{$selected_category->id}:meta_description", new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(ContentBlock::className()), ActiveRecordHelper::getCommonTag(Category::className())]]))], 'meta_description'); } $this->view->title = $selected_category->title; } } } if (is_null($selected_category) || !$selected_category->active) { throw new NotFoundHttpException(); } if (!empty($title_append)) { $this->view->title .= " " . $title_append; } if (!empty($title_prepend)) { $this->view->title = "{$title_prepend} {$this->view->title}"; } $this->view->blocks['h1'] = $selected_category->h1; $this->view->blocks['announce'] = $selected_category->announce; $this->view->blocks['content'] = $selected_category->content; $this->loadDynamicContent($object->id, 'shop/product/list', $request->get()); $params = ['model' => $selected_category, 'selected_category' => $selected_category, 'selected_category_id' => $selected_category_id, 'selected_category_ids' => $selected_category_ids, 'values_by_property_id' => $values_by_property_id, 'products' => $products, 'object' => $object, 'category_group_id' => $category_group_id, 'pages' => $pages, 'title_append' => $title_append, 'selections' => $request->get(), 'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, null, $values_by_property_id), 'allSorts' => $allSorts]; $viewFile = $this->computeViewFile($selected_category, 'list'); if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; $content = $this->renderAjax($viewFile, $params); $filters = ''; $activeWidgets = ThemeActiveWidgets::getActiveWidgets(); foreach ($activeWidgets as $activeWidget) { if ($activeWidget->widget->widget == Widget::className()) { /** @var ThemeWidgets $widgetModel */ $widgetModel = $activeWidget->widget; /** @var BaseWidget $widgetClassName */ $widgetClassName = $widgetModel->widget; $widgetConfiguration = Json::decode($widgetModel->configuration_json, true); if (!is_array($widgetConfiguration)) { $widgetConfiguration = []; } $activeWidgetConfiguration = Json::decode($activeWidget->configuration_json, true); if (!is_array($activeWidgetConfiguration)) { $activeWidgetConfiguration = []; } $config = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration); $config['themeWidgetModel'] = $widgetModel; $config['partRow'] = $activeWidget->part; $config['activeWidget'] = $activeWidget; $filters = $widgetClassName::widget($config); } } return ['content' => $content, 'filters' => $filters, 'title' => $this->view->title, 'url' => Url::to(['/shop/product/list', 'last_category_id' => $selected_category_id, 'properties' => $values_by_property_id])]; } else { return $this->render($viewFile, $params); } }