Commit 2f5c8d1163900a70aca795cf6c8c266361902480
1 parent
879394e3
add variantSku
Showing
2 changed files
with
125 additions
and
129 deletions
Show diff stats
helpers/FilterHelper.php
@@ -111,66 +111,40 @@ | @@ -111,66 +111,40 @@ | ||
111 | 111 | ||
112 | 112 | ||
113 | 113 | ||
114 | - | ||
115 | - | ||
116 | - public static function setNewQueryParams(ActiveQuery $query, array $params) | ||
117 | - { | ||
118 | - $last_query = null; | ||
119 | - foreach ($params as $key => $param) { | ||
120 | - switch ($key) { | ||
121 | - case 'special': | ||
122 | - self::filterSpecial($param, $query); | ||
123 | - break; | ||
124 | - case 'brands': | ||
125 | - self::filterBrands($param, $query); | ||
126 | - break; | ||
127 | - case 'keywords': | ||
128 | - self::filterKeywords($param, $query); | ||
129 | - break; | ||
130 | - case 'prices': | ||
131 | - self::filterPrices($param, $query); | ||
132 | - break; | ||
133 | - default: | ||
134 | - self::filterNewOptions($key, $param, $query); | ||
135 | - break; | ||
136 | - } | ||
137 | - } | ||
138 | - } | ||
139 | - | ||
140 | - | ||
141 | /** | 114 | /** |
142 | - * @param $key | ||
143 | - * @param array $param | ||
144 | - * @param ActiveQuery $query | 115 | + * select options for product variants with selected category |
116 | + * | ||
117 | + * @param integer $categoryId | ||
118 | + * @param integer $langId | ||
119 | + * @return mixed | ||
145 | */ | 120 | */ |
146 | - private static function filterNewOptions($key, array $param, &$query) | ||
147 | - { | ||
148 | - $query->andWhere( | ||
149 | - 'product.id IN ( | ||
150 | - SELECT DISTINCT products | ||
151 | - FROM ( | ||
152 | - SELECT id AS products FROM product WHERE id IN( | ||
153 | - SELECT product_id FROM product_option | ||
154 | - INNER JOIN tax_option ON tax_option.id = product_option.option_id | ||
155 | - INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id | ||
156 | - INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id | ||
157 | - INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id | ||
158 | - WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\')) | ||
159 | - OR id IN ( | ||
160 | - (SELECT product_id AS products | ||
161 | - FROM product_variant_option | ||
162 | - INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.id | ||
163 | - INNER JOIN tax_option ON tax_option.id = product_variant_option.option_id | ||
164 | - INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id | ||
165 | - INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id | ||
166 | - INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id | ||
167 | - WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\')) | ||
168 | - ) | ||
169 | - ) AS table_name | ||
170 | - )' | ||
171 | - ); | ||
172 | - | ||
173 | - | 121 | + public static function getProductVariantOptions($categoryId,$langId){ |
122 | + $result = \Yii::$app->db->cache(function () use($categoryId,$langId) { | ||
123 | + return ( new Query() )->distinct()->select('tax_group_lang.alias') | ||
124 | + ->from('product_variant_option') | ||
125 | + ->innerJoin( | ||
126 | + 'tax_option', | ||
127 | + 'product_variant_option.option_id = tax_option.id' | ||
128 | + ) | ||
129 | + ->innerJoin( | ||
130 | + 'tax_group', | ||
131 | + 'tax_group.id = tax_option.tax_group_id' | ||
132 | + ) | ||
133 | + ->innerJoin( | ||
134 | + 'tax_group_lang', | ||
135 | + 'tax_group_lang.tax_group_id = tax_group.id' | ||
136 | + ) | ||
137 | + ->innerJoin( | ||
138 | + 'tax_group_to_category', | ||
139 | + 'tax_group_to_category.tax_group_id = tax_group.id' | ||
140 | + ) | ||
141 | + ->where([ | ||
142 | + 'tax_group_lang.language_id' => $langId, | ||
143 | + 'tax_group_to_category.category_id' => $categoryId, | ||
144 | + 'tax_group.is_filter' => true | ||
145 | + ])->all(); | ||
146 | + },60*60*24); | ||
147 | + return ArrayHelper::getColumn($result,'alias'); | ||
174 | } | 148 | } |
175 | 149 | ||
176 | 150 | ||
@@ -179,87 +153,119 @@ | @@ -179,87 +153,119 @@ | ||
179 | * | 153 | * |
180 | * @param ActiveQuery $query | 154 | * @param ActiveQuery $query |
181 | * @param array $params | 155 | * @param array $params |
156 | + * @param integer $categoryId | ||
157 | + * @param integer $langId | ||
182 | */ | 158 | */ |
183 | - public static function setQueryParams(ActiveQuery $query, array $params) | 159 | + public static function setQueryParams(ActiveQuery $query, array $params, $categoryId, $langId) |
184 | { | 160 | { |
185 | $last_query = null; | 161 | $last_query = null; |
162 | + $productVariantOptions = self::getProductVariantOptions($categoryId,$langId); | ||
186 | foreach ($params as $key => $param) { | 163 | foreach ($params as $key => $param) { |
187 | switch ($key) { | 164 | switch ($key) { |
188 | case 'special': | 165 | case 'special': |
166 | + unset($params[$key]); | ||
189 | self::filterSpecial($param, $query); | 167 | self::filterSpecial($param, $query); |
190 | break; | 168 | break; |
191 | case 'brands': | 169 | case 'brands': |
170 | + unset($params[$key]); | ||
192 | self::filterBrands($param, $query); | 171 | self::filterBrands($param, $query); |
193 | break; | 172 | break; |
194 | case 'keywords': | 173 | case 'keywords': |
174 | + unset($params[$key]); | ||
195 | self::filterKeywords($param, $query); | 175 | self::filterKeywords($param, $query); |
196 | break; | 176 | break; |
197 | case 'prices': | 177 | case 'prices': |
178 | + unset($params[$key]); | ||
198 | self::filterPrices($param, $query); | 179 | self::filterPrices($param, $query); |
199 | break; | 180 | break; |
200 | - default: | ||
201 | - $last_query = self::filterOptions($param, $last_query); | ||
202 | - break; | 181 | + |
203 | } | 182 | } |
204 | } | 183 | } |
205 | - // If tax option filters were provided filter query with them | ||
206 | - if (!empty( $last_query )) { | ||
207 | - $query->andWhere([ 'product.id' => $last_query ]); | ||
208 | - } | 184 | + self::filterOptions($params, $productVariantOptions, $query); |
209 | } | 185 | } |
210 | 186 | ||
211 | 187 | ||
212 | - | 188 | + |
213 | /** | 189 | /** |
214 | * Tax Option filter | 190 | * Tax Option filter |
215 | * | 191 | * |
216 | * @param string[] $params | 192 | * @param string[] $params |
217 | - * @param \yii\db\Query|null $last_query | 193 | + * @param string[] $productVariantOptions |
194 | + * @param \yii\db\Query|null $query | ||
218 | * @param bool $in_stock | 195 | * @param bool $in_stock |
219 | * | 196 | * |
220 | * @return \yii\db\Query | 197 | * @return \yii\db\Query |
221 | */ | 198 | */ |
222 | - private static function filterOptions(array $params, Query $last_query = null, bool $in_stock = true): Query | 199 | + private static function filterOptions(array $params,$productVariantOptions, Query &$query = null, bool $in_stock = true) |
223 | { | 200 | { |
224 | - | ||
225 | $variant_query = ( new Query() )->distinct() | 201 | $variant_query = ( new Query() )->distinct() |
226 | - ->select('product_variant.product_id as products') | ||
227 | - ->from('product_variant_option') | ||
228 | - ->innerJoin( | ||
229 | - 'product_variant', | ||
230 | - 'product_variant_option.product_variant_id = product_variant.id' | ||
231 | - ) | ||
232 | - ->innerJoin( | ||
233 | - 'tax_option', | ||
234 | - 'tax_option.id = product_variant_option.option_id' | ||
235 | - ) | ||
236 | - ->innerJoin( | ||
237 | - 'tax_option_lang', | ||
238 | - 'tax_option_lang.tax_option_id = tax_option.id' | ||
239 | - ) | ||
240 | - ->where([ 'tax_option_lang.alias' => $params ]); | 202 | + ->select('product_variant.product_id as products') |
203 | + ->from('product_variant'); | ||
204 | + | ||
205 | + foreach ($params as $key=>$param){ | ||
206 | + if(in_array($key, $productVariantOptions)){ | ||
207 | + unset($params[$key]); | ||
208 | + $product_variant_id = ( new Query() )->distinct()->select('product_variant_option.product_variant_id as id') | ||
209 | + ->from('product_variant_option') | ||
210 | + ->innerJoin( | ||
211 | + 'tax_option', | ||
212 | + 'tax_option.id = product_variant_option.option_id' | ||
213 | + ) | ||
214 | + ->innerJoin( | ||
215 | + 'tax_option_lang', | ||
216 | + 'tax_option_lang.tax_option_id = tax_option.id' | ||
217 | + ) | ||
218 | + ->innerJoin( | ||
219 | + 'tax_group', | ||
220 | + 'tax_group.id = tax_option.tax_group_id' | ||
221 | + ) | ||
222 | + ->innerJoin( | ||
223 | + 'tax_group_lang', | ||
224 | + 'tax_group_lang.tax_group_id = tax_group.id' | ||
225 | + ) | ||
226 | + ->where([ 'tax_group_lang.alias' => $key ]) | ||
227 | + ->andWhere([ 'tax_option_lang.alias' => $param ]); | ||
228 | + $variant_query->andWhere(['product_variant.id'=>$product_variant_id]); | ||
229 | + } | ||
230 | + | ||
231 | + } | ||
232 | + | ||
233 | + | ||
241 | if($in_stock) { | 234 | if($in_stock) { |
242 | $variant_query->andWhere(['!=', 'product_variant.stock', 0]); | 235 | $variant_query->andWhere(['!=', 'product_variant.stock', 0]); |
243 | } | 236 | } |
244 | $product_query = ( new Query() )->distinct() | 237 | $product_query = ( new Query() )->distinct() |
245 | - ->select('product_option.product_id as products') | ||
246 | - ->from('product_option') | ||
247 | - ->innerJoin('tax_option', 'product_option.option_id = tax_option.id') | ||
248 | - ->innerJoin( | ||
249 | - 'tax_option_lang', | ||
250 | - 'tax_option_lang.tax_option_id = tax_option.id' | ||
251 | - ) | ||
252 | - ->where( | ||
253 | - [ 'tax_option_lang.alias' => $params ] | ||
254 | - ) | ||
255 | - ->union($variant_query); | ||
256 | - $query = ( new Query() )->select('products') | ||
257 | - ->from([ 'result_table' => $product_query ]); | ||
258 | - if (!empty( $last_query )) { | ||
259 | - $query->andWhere([ 'product.id' => $last_query ]); | 238 | + ->select('product_option.product_id as products') |
239 | + ->from('product_option') | ||
240 | + ->innerJoin( | ||
241 | + 'tax_option', | ||
242 | + 'tax_option.id = product_option.option_id' | ||
243 | + ) | ||
244 | + ->innerJoin( | ||
245 | + 'tax_option_lang', | ||
246 | + 'tax_option_lang.tax_option_id = tax_option.id' | ||
247 | + ) | ||
248 | + ->innerJoin( | ||
249 | + 'tax_group', | ||
250 | + 'tax_group.id = tax_option.tax_group_id' | ||
251 | + ) | ||
252 | + ->innerJoin( | ||
253 | + 'tax_group_lang', | ||
254 | + 'tax_group_lang.tax_group_id = tax_group.id' | ||
255 | + ); | ||
256 | + foreach ($params as $key=>$param){ | ||
257 | + $product_query | ||
258 | + ->where([ 'tax_group_lang.alias' => $key ]) | ||
259 | + ->andWhere([ 'tax_option_lang.alias' => $param ]); | ||
260 | + | ||
260 | } | 261 | } |
261 | - return $query; | 262 | + |
263 | + $query->andWhere([ 'product.id' => $product_query ]); | ||
264 | + $query->andWhere([ 'product.id' => $variant_query ]); | ||
265 | + | ||
262 | } | 266 | } |
267 | + | ||
268 | + | ||
263 | 269 | ||
264 | /** | 270 | /** |
265 | * Fill $query with special filters (used in Product) | 271 | * Fill $query with special filters (used in Product) |
models/ProductFrontendSearch.php
@@ -4,6 +4,7 @@ | @@ -4,6 +4,7 @@ | ||
4 | 4 | ||
5 | use artweb\artbox\ecommerce\helpers\FilterHelper; | 5 | use artweb\artbox\ecommerce\helpers\FilterHelper; |
6 | use artweb\artbox\ecommerce\models\Category; | 6 | use artweb\artbox\ecommerce\models\Category; |
7 | + use artweb\artbox\language\models\Language; | ||
7 | use yii\base\Model; | 8 | use yii\base\Model; |
8 | use yii\data\ActiveDataProvider; | 9 | use yii\data\ActiveDataProvider; |
9 | use yii\data\ArrayDataProvider; | 10 | use yii\data\ArrayDataProvider; |
@@ -116,23 +117,17 @@ | @@ -116,23 +117,17 @@ | ||
116 | 117 | ||
117 | return $dataProvider; | 118 | return $dataProvider; |
118 | } | 119 | } |
119 | - | ||
120 | - public function getSearchQuery($category = null, $params = [], $in_stock = true) | 120 | + |
121 | + /** | ||
122 | + * @param Category $category | ||
123 | + * @param array $params | ||
124 | + * @param bool $in_stock | ||
125 | + * @return mixed | ||
126 | + */ | ||
127 | + public function getSearchQuery($category, $params = [], $in_stock = true) | ||
121 | { | 128 | { |
122 | - | ||
123 | - if (!empty( $category )) { | ||
124 | - /** @var ActiveQuery $query */ | ||
125 | - /**@var Category $category * */ | ||
126 | - $query = $category->getProducts(); | ||
127 | - | ||
128 | - } else { | ||
129 | - $query = Product::find() | ||
130 | - ->joinWith( | ||
131 | - [ | ||
132 | - 'category.lang', | ||
133 | - ] | ||
134 | - ); | ||
135 | - } | 129 | + |
130 | + $query = $category->getProducts(); | ||
136 | 131 | ||
137 | $query->select([ 'product.*' ]); | 132 | $query->select([ 'product.*' ]); |
138 | $query->joinWith( | 133 | $query->joinWith( |
@@ -175,9 +170,9 @@ | @@ -175,9 +170,9 @@ | ||
175 | 'product_variant.price', | 170 | 'product_variant.price', |
176 | ] | 171 | ] |
177 | ); | 172 | ); |
178 | - | ||
179 | - FilterHelper::setQueryParams($query, $params); | ||
180 | - | 173 | + |
174 | + $lang = Language::getCurrent(); | ||
175 | + FilterHelper::setQueryParams($query, $params, $category->id, $lang->id); | ||
181 | return $query; | 176 | return $query; |
182 | } | 177 | } |
183 | 178 | ||
@@ -189,21 +184,16 @@ | @@ -189,21 +184,16 @@ | ||
189 | 184 | ||
190 | public function priceLimits($category = null, $params = []) | 185 | public function priceLimits($category = null, $params = []) |
191 | { | 186 | { |
192 | - if (!empty( $category )) { | ||
193 | - /** @var ActiveQuery $query */ | ||
194 | - // $query = $category->getRelations('product_categories'); | ||
195 | - $query = $category->getProducts(); | ||
196 | - } else { | ||
197 | - $query = Product::find(); | ||
198 | - } | 187 | + $query = $category->getProducts(); |
199 | 188 | ||
200 | $query->select(['MAX('.ProductVariant::tableName() . '.price) as max', 'MIN('.ProductVariant::tableName() . '.price) as min']); | 189 | $query->select(['MAX('.ProductVariant::tableName() . '.price) as max', 'MIN('.ProductVariant::tableName() . '.price) as min']); |
201 | $query->joinWith('variant'); | 190 | $query->joinWith('variant'); |
202 | 191 | ||
203 | // Price filter fix | 192 | // Price filter fix |
204 | unset( $params[ 'prices' ] ); | 193 | unset( $params[ 'prices' ] ); |
205 | - | ||
206 | - FilterHelper::setQueryParams($query, $params); | 194 | + |
195 | + $lang = Language::getCurrent(); | ||
196 | + FilterHelper::setQueryParams($query, $params, $category->id, $lang->id); | ||
207 | $query->andWhere( | 197 | $query->andWhere( |
208 | [ | 198 | [ |
209 | '>=', | 199 | '>=', |