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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Multicurrency / WpQueryMcPrice / PriceFilteringByPostMeta.php
woocommerce-multilingual / classes / Multicurrency / WpQueryMcPrice Last commit date
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