builders
3 years ago
elementor-controls
3 years ago
export-import
3 years ago
onboard
3 years ago
page-templates
3 years ago
register
3 years ago
sample-designs
3 years ago
service-providers
3 years ago
settings
3 years ago
theme-support
3 years ago
query-modifier.php
3 years ago
service-provider-manager.php
3 years ago
template-cpt.php
3 years ago
query-modifier.php
160 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 | if (is_admin() || !$query->is_main_query() || $query->is_single === true) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if (!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | // query filter begins |
| 31 | |
| 32 | // update query for product per page filter |
| 33 | if (!empty($_GET['shopengine_products_per_page'])) { |
| 34 | $query->set('posts_per_page', absint($_GET['shopengine_products_per_page'])); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | $color_prefix = 'shopengine_filter_color_'; |
| 39 | |
| 40 | $attribute_prefix = 'shopengine_filter_attribute_'; |
| 41 | |
| 42 | $image_prefix = 'shopengine_filter_image_'; |
| 43 | |
| 44 | $label_prefix = 'shopengine_filter_label_'; |
| 45 | |
| 46 | $shipping_prefix = 'shopengine_filter_shipping_'; |
| 47 | |
| 48 | $category_prefix = 'shopengine_filter_category'; |
| 49 | |
| 50 | $stock_prefix = 'shopengine_filter_stock'; |
| 51 | |
| 52 | $sale_prefix = 'shopengine_filter_onsale'; |
| 53 | |
| 54 | $meta_query = ['relation' => 'AND']; |
| 55 | |
| 56 | foreach ($_GET as $key => $value) { |
| 57 | |
| 58 | if ($key === 'rating_filter') { |
| 59 | $meta_query[] = [ |
| 60 | 'key' => '_wc_average_rating', |
| 61 | 'value' => explode(',', trim($value)), |
| 62 | 'type' => 'numeric', |
| 63 | 'compare' => 'IN' |
| 64 | ]; |
| 65 | } |
| 66 | |
| 67 | if ($key === $category_prefix) { |
| 68 | |
| 69 | $query->query['product_cat'] = ''; |
| 70 | $query->query_vars['product_cat'] = ''; |
| 71 | $query = $this->query($key . 'product_cat', $category_prefix, $value, $query); |
| 72 | |
| 73 | } elseif (strpos($key, $color_prefix) !== false) { |
| 74 | |
| 75 | $query = $this->query($key, $color_prefix, $value, $query); |
| 76 | |
| 77 | } elseif (strpos($key, $attribute_prefix) !== false) { |
| 78 | |
| 79 | $query = $this->query($key, $attribute_prefix, $value, $query); |
| 80 | |
| 81 | } elseif (strpos($key, $image_prefix) !== false) { |
| 82 | |
| 83 | $query = $this->query($key, $image_prefix, $value, $query); |
| 84 | |
| 85 | } elseif (strpos($key, $label_prefix) !== false) { |
| 86 | |
| 87 | $query = $this->query($key, $label_prefix, $value, $query); |
| 88 | |
| 89 | } elseif (strpos($key, $shipping_prefix) !== false) { |
| 90 | |
| 91 | $query = $this->query($key, $shipping_prefix, $value, $query); |
| 92 | |
| 93 | } elseif ($key === $stock_prefix) { |
| 94 | |
| 95 | $meta_query[] = [ |
| 96 | 'key' => '_stock_status', |
| 97 | 'value' => $value, |
| 98 | 'compare' => 'IN' |
| 99 | ]; |
| 100 | |
| 101 | |
| 102 | } elseif ($key === $sale_prefix) { |
| 103 | |
| 104 | $s = explode(',', $value); |
| 105 | |
| 106 | foreach ($s as $v) { |
| 107 | |
| 108 | if ($v === 'on_sale') { |
| 109 | $meta_query[] = [ |
| 110 | 'key' => '_sale_price', |
| 111 | 'value' => 0, |
| 112 | 'compare' => '>', |
| 113 | 'type' => 'numeric', |
| 114 | 'operator' => 'OR', |
| 115 | ]; |
| 116 | } else { |
| 117 | $meta_query[] = [ |
| 118 | 'key' => '_sale_price', |
| 119 | 'compare' => 'NOT EXISTS', |
| 120 | 'operator' => 'OR', |
| 121 | ]; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (!empty($meta_query)) { |
| 128 | $query->set('meta_query', $meta_query); |
| 129 | } |
| 130 | |
| 131 | $this->custom_query[] = [ |
| 132 | 'taxonomy' => 'product_visibility', |
| 133 | 'terms' => [ 'exclude-from-catalog' ], |
| 134 | 'field' => 'name', |
| 135 | 'operator' => 'NOT IN', |
| 136 | ]; |
| 137 | |
| 138 | $query->set('tax_query', $this->custom_query); |
| 139 | } |
| 140 | |
| 141 | public function query($key, $prefix, $values, $query) |
| 142 | { |
| 143 | |
| 144 | $taxonomy = str_replace($prefix, '', $key); |
| 145 | |
| 146 | $values = explode(',', trim($values)); |
| 147 | |
| 148 | $this->custom_query['relation'] = 'AND'; |
| 149 | |
| 150 | $this->custom_query[] = [ |
| 151 | 'taxonomy' => $taxonomy, |
| 152 | 'field' => 'slug', |
| 153 | 'terms' => $values, |
| 154 | 'operator' => 'IN', |
| 155 | ]; |
| 156 | |
| 157 | return $query; |
| 158 | } |
| 159 | } |
| 160 |