Commit 1a954235fa140827085d074a0494933eef87bbb2
1 parent
e8d4c8fa
add variantSku
Showing
2 changed files
with
354 additions
and
381 deletions
Show diff stats
helpers/FilterHelper.php
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - namespace artweb\artbox\ecommerce\helpers; | |
| 4 | - | |
| 5 | - use artweb\artbox\ecommerce\models\BrandLang; | |
| 6 | - use artweb\artbox\ecommerce\models\CategoryLang; | |
| 7 | - use artweb\artbox\ecommerce\models\Product; | |
| 8 | - use artweb\artbox\ecommerce\models\ProductLang; | |
| 9 | - use artweb\artbox\ecommerce\models\ProductVariant; | |
| 10 | - use artweb\artbox\ecommerce\models\ProductVariantLang; | |
| 11 | - use artweb\artbox\ecommerce\models\TaxGroup; | |
| 12 | - use yii\base\Object; | |
| 13 | - use yii\db\ActiveQuery; | |
| 14 | - use yii\db\Query; | |
| 15 | - use yii\helpers\ArrayHelper; | |
| 16 | - | |
| 17 | - class FilterHelper extends Object | |
| 3 | +namespace artweb\artbox\ecommerce\helpers; | |
| 4 | + | |
| 5 | +use artweb\artbox\ecommerce\models\BrandLang; | |
| 6 | +use artweb\artbox\ecommerce\models\CategoryLang; | |
| 7 | +use artweb\artbox\ecommerce\models\Product; | |
| 8 | +use artweb\artbox\ecommerce\models\ProductLang; | |
| 9 | +use artweb\artbox\ecommerce\models\ProductVariant; | |
| 10 | +use artweb\artbox\ecommerce\models\ProductVariantLang; | |
| 11 | +use artweb\artbox\ecommerce\models\TaxGroup; | |
| 12 | +use yii\base\Object; | |
| 13 | +use yii\db\ActiveQuery; | |
| 14 | +use yii\db\Query; | |
| 15 | +use yii\helpers\ArrayHelper; | |
| 16 | + | |
| 17 | +class FilterHelper extends Object | |
| 18 | +{ | |
| 19 | + | |
| 20 | + public static $optionsList = []; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Get TaxGroups | |
| 24 | + * | |
| 25 | + * @return array | |
| 26 | + */ | |
| 27 | + public static function optionsTemplate() | |
| 18 | 28 | { |
| 19 | - | |
| 20 | - public static $optionsList = []; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * Get TaxGroups | |
| 24 | - * | |
| 25 | - * @return array | |
| 26 | - */ | |
| 27 | - public static function optionsTemplate() | |
| 28 | - { | |
| 29 | - if (empty( static::$optionsList )) { | |
| 30 | - return static::$optionsList = ArrayHelper::getColumn( | |
| 31 | - TaxGroup::find() | |
| 32 | - ->joinWith('lang') | |
| 33 | - ->where([ 'is_filter' => 'TRUE' ]) | |
| 34 | - ->all(), | |
| 35 | - 'lang.alias' | |
| 36 | - ); | |
| 37 | - } else { | |
| 38 | - return static::$optionsList; | |
| 39 | - } | |
| 40 | - | |
| 29 | + if (empty( static::$optionsList )) { | |
| 30 | + return static::$optionsList = ArrayHelper::getColumn( | |
| 31 | + TaxGroup::find() | |
| 32 | + ->joinWith('lang') | |
| 33 | + ->where([ 'is_filter' => 'TRUE' ]) | |
| 34 | + ->all(), | |
| 35 | + 'lang.alias' | |
| 36 | + ); | |
| 37 | + } else { | |
| 38 | + return static::$optionsList; | |
| 41 | 39 | } |
| 42 | 40 | |
| 43 | - /** | |
| 44 | - * Return custom filter-option link | |
| 45 | - * | |
| 46 | - * @param array $filter | |
| 47 | - * @param string $key | |
| 48 | - * @param mixed $value | |
| 49 | - * @param bool $remove | |
| 50 | - * | |
| 51 | - * @return array | |
| 52 | - */ | |
| 53 | - public static function getFilterForOption(array $filter, string $key, $value, bool $remove = false) | |
| 54 | - { | |
| 41 | + } | |
| 55 | 42 | |
| 56 | - $optionsTemplate = self::optionsTemplate(); | |
| 57 | - array_unshift($optionsTemplate, "special", "brands"); | |
| 43 | + /** | |
| 44 | + * Return custom filter-option link | |
| 45 | + * | |
| 46 | + * @param array $filter | |
| 47 | + * @param string $key | |
| 48 | + * @param mixed $value | |
| 49 | + * @param bool $remove | |
| 50 | + * | |
| 51 | + * @return array | |
| 52 | + */ | |
| 53 | + public static function getFilterForOption(array $filter, string $key, $value, bool $remove = false) | |
| 54 | + { | |
| 58 | 55 | |
| 59 | - $result = $filter; | |
| 56 | + $optionsTemplate = self::optionsTemplate(); | |
| 57 | + array_unshift($optionsTemplate, "special", "brands"); | |
| 60 | 58 | |
| 61 | - if (is_array($value)) { | |
| 62 | - foreach ($value as $value_key => $value_items) { | |
| 63 | - if (!is_array($value_items)) { | |
| 64 | - $value_items = [ $value_items ]; | |
| 65 | - } | |
| 66 | - foreach ($value_items as $value_item) { | |
| 67 | - if ($remove && isset( $result[ $key ] ) && ( $i = array_search( | |
| 59 | + $result = $filter; | |
| 60 | + | |
| 61 | + if (is_array($value)) { | |
| 62 | + foreach ($value as $value_key => $value_items) { | |
| 63 | + if (!is_array($value_items)) { | |
| 64 | + $value_items = [ $value_items ]; | |
| 65 | + } | |
| 66 | + foreach ($value_items as $value_item) { | |
| 67 | + if ($remove && isset( $result[ $key ] ) && ( $i = array_search( | |
| 68 | + $value_item, | |
| 69 | + $result[ $key ][ $value_key ] | |
| 70 | + ) ) !== false | |
| 71 | + ) { | |
| 72 | + unset( $result[ $key ][ $value_key ][ $i ] ); | |
| 73 | + if (empty( $result[ $key ][ $value_key ] )) { | |
| 74 | + unset( $result[ $key ][ $value_key ] ); | |
| 75 | + } | |
| 76 | + } else { | |
| 77 | + if (!isset( $result[ $key ][ $value_key ] ) || array_search( | |
| 68 | 78 | $value_item, |
| 69 | 79 | $result[ $key ][ $value_key ] |
| 70 | - ) ) !== false | |
| 80 | + ) === false | |
| 71 | 81 | ) { |
| 72 | - unset( $result[ $key ][ $value_key ][ $i ] ); | |
| 73 | - if (empty( $result[ $key ][ $value_key ] )) { | |
| 74 | - unset( $result[ $key ][ $value_key ] ); | |
| 75 | - } | |
| 76 | - } else { | |
| 77 | - if (!isset( $result[ $key ][ $value_key ] ) || array_search( | |
| 78 | - $value_item, | |
| 79 | - $result[ $key ][ $value_key ] | |
| 80 | - ) === false | |
| 81 | - ) { | |
| 82 | - $result[ $key ][ $value_key ][] = $value_item; | |
| 83 | - } | |
| 82 | + $result[ $key ][ $value_key ][] = $value_item; | |
| 84 | 83 | } |
| 85 | 84 | } |
| 86 | 85 | } |
| 86 | + } | |
| 87 | + } else { | |
| 88 | + if ($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) { | |
| 89 | + unset( $result[ $key ][ $i ] ); | |
| 90 | + if (empty( $result[ $key ] )) { | |
| 91 | + unset( $result[ $key ] ); | |
| 92 | + } | |
| 87 | 93 | } else { |
| 88 | - if ($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) { | |
| 89 | - unset( $result[ $key ][ $i ] ); | |
| 90 | - if (empty( $result[ $key ] )) { | |
| 91 | - unset( $result[ $key ] ); | |
| 92 | - } | |
| 93 | - } else { | |
| 94 | - if (!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) { | |
| 95 | - $result[ $key ][] = $value; | |
| 96 | - } | |
| 94 | + if (!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) { | |
| 95 | + $result[ $key ][] = $value; | |
| 97 | 96 | } |
| 98 | 97 | } |
| 98 | + } | |
| 99 | 99 | |
| 100 | - $filterView = []; | |
| 101 | - | |
| 102 | - foreach ($optionsTemplate as $optionKey) { | |
| 103 | - if (isset( $result[ $optionKey ] )) { | |
| 104 | - $filterView[ $optionKey ] = $result[ $optionKey ]; | |
| 105 | - } | |
| 100 | + $filterView = []; | |
| 106 | 101 | |
| 102 | + foreach ($optionsTemplate as $optionKey) { | |
| 103 | + if (isset( $result[ $optionKey ] )) { | |
| 104 | + $filterView[ $optionKey ] = $result[ $optionKey ]; | |
| 107 | 105 | } |
| 108 | 106 | |
| 109 | - return $filterView; | |
| 110 | 107 | } |
| 111 | 108 | |
| 109 | + return $filterView; | |
| 110 | + } | |
| 112 | 111 | |
| 113 | 112 | |
| 114 | - /** | |
| 115 | - * select options for product variants with selected category | |
| 116 | - * | |
| 117 | - * @param integer $categoryId | |
| 118 | - * @param integer $langId | |
| 119 | - * @return mixed | |
| 120 | - */ | |
| 121 | - public static function getProductVariantOptions($categoryId,$langId){ | |
| 122 | - | |
| 123 | - $cacheKey = [ | |
| 124 | - 'OptionsForFilter', | |
| 125 | - 'categoryId' => $categoryId, | |
| 126 | - 'langId' =>$langId | |
| 127 | - ]; | |
| 128 | - if (!$OptionsForFilter = \Yii::$app->cache->get($cacheKey)) { | |
| 129 | - $OptionsForFilter = ( new Query() )->distinct()->select('tax_group_lang.alias') | |
| 130 | - ->from('product_variant_option') | |
| 131 | - ->innerJoin( | |
| 132 | - 'tax_option', | |
| 133 | - 'product_variant_option.option_id = tax_option.id' | |
| 134 | - ) | |
| 135 | - ->innerJoin( | |
| 136 | - 'tax_group', | |
| 137 | - 'tax_group.id = tax_option.tax_group_id' | |
| 138 | - ) | |
| 139 | - ->innerJoin( | |
| 140 | - 'tax_group_lang', | |
| 141 | - 'tax_group_lang.tax_group_id = tax_group.id' | |
| 142 | - ) | |
| 143 | - ->innerJoin( | |
| 144 | - 'tax_group_to_category', | |
| 145 | - 'tax_group_to_category.tax_group_id = tax_group.id' | |
| 146 | - ) | |
| 147 | - ->where([ | |
| 148 | - 'tax_group_lang.language_id' => $langId, | |
| 149 | - 'tax_group_to_category.category_id' => $categoryId, | |
| 150 | - 'tax_group.is_filter' => true | |
| 151 | - ])->all(); | |
| 152 | - $OptionsForFilter = ArrayHelper::getColumn($OptionsForFilter,'alias'); | |
| 153 | - \Yii::$app->cache->set($cacheKey, $OptionsForFilter, 3600 * 24); | |
| 154 | - } | |
| 155 | - | |
| 156 | - return $OptionsForFilter; | |
| 157 | - } | |
| 158 | 113 | |
| 159 | 114 | |
| 160 | - /** | |
| 161 | - * Fill query with filter conditions | |
| 162 | - * | |
| 163 | - * @param ActiveQuery $query | |
| 164 | - * @param array $params | |
| 165 | - * @param integer $categoryId | |
| 166 | - * @param integer $langId | |
| 167 | - */ | |
| 168 | - public static function setQueryParams(ActiveQuery $query, array $params, $categoryId, $langId) | |
| 169 | - { | |
| 170 | - $last_query = null; | |
| 171 | - $productVariantOptions = self::getProductVariantOptions($categoryId,$langId); | |
| 172 | - foreach ($params as $key => $param) { | |
| 173 | - switch ($key) { | |
| 174 | - case 'special': | |
| 175 | - unset($params[$key]); | |
| 176 | - self::filterSpecial($param, $query); | |
| 177 | - break; | |
| 178 | - case 'brands': | |
| 179 | - unset($params[$key]); | |
| 180 | - self::filterBrands($param, $query); | |
| 181 | - break; | |
| 182 | - case 'keywords': | |
| 183 | - unset($params[$key]); | |
| 184 | - self::filterKeywords($param, $query); | |
| 185 | - break; | |
| 186 | - case 'prices': | |
| 187 | - unset($params[$key]); | |
| 188 | - self::filterPrices($param, $query); | |
| 189 | - break; | |
| 190 | 115 | |
| 191 | - } | |
| 192 | - } | |
| 193 | - if(!empty($params)){ | |
| 194 | - self::filterOptions($params, $productVariantOptions, $query); | |
| 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; | |
| 195 | 136 | } |
| 196 | - | |
| 197 | 137 | } |
| 138 | + } | |
| 198 | 139 | |
| 199 | 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 | + ); | |
| 200 | 172 | |
| 201 | - /** | |
| 202 | - * Tax Option filter | |
| 203 | - * | |
| 204 | - * @param string[] $params | |
| 205 | - * @param string[] $productVariantOptions | |
| 206 | - * @param \yii\db\Query|null $query | |
| 207 | - * @param bool $in_stock | |
| 208 | - * | |
| 209 | - * @return \yii\db\Query | |
| 210 | - */ | |
| 211 | - private static function filterOptions(array $params,$productVariantOptions, Query &$query = null, bool $in_stock = true) | |
| 212 | - { | |
| 213 | - $variant_query = ( new Query() )->distinct() | |
| 214 | - ->select('product_variant.product_id as products') | |
| 215 | - ->from('product_variant'); | |
| 216 | - $variantSearch = false; | |
| 217 | - foreach ($params as $key=>$param){ | |
| 218 | - if(in_array($key, $productVariantOptions)){ | |
| 219 | - $variantSearch = true; | |
| 220 | - unset($params[$key]); | |
| 221 | - $product_variant_id = ( new Query() )->distinct()->select('product_variant_option.product_variant_id as id') | |
| 222 | - ->from('product_variant_option') | |
| 223 | - ->innerJoin( | |
| 224 | - 'tax_option', | |
| 225 | - 'tax_option.id = product_variant_option.option_id' | |
| 226 | - ) | |
| 227 | - ->innerJoin( | |
| 228 | - 'tax_option_lang', | |
| 229 | - 'tax_option_lang.tax_option_id = tax_option.id' | |
| 230 | - ) | |
| 231 | - ->innerJoin( | |
| 232 | - 'tax_group', | |
| 233 | - 'tax_group.id = tax_option.tax_group_id' | |
| 234 | - ) | |
| 235 | - ->innerJoin( | |
| 236 | - 'tax_group_lang', | |
| 237 | - 'tax_group_lang.tax_group_id = tax_group.id' | |
| 238 | - ) | |
| 239 | - ->where([ 'tax_group_lang.alias' => $key ]) | |
| 240 | - ->andWhere([ 'tax_option_lang.alias' => $param ]); | |
| 241 | - $variant_query->andWhere(['product_variant.id'=>$product_variant_id]); | |
| 242 | - } | |
| 243 | - | |
| 244 | - } | |
| 245 | - | |
| 246 | - if($variantSearch){ | |
| 247 | - if($in_stock) { | |
| 248 | - $variant_query->andWhere(['!=', 'product_variant.stock', 0]); | |
| 249 | - } | |
| 250 | 173 | |
| 174 | + } | |
| 251 | 175 | |
| 252 | - $query->andWhere([ 'product.id' => $variant_query ]); | |
| 253 | 176 | |
| 177 | + /** | |
| 178 | + * Fill query with filter conditions | |
| 179 | + * | |
| 180 | + * @param ActiveQuery $query | |
| 181 | + * @param array $params | |
| 182 | + */ | |
| 183 | + public static function setQueryParams(ActiveQuery $query, array $params) | |
| 184 | + { | |
| 185 | + $last_query = null; | |
| 186 | + foreach ($params as $key => $param) { | |
| 187 | + switch ($key) { | |
| 188 | + case 'special': | |
| 189 | + self::filterSpecial($param, $query); | |
| 190 | + break; | |
| 191 | + case 'brands': | |
| 192 | + self::filterBrands($param, $query); | |
| 193 | + break; | |
| 194 | + case 'keywords': | |
| 195 | + self::filterKeywords($param, $query); | |
| 196 | + break; | |
| 197 | + case 'prices': | |
| 198 | + self::filterPrices($param, $query); | |
| 199 | + break; | |
| 200 | + default: | |
| 201 | + $last_query = self::filterOptions($param, $last_query); | |
| 202 | + break; | |
| 254 | 203 | } |
| 204 | + } | |
| 205 | + // If tax option filters were provided filter query with them | |
| 206 | + if (!empty( $last_query )) { | |
| 207 | + $query->andWhere([ 'product.id' => $last_query ]); | |
| 208 | + } | |
| 209 | + } | |
| 255 | 210 | |
| 256 | 211 | |
| 257 | 212 | |
| 258 | - if(!empty($params)) { | |
| 259 | - $product_query = ( new Query() )->distinct() | |
| 260 | - ->select('product_option.product_id as products') | |
| 261 | - ->from('product_option') | |
| 262 | - ->innerJoin( | |
| 263 | - 'tax_option', | |
| 264 | - 'tax_option.id = product_option.option_id' | |
| 265 | - ) | |
| 266 | - ->innerJoin( | |
| 267 | - 'tax_option_lang', | |
| 268 | - 'tax_option_lang.tax_option_id = tax_option.id' | |
| 269 | - ) | |
| 270 | - ->innerJoin( | |
| 271 | - 'tax_group', | |
| 272 | - 'tax_group.id = tax_option.tax_group_id' | |
| 273 | - ) | |
| 274 | - ->innerJoin( | |
| 275 | - 'tax_group_lang', | |
| 276 | - 'tax_group_lang.tax_group_id = tax_group.id' | |
| 277 | - ); | |
| 278 | - foreach ($params as $key=>$param){ | |
| 279 | - $product_query | |
| 280 | - ->where([ 'tax_group_lang.alias' => $key ]) | |
| 281 | - ->andWhere([ 'tax_option_lang.alias' => $param ]); | |
| 282 | - | |
| 283 | - } | |
| 284 | - $query->andWhere([ 'product.id' => $product_query ]); | |
| 285 | - } | |
| 286 | - | |
| 213 | + /** | |
| 214 | + * Tax Option filter | |
| 215 | + * | |
| 216 | + * @param string[] $params | |
| 217 | + * @param \yii\db\Query|null $last_query | |
| 218 | + * @param bool $in_stock | |
| 219 | + * | |
| 220 | + * @return \yii\db\Query | |
| 221 | + */ | |
| 222 | + private static function filterOptions(array $params, Query $last_query = null, bool $in_stock = true): Query | |
| 223 | + { | |
| 287 | 224 | |
| 225 | + $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 ]); | |
| 241 | + if($in_stock) { | |
| 242 | + $variant_query->andWhere(['!=', 'product_variant.stock', 0]); | |
| 288 | 243 | } |
| 289 | - | |
| 290 | - | |
| 291 | - | |
| 292 | - /** | |
| 293 | - * Fill $query with special filters (used in Product) | |
| 294 | - * | |
| 295 | - * @param array $params | |
| 296 | - * @param \yii\db\ActiveQuery $query | |
| 297 | - */ | |
| 298 | - private static function filterSpecial(array $params, ActiveQuery $query) | |
| 299 | - { | |
| 300 | - $conditions = []; | |
| 301 | - /** | |
| 302 | - * @var string $key | |
| 303 | - */ | |
| 304 | - foreach ($params as $key => $param) { | |
| 305 | - $conditions[] = [ | |
| 306 | - '=', | |
| 307 | - Product::tableName() . '.' . $key, | |
| 308 | - $param, | |
| 309 | - ]; | |
| 310 | - } | |
| 311 | - /* If 2 or more special conditions get all that satisfy at least one of them. */ | |
| 312 | - if (count($conditions) > 1) { | |
| 313 | - array_unshift($conditions, 'or'); | |
| 314 | - } else { | |
| 315 | - $conditions = $conditions[ 0 ]; | |
| 316 | - } | |
| 317 | - $query->andFilterWhere($conditions); | |
| 244 | + $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 ]); | |
| 318 | 260 | } |
| 261 | + return $query; | |
| 262 | + } | |
| 319 | 263 | |
| 264 | + /** | |
| 265 | + * Fill $query with special filters (used in Product) | |
| 266 | + * | |
| 267 | + * @param array $params | |
| 268 | + * @param \yii\db\ActiveQuery $query | |
| 269 | + */ | |
| 270 | + private static function filterSpecial(array $params, ActiveQuery $query) | |
| 271 | + { | |
| 272 | + $conditions = []; | |
| 320 | 273 | /** |
| 321 | - * Fill query with brands filter | |
| 322 | - * | |
| 323 | - * @param int[] $param | |
| 324 | - * @param \yii\db\ActiveQuery $query | |
| 274 | + * @var string $key | |
| 325 | 275 | */ |
| 326 | - private static function filterBrands(array $param, ActiveQuery $query) | |
| 327 | - { | |
| 328 | - $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); | |
| 276 | + foreach ($params as $key => $param) { | |
| 277 | + $conditions[] = [ | |
| 278 | + '=', | |
| 279 | + Product::tableName() . '.' . $key, | |
| 280 | + $param, | |
| 281 | + ]; | |
| 329 | 282 | } |
| 283 | + /* If 2 or more special conditions get all that satisfy at least one of them. */ | |
| 284 | + if (count($conditions) > 1) { | |
| 285 | + array_unshift($conditions, 'or'); | |
| 286 | + } else { | |
| 287 | + $conditions = $conditions[ 0 ]; | |
| 288 | + } | |
| 289 | + $query->andFilterWhere($conditions); | |
| 290 | + } | |
| 330 | 291 | |
| 331 | - /** | |
| 332 | - * Fill query with keywords filter | |
| 333 | - * | |
| 334 | - * @param array $params | |
| 335 | - * @param \yii\db\ActiveQuery $query | |
| 336 | - */ | |
| 337 | - private static function filterKeywords(array $params, ActiveQuery $query) | |
| 338 | - { | |
| 339 | - $conditions = []; | |
| 340 | - if (!empty( $params )) { | |
| 341 | - if (!is_array($params)) { | |
| 342 | - $params = [ $params ]; | |
| 292 | + /** | |
| 293 | + * Fill query with brands filter | |
| 294 | + * | |
| 295 | + * @param int[] $param | |
| 296 | + * @param \yii\db\ActiveQuery $query | |
| 297 | + */ | |
| 298 | + private static function filterBrands(array $param, ActiveQuery $query) | |
| 299 | + { | |
| 300 | + $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); | |
| 301 | + } | |
| 302 | + | |
| 303 | + /** | |
| 304 | + * Fill query with keywords filter | |
| 305 | + * | |
| 306 | + * @param array $params | |
| 307 | + * @param \yii\db\ActiveQuery $query | |
| 308 | + */ | |
| 309 | + private static function filterKeywords(array $params, ActiveQuery $query) | |
| 310 | + { | |
| 311 | + $conditions = []; | |
| 312 | + if (!empty( $params )) { | |
| 313 | + if (!is_array($params)) { | |
| 314 | + $params = [ $params ]; | |
| 315 | + } | |
| 316 | + /** | |
| 317 | + * @var string $param Inputed keyword | |
| 318 | + */ | |
| 319 | + foreach ($params as $param) { | |
| 320 | + | |
| 321 | + if(iconv_strlen($param) >= 3){ | |
| 322 | + $conditions[] = [ | |
| 323 | + 'or', | |
| 324 | + [ | |
| 325 | + 'ilike', | |
| 326 | + ProductLang::tableName() . '.title', | |
| 327 | + $param, | |
| 328 | + ], | |
| 329 | + [ | |
| 330 | + 'ilike', | |
| 331 | + BrandLang::tableName() . '.title', | |
| 332 | + $param, | |
| 333 | + ], | |
| 334 | + [ | |
| 335 | + 'ilike', | |
| 336 | + CategoryLang::tableName() . '.title', | |
| 337 | + $param, | |
| 338 | + ], | |
| 339 | + [ | |
| 340 | + 'ilike', | |
| 341 | + ProductVariantLang::tableName() . '.title', | |
| 342 | + $param, | |
| 343 | + ], | |
| 344 | + [ | |
| 345 | + 'ilike', | |
| 346 | + ProductVariant::tableName() . '.sku', | |
| 347 | + $param, | |
| 348 | + ] | |
| 349 | + | |
| 350 | + ]; | |
| 343 | 351 | } |
| 344 | - /** | |
| 345 | - * @var string $param Inputed keyword | |
| 346 | - */ | |
| 347 | - foreach ($params as $param) { | |
| 348 | - | |
| 349 | - if(iconv_strlen($param) >= 3){ | |
| 350 | - $conditions[] = [ | |
| 351 | - 'or', | |
| 352 | - [ | |
| 353 | - 'ilike', | |
| 354 | - ProductLang::tableName() . '.title', | |
| 355 | - $param, | |
| 356 | - ], | |
| 357 | - [ | |
| 358 | - 'ilike', | |
| 359 | - BrandLang::tableName() . '.title', | |
| 360 | - $param, | |
| 361 | - ], | |
| 362 | - [ | |
| 363 | - 'ilike', | |
| 364 | - CategoryLang::tableName() . '.title', | |
| 365 | - $param, | |
| 366 | - ], | |
| 367 | - [ | |
| 368 | - 'ilike', | |
| 369 | - ProductVariantLang::tableName() . '.title', | |
| 370 | - $param, | |
| 371 | - ], | |
| 372 | - [ | |
| 373 | - 'ilike', | |
| 374 | - ProductVariant::tableName() . '.sku', | |
| 375 | - $param, | |
| 376 | - ] | |
| 377 | - | |
| 378 | - ]; | |
| 379 | - } | |
| 380 | 352 | |
| 381 | 353 | |
| 382 | - } | |
| 383 | - } | |
| 384 | - if (count($conditions) > 1) { | |
| 385 | - array_unshift($conditions, 'or'); | |
| 386 | - } else { | |
| 387 | - $conditions = $conditions[ 0 ]; | |
| 388 | 354 | } |
| 389 | - $query->andFilterWhere($conditions); | |
| 390 | 355 | } |
| 391 | - | |
| 392 | - /** | |
| 393 | - * Fill query with price limits filter | |
| 394 | - * | |
| 395 | - * @param array $params | |
| 396 | - * @param \yii\db\ActiveQuery $query | |
| 397 | - */ | |
| 398 | - private static function filterPrices(array $params, ActiveQuery $query) | |
| 399 | - { | |
| 400 | - $conditions = []; | |
| 401 | - if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) { | |
| 402 | - $conditions[] = [ | |
| 403 | - '>=', | |
| 404 | - ProductVariant::tableName() . '.price', | |
| 405 | - $params[ 'min' ], | |
| 406 | - ]; | |
| 407 | - } | |
| 408 | - if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) { | |
| 409 | - $conditions[] = [ | |
| 410 | - '<=', | |
| 411 | - ProductVariant::tableName() . '.price', | |
| 412 | - $params[ 'max' ], | |
| 413 | - ]; | |
| 414 | - } | |
| 415 | - if (count($conditions) > 1) { | |
| 416 | - array_unshift($conditions, 'and'); | |
| 417 | - } else { | |
| 418 | - $conditions = $conditions[ 0 ]; | |
| 419 | - } | |
| 420 | - $query->andFilterWhere($conditions); | |
| 356 | + if (count($conditions) > 1) { | |
| 357 | + array_unshift($conditions, 'or'); | |
| 358 | + } else { | |
| 359 | + $conditions = $conditions[ 0 ]; | |
| 421 | 360 | } |
| 361 | + $query->andFilterWhere($conditions); | |
| 362 | + } | |
| 422 | 363 | |
| 364 | + /** | |
| 365 | + * Fill query with price limits filter | |
| 366 | + * | |
| 367 | + * @param array $params | |
| 368 | + * @param \yii\db\ActiveQuery $query | |
| 369 | + */ | |
| 370 | + private static function filterPrices(array $params, ActiveQuery $query) | |
| 371 | + { | |
| 372 | + $conditions = []; | |
| 373 | + if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) { | |
| 374 | + $conditions[] = [ | |
| 375 | + '>=', | |
| 376 | + ProductVariant::tableName() . '.price', | |
| 377 | + $params[ 'min' ], | |
| 378 | + ]; | |
| 379 | + } | |
| 380 | + if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) { | |
| 381 | + $conditions[] = [ | |
| 382 | + '<=', | |
| 383 | + ProductVariant::tableName() . '.price', | |
| 384 | + $params[ 'max' ], | |
| 385 | + ]; | |
| 386 | + } | |
| 387 | + if (count($conditions) > 1) { | |
| 388 | + array_unshift($conditions, 'and'); | |
| 389 | + } else { | |
| 390 | + $conditions = $conditions[ 0 ]; | |
| 391 | + } | |
| 392 | + $query->andFilterWhere($conditions); | |
| 423 | 393 | } |
| 394 | + | |
| 395 | +} | |
| 424 | 396 | |
| 425 | 397 | \ No newline at end of file | ... | ... |
models/ProductFrontendSearch.php
| ... | ... | @@ -4,7 +4,6 @@ |
| 4 | 4 | |
| 5 | 5 | use artweb\artbox\ecommerce\helpers\FilterHelper; |
| 6 | 6 | use artweb\artbox\ecommerce\models\Category; |
| 7 | - use artweb\artbox\language\models\Language; | |
| 8 | 7 | use yii\base\Model; |
| 9 | 8 | use yii\data\ActiveDataProvider; |
| 10 | 9 | use yii\data\ArrayDataProvider; |
| ... | ... | @@ -118,18 +117,22 @@ |
| 118 | 117 | return $dataProvider; |
| 119 | 118 | } |
| 120 | 119 | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * @param Category $category | |
| 124 | - * @param array $params | |
| 125 | - * @param bool $in_stock | |
| 126 | - * @return mixed | |
| 127 | - */ | |
| 128 | - public function getSearchQuery($category, $params = [], $in_stock = true) | |
| 120 | + public function getSearchQuery($category = null, $params = [], $in_stock = true) | |
| 129 | 121 | { |
| 130 | 122 | |
| 131 | - $query = $category->getProducts(); | |
| 123 | + if (!empty( $category )) { | |
| 124 | + /** @var ActiveQuery $query */ | |
| 125 | + /**@var Category $category * */ | |
| 126 | + $query = $category->getProducts(); | |
| 132 | 127 | |
| 128 | + } else { | |
| 129 | + $query = Product::find() | |
| 130 | + ->joinWith( | |
| 131 | + [ | |
| 132 | + 'category.lang', | |
| 133 | + ] | |
| 134 | + ); | |
| 135 | + } | |
| 133 | 136 | |
| 134 | 137 | $query->select([ 'product.*' ]); |
| 135 | 138 | $query->joinWith( |
| ... | ... | @@ -173,8 +176,7 @@ |
| 173 | 176 | ] |
| 174 | 177 | ); |
| 175 | 178 | |
| 176 | - $lang = Language::getCurrent(); | |
| 177 | - FilterHelper::setQueryParams($query, $params, $category->id, $lang->id); | |
| 179 | + FilterHelper::setQueryParams($query, $params); | |
| 178 | 180 | return $query; |
| 179 | 181 | } |
| 180 | 182 | |
| ... | ... | @@ -200,8 +202,7 @@ |
| 200 | 202 | // Price filter fix |
| 201 | 203 | unset( $params[ 'prices' ] ); |
| 202 | 204 | |
| 203 | - $lang = Language::getCurrent(); | |
| 204 | - FilterHelper::setQueryParams($query, $params, $category->id, $lang->id); | |
| 205 | + FilterHelper::setQueryParams($query, $params); | |
| 205 | 206 | $query->andWhere( |
| 206 | 207 | [ |
| 207 | 208 | '>=', | ... | ... |