From cb55aee82f5837e976b29eec4d63505317c6d88a Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Sun, 4 Dec 2016 02:10:48 +0200 Subject: [PATCH] add variantSku --- helpers/FilterHelper.php | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------- 1 file changed, 90 insertions(+), 67 deletions(-) diff --git a/helpers/FilterHelper.php b/helpers/FilterHelper.php index f073a15..696c6b1 100755 --- a/helpers/FilterHelper.php +++ b/helpers/FilterHelper.php @@ -111,6 +111,69 @@ + + + public static function setNewQueryParams(ActiveQuery $query, array $params) + { + $last_query = null; + foreach ($params as $key => $param) { + switch ($key) { + case 'special': + self::filterSpecial($param, $query); + break; + case 'brands': + self::filterBrands($param, $query); + break; + case 'keywords': + self::filterKeywords($param, $query); + break; + case 'prices': + self::filterPrices($param, $query); + break; + default: + self::filterNewOptions($key, $param, $query); + break; + } + } + } + + + /** + * @param $key + * @param array $param + * @param ActiveQuery $query + */ + private static function filterNewOptions($key, array $param, &$query) + { + $query->andWhere( + 'product.id IN ( + SELECT DISTINCT products + FROM ( + SELECT id AS products FROM product WHERE id IN( + SELECT product_id FROM product_option + INNER JOIN tax_option ON tax_option.id = product_option.option_id + INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id + INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id + INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id + WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\')) + OR id IN ( + (SELECT product_id AS products + FROM product_variant_option + INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.id + INNER JOIN tax_option ON tax_option.id = product_variant_option.option_id + INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id + INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id + INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id + WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\')) + ) + ) AS table_name + )' + ); + + + } + + /** * Fill query with filter conditions * @@ -123,24 +186,22 @@ foreach ($params as $key => $param) { switch ($key) { case 'special': - unset($params[$key]); self::filterSpecial($param, $query); break; case 'brands': - unset($params[$key]); self::filterBrands($param, $query); break; case 'keywords': - unset($params[$key]); self::filterKeywords($param, $query); break; case 'prices': - unset($params[$key]); self::filterPrices($param, $query); break; + default: + $last_query = self::filterOptions($param, $last_query); + break; } } - $last_query = self::filterOptions($params, $last_query); // If tax option filters were provided filter query with them if (!empty( $last_query )) { $query->andWhere([ 'product.id' => $last_query ]); @@ -148,84 +209,46 @@ } - - + /** * Tax Option filter * * @param string[] $params * @param \yii\db\Query|null $last_query - * @param bool $in_stock * - * @return \yii\db\Query + * @return Query */ - private static function filterOptions(array $params, Query $last_query = null, bool $in_stock = true): Query + private static function filterOptions(array $params, Query $last_query = null): Query { + $variant_query = ( new Query() )->distinct() ->select('product_variant.product_id as products') ->from('product_variant_option') ->innerJoin( 'product_variant', 'product_variant_option.product_variant_id = product_variant.id' - ); - - foreach ($params as $key=>$param){ - $product_variant_id = ( new Query() )->distinct()->select('product_variant_option.product_variant_id as id') - ->from('product_variant_option') - ->innerJoin( - 'tax_option', - 'tax_option.id = product_variant_option.option_id' - ) - ->innerJoin( - 'tax_option_lang', - 'tax_option_lang.tax_option_id = tax_option.id' - ) - ->innerJoin( - 'tax_group', - 'tax_group.id = tax_option.tax_group_id' - ) - ->innerJoin( - 'tax_group_lang', - 'tax_group_lang.tax_group_id = tax_group.id' - ) - ->where([ 'tax_group_lang.alias' => $key ]) - ->andWhere([ 'tax_option_lang.alias' => $param ]); - $variant_query->andWhere(['product_variant_option.product_variant_id'=>$product_variant_id]); - - } - - - if($in_stock) { - $variant_query->andWhere(['!=', 'product_variant.stock', 0]); - } + ) + ->innerJoin( + 'tax_option', + 'tax_option.id = product_variant_option.option_id' + ) + ->innerJoin( + 'tax_option_lang', + 'tax_option_lang.tax_option_id = tax_option.id' + ) + ->where([ 'tax_option_lang.alias' => $params ]); $product_query = ( new Query() )->distinct() ->select('product_option.product_id as products') - ->from('product_option'); - foreach ($params as $key=>$param){ - $product_id = ( new Query() )->distinct()->select('product_option.product_id as id') - ->from('product_option') - ->innerJoin( - 'tax_option', - 'tax_option.id = product_option.option_id' - ) - ->innerJoin( - 'tax_option_lang', - 'tax_option_lang.tax_option_id = tax_option.id' - ) - ->innerJoin( - 'tax_group', - 'tax_group.id = tax_option.tax_group_id' - ) - ->innerJoin( - 'tax_group_lang', - 'tax_group_lang.tax_group_id = tax_group.id' - ) - ->where([ 'tax_group_lang.alias' => $key ]) - ->andWhere([ 'tax_option_lang.alias' => $param ]); - $product_query->andWhere(['product_option.product_id'=>$product_id]); - - } - $product_query->union($variant_query); + ->from('product_option') + ->innerJoin('tax_option', 'product_option.option_id = tax_option.id') + ->innerJoin( + 'tax_option_lang', + 'tax_option_lang.tax_option_id = tax_option.id' + ) + ->where( + [ 'tax_option_lang.alias' => $params ] + ) + ->union($variant_query); $query = ( new Query() )->select('products') ->from([ 'result_table' => $product_query ]); if (!empty( $last_query )) { -- libgit2 0.21.4