Commit cb55aee82f5837e976b29eec4d63505317c6d88a

Authored by Administrator
1 parent ad348101

add variantSku

Showing 1 changed file with 90 additions and 67 deletions   Show diff stats
helpers/FilterHelper.php
... ... @@ -111,6 +111,69 @@
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 + /**
  142 + * @param $key
  143 + * @param array $param
  144 + * @param ActiveQuery $query
  145 + */
  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 +
  174 + }
  175 +
  176 +
114 177 /**
115 178 * Fill query with filter conditions
116 179 *
... ... @@ -123,24 +186,22 @@
123 186 foreach ($params as $key => $param) {
124 187 switch ($key) {
125 188 case 'special':
126   - unset($params[$key]);
127 189 self::filterSpecial($param, $query);
128 190 break;
129 191 case 'brands':
130   - unset($params[$key]);
131 192 self::filterBrands($param, $query);
132 193 break;
133 194 case 'keywords':
134   - unset($params[$key]);
135 195 self::filterKeywords($param, $query);
136 196 break;
137 197 case 'prices':
138   - unset($params[$key]);
139 198 self::filterPrices($param, $query);
140 199 break;
  200 + default:
  201 + $last_query = self::filterOptions($param, $last_query);
  202 + break;
141 203 }
142 204 }
143   - $last_query = self::filterOptions($params, $last_query);
144 205 // If tax option filters were provided filter query with them
145 206 if (!empty( $last_query )) {
146 207 $query->andWhere([ 'product.id' => $last_query ]);
... ... @@ -148,84 +209,46 @@
148 209 }
149 210  
150 211  
151   -
152   -
  212 +
153 213 /**
154 214 * Tax Option filter
155 215 *
156 216 * @param string[] $params
157 217 * @param \yii\db\Query|null $last_query
158   - * @param bool $in_stock
159 218 *
160   - * @return \yii\db\Query
  219 + * @return Query
161 220 */
162   - private static function filterOptions(array $params, Query $last_query = null, bool $in_stock = true): Query
  221 + private static function filterOptions(array $params, Query $last_query = null): Query
163 222 {
  223 +
164 224 $variant_query = ( new Query() )->distinct()
165 225 ->select('product_variant.product_id as products')
166 226 ->from('product_variant_option')
167 227 ->innerJoin(
168 228 'product_variant',
169 229 'product_variant_option.product_variant_id = product_variant.id'
170   - );
171   -
172   - foreach ($params as $key=>$param){
173   - $product_variant_id = ( new Query() )->distinct()->select('product_variant_option.product_variant_id as id')
174   - ->from('product_variant_option')
175   - ->innerJoin(
176   - 'tax_option',
177   - 'tax_option.id = product_variant_option.option_id'
178   - )
179   - ->innerJoin(
180   - 'tax_option_lang',
181   - 'tax_option_lang.tax_option_id = tax_option.id'
182   - )
183   - ->innerJoin(
184   - 'tax_group',
185   - 'tax_group.id = tax_option.tax_group_id'
186   - )
187   - ->innerJoin(
188   - 'tax_group_lang',
189   - 'tax_group_lang.tax_group_id = tax_group.id'
190   - )
191   - ->where([ 'tax_group_lang.alias' => $key ])
192   - ->andWhere([ 'tax_option_lang.alias' => $param ]);
193   - $variant_query->andWhere(['product_variant_option.product_variant_id'=>$product_variant_id]);
194   -
195   - }
196   -
197   -
198   - if($in_stock) {
199   - $variant_query->andWhere(['!=', 'product_variant.stock', 0]);
200   - }
  230 + )
  231 + ->innerJoin(
  232 + 'tax_option',
  233 + 'tax_option.id = product_variant_option.option_id'
  234 + )
  235 + ->innerJoin(
  236 + 'tax_option_lang',
  237 + 'tax_option_lang.tax_option_id = tax_option.id'
  238 + )
  239 + ->where([ 'tax_option_lang.alias' => $params ]);
201 240 $product_query = ( new Query() )->distinct()
202 241 ->select('product_option.product_id as products')
203   - ->from('product_option');
204   - foreach ($params as $key=>$param){
205   - $product_id = ( new Query() )->distinct()->select('product_option.product_id as id')
206   - ->from('product_option')
207   - ->innerJoin(
208   - 'tax_option',
209   - 'tax_option.id = product_option.option_id'
210   - )
211   - ->innerJoin(
212   - 'tax_option_lang',
213   - 'tax_option_lang.tax_option_id = tax_option.id'
214   - )
215   - ->innerJoin(
216   - 'tax_group',
217   - 'tax_group.id = tax_option.tax_group_id'
218   - )
219   - ->innerJoin(
220   - 'tax_group_lang',
221   - 'tax_group_lang.tax_group_id = tax_group.id'
222   - )
223   - ->where([ 'tax_group_lang.alias' => $key ])
224   - ->andWhere([ 'tax_option_lang.alias' => $param ]);
225   - $product_query->andWhere(['product_option.product_id'=>$product_id]);
226   -
227   - }
228   - $product_query->union($variant_query);
  242 + ->from('product_option')
  243 + ->innerJoin('tax_option', 'product_option.option_id = tax_option.id')
  244 + ->innerJoin(
  245 + 'tax_option_lang',
  246 + 'tax_option_lang.tax_option_id = tax_option.id'
  247 + )
  248 + ->where(
  249 + [ 'tax_option_lang.alias' => $params ]
  250 + )
  251 + ->union($variant_query);
229 252 $query = ( new Query() )->select('products')
230 253 ->from([ 'result_table' => $product_query ]);
231 254 if (!empty( $last_query )) {
... ...