Commit bf70a38094efddefbee39b4e724acfcd9cab708d

Authored by Yarik
1 parent 8a5570d6

Not in stock

Showing 1 changed file with 57 additions and 44 deletions   Show diff stats
models/ProductFrontendSearch.php
... ... @@ -67,25 +67,25 @@
67 67 $sort = new Sort(
68 68 [
69 69 'attributes' => [
70   - 'name_asc' => [
  70 + 'name_asc' => [
71 71 'asc' => [ 'product_lang.title' => SORT_ASC ],
72 72 'desc' => [ 'product_lang.title' => SORT_ASC ],
73 73 'default' => SORT_ASC,
74 74 'label' => 'имени от А до Я',
75 75 ],
76   - 'name_desc' => [
  76 + 'name_desc' => [
77 77 'asc' => [ 'product_lang.title' => SORT_DESC ],
78 78 'desc' => [ 'product_lang.title' => SORT_DESC ],
79 79 'default' => SORT_DESC,
80 80 'label' => 'имени от Я до А',
81 81 ],
82   - 'price_asc' => [
  82 + 'price_asc' => [
83 83 'asc' => [ 'product_variant.price' => SORT_ASC ],
84 84 'desc' => [ 'product_variant.price' => SORT_ASC ],
85 85 'default' => SORT_ASC,
86 86 'label' => 'по цене по возрастанию',
87 87 ],
88   - 'price_desc' => [
  88 + 'price_desc' => [
89 89 'asc' => [ 'product_variant.price' => SORT_DESC ],
90 90 'desc' => [ 'product_variant.price' => SORT_DESC ],
91 91 'default' => SORT_DESC,
... ... @@ -94,17 +94,18 @@
94 94 ],
95 95 ]
96 96 );
97   -
  97 +
98 98 $dataProvider = new ActiveDataProvider(
99 99 [
100   - 'query' => $this->getSearchQuery($category, $params, $in_stock)
  100 + 'query' => $this->getSearchQuery($category, $params, $in_stock)
101 101 ->with('variant', 'videos')
102   - ->groupBy(
103   - [
104   - 'product_lang.title',
105   - 'product_variant.price',
106   - 'product.id'
107   - ]),
  102 + ->groupBy(
  103 + [
  104 + 'product_lang.title',
  105 + 'product_variant.price',
  106 + 'product.id',
  107 + ]
  108 + ),
108 109 'pagination' => [
109 110 'pageSize' => 12,
110 111 ],
... ... @@ -125,7 +126,12 @@
125 126  
126 127 } else {
127 128 $query = Product::find()
128   - ->joinWith(['category','category.lang']);
  129 + ->joinWith(
  130 + [
  131 + 'category',
  132 + 'category.lang',
  133 + ]
  134 + );
129 135 }
130 136  
131 137 $query->select([ 'product.*' ]);
... ... @@ -135,18 +141,33 @@
135 141 'brand.lang',
136 142 'options',
137 143 ]
138   - )
139   - ->innerJoinWith(
140   - [
141   - 'enabledVariants' => function ($query) {
142   - /**
143   - * @var ActiveQuery $query
144   - */
145   - $query->joinWith('lang')
146   - ->with('images');
147   - },
148   - ]
149   - );
  144 + );
  145 +
  146 + if ($in_stock) {
  147 + $query->innerJoinWith(
  148 + [
  149 + 'enabledVariants' => function ($query) {
  150 + /**
  151 + * @var ActiveQuery $query
  152 + */
  153 + $query->joinWith('lang')
  154 + ->with('images');
  155 + },
  156 + ]
  157 + );
  158 + } else {
  159 + $query->innerJoinWith(
  160 + [
  161 + 'variants' => function ($query) {
  162 + /**
  163 + * @var ActiveQuery $query
  164 + */
  165 + $query->joinWith('lang')
  166 + ->with('images');
  167 + },
  168 + ]
  169 + );
  170 + }
150 171  
151 172 $query->groupBy(
152 173 [
... ... @@ -156,16 +177,7 @@
156 177 );
157 178  
158 179 FilterHelper::setQueryParams($query, $params);
159   - if ($in_stock) {
160   - $query->andWhere(
161   - [
162   - '>=',
163   - ProductVariant::tableName() . '.stock',
164   - 1,
165   - ]
166   - );
167   - }
168   -
  180 +
169 181 return $query;
170 182 }
171 183  
... ... @@ -174,20 +186,21 @@
174 186 *
175 187 * @return array
176 188 */
177   -
178   - public function priceLimits($category = null, $params = []) {
179   - if (!empty($category)) {
  189 +
  190 + public function priceLimits($category = null, $params = [])
  191 + {
  192 + if (!empty( $category )) {
180 193 /** @var ActiveQuery $query */
181   -// $query = $category->getRelations('product_categories');
  194 + // $query = $category->getRelations('product_categories');
182 195 $query = $category->getProducts();
183 196 } else {
184 197 $query = Product::find();
185 198 }
186 199 $query->joinWith('variant');
187   -
  200 +
188 201 // Price filter fix
189   - unset($params['prices']);
190   -
  202 + unset( $params[ 'prices' ] );
  203 +
191 204 FilterHelper::setQueryParams($query, $params);
192 205 $query->andWhere(
193 206 [
... ... @@ -197,8 +210,8 @@
197 210 ]
198 211 );
199 212 return [
200   - 'min' => $query->min(ProductVariant::tableName() .'.price'),
201   - 'max' => $query->max(ProductVariant::tableName() .'.price'),
  213 + 'min' => $query->min(ProductVariant::tableName() . '.price'),
  214 + 'max' => $query->max(ProductVariant::tableName() . '.price'),
202 215 ];
203 216 }
204 217 }
205 218 \ No newline at end of file
... ...