From bf70a38094efddefbee39b4e724acfcd9cab708d Mon Sep 17 00:00:00 2001 From: yarik Date: Thu, 1 Dec 2016 23:41:03 +0200 Subject: [PATCH] Not in stock --- models/ProductFrontendSearch.php | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/models/ProductFrontendSearch.php b/models/ProductFrontendSearch.php index 7a83265..578ae73 100755 --- a/models/ProductFrontendSearch.php +++ b/models/ProductFrontendSearch.php @@ -67,25 +67,25 @@ $sort = new Sort( [ 'attributes' => [ - 'name_asc' => [ + 'name_asc' => [ 'asc' => [ 'product_lang.title' => SORT_ASC ], 'desc' => [ 'product_lang.title' => SORT_ASC ], 'default' => SORT_ASC, 'label' => 'имени от А до Я', ], - 'name_desc' => [ + 'name_desc' => [ 'asc' => [ 'product_lang.title' => SORT_DESC ], 'desc' => [ 'product_lang.title' => SORT_DESC ], 'default' => SORT_DESC, 'label' => 'имени от Я до А', ], - 'price_asc' => [ + 'price_asc' => [ 'asc' => [ 'product_variant.price' => SORT_ASC ], 'desc' => [ 'product_variant.price' => SORT_ASC ], 'default' => SORT_ASC, 'label' => 'по цене по возрастанию', ], - 'price_desc' => [ + 'price_desc' => [ 'asc' => [ 'product_variant.price' => SORT_DESC ], 'desc' => [ 'product_variant.price' => SORT_DESC ], 'default' => SORT_DESC, @@ -94,17 +94,18 @@ ], ] ); - + $dataProvider = new ActiveDataProvider( [ - 'query' => $this->getSearchQuery($category, $params, $in_stock) + 'query' => $this->getSearchQuery($category, $params, $in_stock) ->with('variant', 'videos') - ->groupBy( - [ - 'product_lang.title', - 'product_variant.price', - 'product.id' - ]), + ->groupBy( + [ + 'product_lang.title', + 'product_variant.price', + 'product.id', + ] + ), 'pagination' => [ 'pageSize' => 12, ], @@ -125,7 +126,12 @@ } else { $query = Product::find() - ->joinWith(['category','category.lang']); + ->joinWith( + [ + 'category', + 'category.lang', + ] + ); } $query->select([ 'product.*' ]); @@ -135,18 +141,33 @@ 'brand.lang', 'options', ] - ) - ->innerJoinWith( - [ - 'enabledVariants' => function ($query) { - /** - * @var ActiveQuery $query - */ - $query->joinWith('lang') - ->with('images'); - }, - ] - ); + ); + + if ($in_stock) { + $query->innerJoinWith( + [ + 'enabledVariants' => function ($query) { + /** + * @var ActiveQuery $query + */ + $query->joinWith('lang') + ->with('images'); + }, + ] + ); + } else { + $query->innerJoinWith( + [ + 'variants' => function ($query) { + /** + * @var ActiveQuery $query + */ + $query->joinWith('lang') + ->with('images'); + }, + ] + ); + } $query->groupBy( [ @@ -156,16 +177,7 @@ ); FilterHelper::setQueryParams($query, $params); - if ($in_stock) { - $query->andWhere( - [ - '>=', - ProductVariant::tableName() . '.stock', - 1, - ] - ); - } - + return $query; } @@ -174,20 +186,21 @@ * * @return array */ - - public function priceLimits($category = null, $params = []) { - if (!empty($category)) { + + public function priceLimits($category = null, $params = []) + { + if (!empty( $category )) { /** @var ActiveQuery $query */ -// $query = $category->getRelations('product_categories'); + // $query = $category->getRelations('product_categories'); $query = $category->getProducts(); } else { $query = Product::find(); } $query->joinWith('variant'); - + // Price filter fix - unset($params['prices']); - + unset( $params[ 'prices' ] ); + FilterHelper::setQueryParams($query, $params); $query->andWhere( [ @@ -197,8 +210,8 @@ ] ); return [ - 'min' => $query->min(ProductVariant::tableName() .'.price'), - 'max' => $query->max(ProductVariant::tableName() .'.price'), + 'min' => $query->min(ProductVariant::tableName() . '.price'), + 'max' => $query->max(ProductVariant::tableName() . '.price'), ]; } } \ No newline at end of file -- libgit2 0.21.4