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
settings
4 years ago
query-modifier.php
4 years ago
template-cpt.php
4 years ago
query-modifier.php
146 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 | public function init() { |
| 13 | |
| 14 | add_action('pre_get_posts', [$this, 'modify_query']); |
| 15 | } |
| 16 | |
| 17 | public function modify_query($query) { |
| 18 | |
| 19 | if(is_admin() || !$query->is_main_query() || $query->is_single === true) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | if(!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | // query filter begins |
| 28 | |
| 29 | // update query for product per page filter |
| 30 | if(!empty($_GET['shopengine_products_per_page'])) { |
| 31 | $query->set('posts_per_page', absint($_GET['shopengine_products_per_page'])); |
| 32 | } |
| 33 | |
| 34 | //update query if query param has color & attribute filter |
| 35 | foreach($_GET as $key => $value) { |
| 36 | |
| 37 | $color_prefix = 'shopengine_filter_color_'; |
| 38 | |
| 39 | $attribute_prefix = 'shopengine_filter_attribute_'; |
| 40 | |
| 41 | $image_prefix = 'shopengine_filter_image_'; |
| 42 | |
| 43 | $label_prefix = 'shopengine_filter_label_'; |
| 44 | |
| 45 | $shipping_prefix = 'shopengine_filter_shipping_'; |
| 46 | |
| 47 | $category_prefix = 'shopengine_filter_category'; |
| 48 | |
| 49 | $stock_prefix = 'shopengine_filter_stock'; |
| 50 | |
| 51 | $sale_prefix ='shopengine_filter_onsale'; |
| 52 | |
| 53 | if( $key === $category_prefix ) { |
| 54 | |
| 55 | // The archive category will be removed form query if users filter by category |
| 56 | $query->query['product_cat'] = ''; |
| 57 | $query->query_vars['product_cat'] = ''; |
| 58 | |
| 59 | $query = $this->query($key.'product_cat', $category_prefix, $value, $query); |
| 60 | |
| 61 | } elseif( strpos($key, $color_prefix) !== false) { |
| 62 | |
| 63 | $query = $this->query($key, $color_prefix, $value, $query); |
| 64 | |
| 65 | } elseif( strpos($key, $attribute_prefix) !== false ) { |
| 66 | |
| 67 | $query = $this->query($key, $attribute_prefix, $value, $query); |
| 68 | |
| 69 | } elseif( strpos($key, $image_prefix) !== false ) { |
| 70 | |
| 71 | $query = $this->query($key, $image_prefix, $value, $query); |
| 72 | |
| 73 | } elseif( strpos($key, $label_prefix) !== false ) { |
| 74 | |
| 75 | $query = $this->query($key, $label_prefix, $value, $query); |
| 76 | |
| 77 | } elseif( strpos($key, $shipping_prefix) !== false ) { |
| 78 | |
| 79 | $query = $this->query($key, $shipping_prefix, $value, $query); |
| 80 | |
| 81 | } elseif( $key === $stock_prefix ) { |
| 82 | |
| 83 | $query->set('meta_query', |
| 84 | [ |
| 85 | [ |
| 86 | 'key' => '_stock_status', |
| 87 | 'value' => $value, |
| 88 | 'compare' => 'IN' |
| 89 | ] |
| 90 | ] |
| 91 | ); |
| 92 | |
| 93 | } elseif( $key === $sale_prefix ) { |
| 94 | |
| 95 | $s = explode(',', $value); |
| 96 | $sale_query = ['relation' => 'OR']; |
| 97 | |
| 98 | foreach($s as $v) { |
| 99 | |
| 100 | if($v === 'on_sale') { |
| 101 | $sale_query[] = [ |
| 102 | 'key' => '_sale_price', |
| 103 | 'value' => 0, |
| 104 | 'compare' => '>', |
| 105 | 'type' => 'numeric', |
| 106 | 'operator' => 'OR', |
| 107 | ]; |
| 108 | } else { |
| 109 | $sale_query[] = [ |
| 110 | 'key' => '_sale_price', |
| 111 | 'compare' => 'NOT EXISTS', |
| 112 | 'operator' => 'OR', |
| 113 | ]; |
| 114 | } |
| 115 | } |
| 116 | $query->set('meta_query', $sale_query); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public function query($key, $prefix, $values, $query) { |
| 122 | |
| 123 | $taxonomy = str_replace($prefix, '', $key); |
| 124 | $query_array = ['relation' => 'AND']; |
| 125 | |
| 126 | if('product_cat' === $taxonomy) { |
| 127 | $query_array = ['relation' => 'OR']; |
| 128 | } |
| 129 | |
| 130 | $values = explode(',', $values); |
| 131 | |
| 132 | foreach($values as $value) { |
| 133 | $query_array[] = [ |
| 134 | 'taxonomy' => $taxonomy, |
| 135 | 'field' => 'slug', |
| 136 | 'terms' => $value, |
| 137 | 'operator' => 'IN', |
| 138 | ]; |
| 139 | } |
| 140 | |
| 141 | $query->set('tax_query', $query_array); |
| 142 | |
| 143 | return $query; |
| 144 | } |
| 145 | } |
| 146 |