PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 2.2.1
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v2.2.1
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / core / query-modifier.php
shopengine / core Last commit date
builders 4 years ago elementor-controls 4 years ago export-import 4 years ago page-templates 4 years ago register 4 years ago sample-designs 4 years ago service-providers 4 years ago settings 4 years ago theme-support 4 years ago query-modifier.php 4 years ago service-provider-manager.php 4 years ago template-cpt.php 4 years ago
query-modifier.php
154 lines
1 <?php
2
3 namespace ShopEngine\Core;
4
5 use ShopEngine\Traits\Singleton;
6
7 class Query_Modifier
8 {
9
10 use Singleton;
11
12 private $custom_query = [];
13
14 public function init()
15 {
16
17 add_action('pre_get_posts', [$this, 'modify_query']);
18 }
19
20 public function modify_query($query)
21 {
22
23 if (is_admin() || !$query->is_main_query() || $query->is_single === true) {
24 return;
25 }
26
27 if (!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') {
28 return;
29 }
30
31 // query filter begins
32
33 // update query for product per page filter
34 if (!empty($_GET['shopengine_products_per_page'])) {
35 $query->set('posts_per_page', absint($_GET['shopengine_products_per_page']));
36 }
37
38
39 $color_prefix = 'shopengine_filter_color_';
40
41 $attribute_prefix = 'shopengine_filter_attribute_';
42
43 $image_prefix = 'shopengine_filter_image_';
44
45 $label_prefix = 'shopengine_filter_label_';
46
47 $shipping_prefix = 'shopengine_filter_shipping_';
48
49 $category_prefix = 'shopengine_filter_category';
50
51 $stock_prefix = 'shopengine_filter_stock';
52
53 $sale_prefix = 'shopengine_filter_onsale';
54
55 $meta_query = ['relation' => 'AND'];
56
57 foreach ($_GET as $key => $value) {
58
59 if ($key === 'rating_filter') {
60 $meta_query[] = [
61 'key' => '_wc_average_rating',
62 'value' => explode(',', trim($value)),
63 'type' => 'numeric',
64 'compare' => 'IN'
65 ];
66 }
67
68 if ($key === $category_prefix) {
69
70 $query->query['product_cat'] = '';
71 $query->query_vars['product_cat'] = '';
72 $query = $this->query($key . 'product_cat', $category_prefix, $value, $query);
73
74 } elseif (strpos($key, $color_prefix) !== false) {
75
76 $query = $this->query($key, $color_prefix, $value, $query);
77
78 } elseif (strpos($key, $attribute_prefix) !== false) {
79
80 $query = $this->query($key, $attribute_prefix, $value, $query);
81
82 } elseif (strpos($key, $image_prefix) !== false) {
83
84 $query = $this->query($key, $image_prefix, $value, $query);
85
86 } elseif (strpos($key, $label_prefix) !== false) {
87
88 $query = $this->query($key, $label_prefix, $value, $query);
89
90 } elseif (strpos($key, $shipping_prefix) !== false) {
91
92 $query = $this->query($key, $shipping_prefix, $value, $query);
93
94 } elseif ($key === $stock_prefix) {
95
96 $meta_query[] = [
97 'key' => '_stock_status',
98 'value' => $value,
99 'compare' => 'IN'
100 ];
101
102
103 } elseif ($key === $sale_prefix) {
104
105 $s = explode(',', $value);
106
107 foreach ($s as $v) {
108
109 if ($v === 'on_sale') {
110 $meta_query[] = [
111 'key' => '_sale_price',
112 'value' => 0,
113 'compare' => '>',
114 'type' => 'numeric',
115 'operator' => 'OR',
116 ];
117 } else {
118 $meta_query[] = [
119 'key' => '_sale_price',
120 'compare' => 'NOT EXISTS',
121 'operator' => 'OR',
122 ];
123 }
124 }
125 }
126 }
127
128 if (!empty($meta_query)) {
129 $query->set('meta_query', $meta_query);
130 }
131
132 $query->set('tax_query', $this->custom_query);
133 }
134
135 public function query($key, $prefix, $values, $query)
136 {
137
138 $taxonomy = str_replace($prefix, '', $key);
139
140 $values = explode(',', trim($values));
141
142 $this->custom_query['relation'] = 'AND';
143
144 $this->custom_query[] = [
145 'taxonomy' => $taxonomy,
146 'field' => 'slug',
147 'terms' => $values,
148 'operator' => 'IN',
149 ];
150
151 return $query;
152 }
153 }
154