request->get('category'); $filter = Yii::$app->request->get('filters', [ ]); $filter_check = $filter; if(empty( $category->category_id ) && empty( $word )) { return $this->render('catalog'); } ProductHelper::addLastCategory($category->category_id); $params = [ ]; $optionsList = ArrayHelper::getColumn(TaxGroup::find() ->where([ 'is_filter' => 'TRUE' ]) ->all(), 'alias'); if(!empty( $filter[ 'brands' ] )) { unset( $filter_check[ 'brands' ] ); $brands = Brand::find() ->select('brand_id') ->where([ 'in', 'alias', $filter[ 'brands' ], ]) ->all(); $params[ 'brands' ] = [ ]; foreach($brands as $brand) { $params[ 'brands' ][] = $brand->brand_id; } } if(!empty( $filter[ 'special' ] )) { unset( $filter_check[ 'special' ] ); if(!is_array($filter[ 'special' ])) { $filter[ 'special' ] = [ $filter[ 'special' ] ]; } if(in_array('new', $filter[ 'special' ])) { $params[ 'special' ][ 'is_new' ] = true; } if(in_array('top', $filter[ 'special' ])) { $params[ 'special' ][ 'is_top' ] = true; } if(in_array('promo', $filter[ 'special' ])) { $params[ 'special' ][ 'akciya' ] = true; } } if(!empty( $filter[ 'prices' ] )) { unset( $filter_check[ 'prices' ] ); $params[ 'prices' ] = $filter[ 'prices' ]; } foreach($optionsList as $optionList) { if(isset( $filter[ $optionList ] )) { unset( $filter_check[ $optionList ] ); $params[ $optionList ] = $filter[ $optionList ]; } } if(!empty( $filter_check )) { $filter = array_diff_key($filter, $filter_check); Yii::$app->response->redirect([ 'catalog/category', 'category' => $category, 'filters' => $filter, ], 301); } $productModel = new ProductFrontendSearch(); //$productQuery = $productModel->getSearchQuery($category, $params); $productProvider = $productModel->search($category, $params); $brandModel = new BrandSearch(); $brands = $brandModel->getBrands($category, $params) ->all(); $groups = $category->getActiveFilters()->all(); $groups = ArrayHelper::index($groups, null, 'name'); $priceLimits = $productModel->priceLimits($category, $params); /* * Greedy search for comments and rating */ $query = $productProvider->query; $query->with([ 'variant', 'comments', 'averageRating', ]); /* * End of greedy search for rating and comments */ $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 15, ], ]); $products = Product::find()->joinWith('categories') ->where([ 'category.category_id' => $category->getChildrenByDepth(2)->column(), ]); $productsProvider = new ActiveDataProvider([ 'query' => $products, 'pagination' => [ 'pageSize' => 15, ], ]); $stockProgram = Category::find() ->where([ 'stock_program' => true, 'parent_id' => $category->category_id, ])->all(); $onOrder = Category::find() ->where([ 'on_order' => true, 'parent_id' => $category->category_id, ])->all(); return $this->render('products', [ 'category' => $category, 'brandModel' => $brandModel, 'dataProvider' => $dataProvider, 'brands' => $brands, 'filter' => $filter, 'params' => $params, 'productModel' => $productModel, 'productsProvider' => $dataProvider, 'groups' => $groups, 'priceLimits' => $priceLimits, 'stockProgram' => $stockProgram, 'onOrder' => $onOrder, ]); } /** * @param string $product * @param string $variant * @return string */ public function actionProduct($product, $variant) { $product = Product::find() ->joinWith([ 'variants' => function($query) { $query->indexBy('sku'); }, ], true, 'INNER JOIN') ->where([ 'product.alias' => $product, 'product_variant.sku' => $variant, ])->with([ 'category.parent', 'options.group', ])->one(); $attributes = []; if(!empty($product->options)) { foreach($product->options as $option) { if(!$option->group->is_menu) { continue; } $attributes[ $option->group->name ][] = $option->value; } } $variant = $product->variants[$variant]; $variants = $product->variants; return $this->render('view', [ 'variants' => $variants, 'product' => $product, 'variant' => $variant, 'attributes' => $attributes, ]); } public function actionBrands() { $dataProvider = new ActiveDataProvider([ 'query' => Brand::find()->orderBy('name'), 'pagination' => [ 'pageSize' => -1, ] ]); return $this->render('brands', [ 'dataProvider' => $dataProvider, ]); } public function actionBrand($brand) { $brand = BrandSearch::findByAlias($brand); $params = [ 'brands' => $brand->brand_id, ]; $productModel = new ProductFrontendSearch(); $productProvider = $productModel->search(null, $params); $priceLimits = $productModel->priceLimits(null, $params); return $this->render('brand', [ 'productModel' => $productModel, 'productProvider' => $productProvider, 'brand' => $brand, 'priceLimits' => $priceLimits, ]); } }