AbstractPriceByPostMeta.php
1 week ago
Factory.php
1 week ago
PriceFilteringByPostMeta.php
1 week ago
PriceOrderByPostMeta.php
1 week ago
PriceFilteringByPostMeta.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Multicurrency\WpQueryMcPrice; |
| 4 | |
| 5 | class PriceFilteringByPostMeta extends AbstractPriceByPostMeta implements \IWPML_Backend_Action { |
| 6 | |
| 7 | public function add_hooks() { |
| 8 | if ( $this->detectProductFilteringByPriceInMultiCurrency() ) { |
| 9 | add_filter( 'pre_get_posts', [ $this, 'pre_get_posts' ] ); |
| 10 | add_filter( 'posts_clauses', [ $this, 'posts_clauses_wcml_price_filter_post_meta' ], 10, 2 ); |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | private function detectProductFilteringByPriceInMultiCurrency(): bool { |
| 15 | if ( ! isset( $_GET['min_price'] ) && ! isset( $_GET['max_price'] ) ) { |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | return $this->default_currency !== $this->client_currency; |
| 20 | } |
| 21 | |
| 22 | public function searchAndRemoveMetaQueryPrice( $needle, &$haystack, &$matches ): bool { |
| 23 | if ( ! is_array( $haystack ) ) { |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | if ( isset( $haystack['key'] ) ) { |
| 28 | if ( $haystack['key'] === $needle ) { |
| 29 | $matches[] = $haystack; |
| 30 | |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | foreach ( $haystack as $key => &$row ) { |
| 38 | if ( $this->searchAndRemoveMetaQueryPrice( $needle, $row, $matches ) ) { |
| 39 | unset( $haystack[ $key ] ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | public function pre_get_posts( $wp_query ) { |
| 47 | if ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] === 'product' ) { |
| 48 | if ( isset( $wp_query->query['meta_query'] ) ) { |
| 49 | $needle = '_price'; |
| 50 | |
| 51 | $matchesQuery = []; |
| 52 | $matchesQueryVars = []; |
| 53 | $this->searchAndRemoveMetaQueryPrice( $needle, $wp_query->query['meta_query'], $matchesQuery ); |
| 54 | $this->searchAndRemoveMetaQueryPrice( $needle, $wp_query->query_vars['meta_query'], $matchesQueryVars ); |
| 55 | $wp_query->wcml_filter_price = $this->findMinMax( $matchesQuery ); |
| 56 | } else { |
| 57 | $query = []; |
| 58 | if ( $wp_query->query['min_price'] ) { |
| 59 | $query['min'] = floatval( $wp_query->query['min_price'] ); |
| 60 | unset( $wp_query->query['min_price'] ); |
| 61 | unset( $wp_query->query_vars['min_price'] ); |
| 62 | } |
| 63 | |
| 64 | if ( $wp_query->query['max_price'] ) { |
| 65 | $query['max'] = floatval( $wp_query->query['max_price'] ); |
| 66 | unset( $wp_query->query['max_price'] ); |
| 67 | unset( $wp_query->query_vars['max_price'] ); |
| 68 | } |
| 69 | $wp_query->wcml_filter_price = $query; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return $wp_query; |
| 74 | } |
| 75 | |
| 76 | public function posts_clauses_wcml_price_filter_post_meta( $clauses, $wp_query ) { |
| 77 | if ( empty( $wp_query->wcml_filter_price ) || ! is_array( $wp_query->wcml_filter_price ) ) { |
| 78 | return $clauses; |
| 79 | } |
| 80 | |
| 81 | $min_price_in_MC = $wp_query->wcml_filter_price['min'] ?? null; |
| 82 | $max_price_in_MC = $wp_query->wcml_filter_price['max'] ?? null; |
| 83 | |
| 84 | $min_price_in_default_currency = $this->woocommerce_wpml->multi_currency->prices->unconvert_price_amount( $min_price_in_MC ); |
| 85 | $max_price_in_default_currency = $this->woocommerce_wpml->multi_currency->prices->unconvert_price_amount( $max_price_in_MC ); |
| 86 | |
| 87 | $clauses['join'] = $this->buildWCMLMultiCurrencyQueryJoin( $clauses['join'] ); |
| 88 | |
| 89 | if ( ! is_null( $min_price_in_MC ) ) { |
| 90 | $min_searchCustomPriceOn = "( ( CAST(" . self::WCML_CUSTOM_PRICES_STATUS_ALIAS . ".meta_value AS SIGNED) = '1') AND ( CAST(" . self::WCML_MC_PRICE_ALIAS . ".meta_value AS decimal(19,4)) >= '{$min_price_in_MC}') ) "; |
| 91 | $min_searchCustomPriceOff = "( ( " . self::WCML_CUSTOM_PRICES_STATUS_ALIAS . ".meta_value IS NULL OR CAST(" . self::WCML_CUSTOM_PRICES_STATUS_ALIAS . ".meta_value AS SIGNED) <> '1') AND ( CAST(" . self::WCML_PRICE_ALIAS . ".meta_value AS decimal(19,4)) >= '{$min_price_in_default_currency}') )"; |
| 92 | |
| 93 | $clauses['where'] .= ' AND ( ' . $min_searchCustomPriceOn . ' OR ' . $min_searchCustomPriceOff . ' )'; |
| 94 | } |
| 95 | |
| 96 | if ( ! is_null( $max_price_in_MC ) ) { |
| 97 | $max_searchCustomPriceOn = "( ( CAST(" . self::WCML_CUSTOM_PRICES_STATUS_ALIAS . ".meta_value AS SIGNED) = '1') AND ( CAST(" . self::WCML_MC_PRICE_ALIAS . ".meta_value AS decimal(19,4)) <= '{$max_price_in_MC}') ) "; |
| 98 | $max_searchCustomPriceOff = "( ( " . self::WCML_CUSTOM_PRICES_STATUS_ALIAS . ".meta_value IS NULL OR CAST(" . self::WCML_CUSTOM_PRICES_STATUS_ALIAS . ".meta_value AS SIGNED) <> '1') AND ( CAST(" . self::WCML_PRICE_ALIAS . ".meta_value AS decimal(19,4)) <= '{$max_price_in_default_currency}') )"; |
| 99 | |
| 100 | $clauses['where'] .= ' AND ( ' . $max_searchCustomPriceOn . ' OR ' . $max_searchCustomPriceOff . ' )'; |
| 101 | } |
| 102 | |
| 103 | return $clauses; |
| 104 | } |
| 105 | |
| 106 | public function findMinMax( array $wcml_filter_price ): array { |
| 107 | $result = [ |
| 108 | 'min' => null, |
| 109 | 'max' => null, |
| 110 | ]; |
| 111 | |
| 112 | foreach ( $wcml_filter_price as $meta ) { |
| 113 | if ( isset( $meta['compare'] ) ) { |
| 114 | if ( $meta['compare'] == '<=' ) { |
| 115 | $result['max'] = floatval( wp_unslash( $meta['value'] ) ); |
| 116 | } else if ( $meta['compare'] == '>=' ) { |
| 117 | $result['min'] = floatval( wp_unslash( $meta['value'] ) ); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $result; |
| 123 | } |
| 124 | } |
| 125 |