builders
4 years ago
elementor-controls
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
5 years ago
query-modifier.php
95 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 filter |
| 35 | if(!empty($_GET['shopengine_filter_color'])) { |
| 36 | |
| 37 | $dt = (array)json_decode(stripcslashes($_GET['shopengine_filter_color'])); |
| 38 | |
| 39 | if(!empty($dt) && is_array($dt)) { |
| 40 | foreach($dt as $pa_name => $vals) { |
| 41 | $query->set( |
| 42 | 'tax_query', |
| 43 | [ |
| 44 | 'relation' => 'OR', |
| 45 | [ |
| 46 | 'taxonomy' => $pa_name, |
| 47 | 'field' => 'slug', |
| 48 | 'terms' => $vals, |
| 49 | 'operator' => 'IN', |
| 50 | ], |
| 51 | ] |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | //update query if query param has size filter |
| 58 | if(!empty($_GET['shopengine_filter_size'])) { |
| 59 | |
| 60 | $size_values = explode(',', $_GET['shopengine_filter_size']); |
| 61 | |
| 62 | if( is_array($size_values) && !empty($size_values) ){ |
| 63 | $query->set('tax_query', [ |
| 64 | [ |
| 65 | 'taxonomy' => 'pa_size', |
| 66 | 'field' => 'slug', |
| 67 | 'terms' => $size_values, |
| 68 | 'operator' => 'IN', |
| 69 | ] |
| 70 | ]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | //update query if query param has category filter |
| 75 | if(!empty($_GET['shopengine_filter_category'])) { |
| 76 | |
| 77 | $category_values = explode(',', $_GET['shopengine_filter_category']); |
| 78 | |
| 79 | if( is_array($category_values) && !empty($category_values) ){ |
| 80 | $query->set('tax_query', [ |
| 81 | [ |
| 82 | 'taxonomy' => 'product_cat', |
| 83 | 'field' => 'slug', |
| 84 | 'terms' => $category_values, |
| 85 | 'operator' => 'IN', |
| 86 | ] |
| 87 | ]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // query filter ends |
| 93 | } |
| 94 | } |
| 95 |