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 |