builders
1 year ago
elementor-controls
2 years ago
export-import
3 years ago
multi-language
3 years ago
onboard
3 years ago
page-templates
1 year ago
register
2 years ago
sample-designs
2 years ago
service-providers
3 years ago
settings
2 years ago
theme-support
3 years ago
wc-customizer
2 years ago
query-modifier.php
1 year ago
service-provider-manager.php
3 years ago
template-cpt.php
2 years ago
query-modifier.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | use ShopEngine\Core\Register\Widget_List; |
| 7 | |
| 8 | class Query_Modifier |
| 9 | { |
| 10 | |
| 11 | use Singleton; |
| 12 | |
| 13 | private $custom_query = []; |
| 14 | |
| 15 | public function init() |
| 16 | { |
| 17 | |
| 18 | add_action('pre_get_posts', [$this, 'modify_query']); |
| 19 | } |
| 20 | |
| 21 | public function modify_query($query) |
| 22 | { |
| 23 | |
| 24 | |
| 25 | if (is_admin() || !$query->is_main_query() || $query->is_single === true) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | if (!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | // query filter begins |
| 34 | |
| 35 | // update query for product per page filter |
| 36 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here |
| 37 | if (!empty($_GET['shopengine_products_per_page'])) { |
| 38 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 39 | $query->set('posts_per_page', absint(intval($_GET['shopengine_products_per_page']))); |
| 40 | } |
| 41 | |
| 42 | // checking product filter widget active or not |
| 43 | $active_widgets = Widget_List::instance()->get_list(true, 'active'); |
| 44 | if (!isset($active_widgets['product-filters'])) { |
| 45 | |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | $color_prefix = 'shopengine_filter_color_'; |
| 50 | |
| 51 | $attribute_prefix = 'shopengine_filter_attribute_'; |
| 52 | |
| 53 | $image_prefix = 'shopengine_filter_image_'; |
| 54 | |
| 55 | $label_prefix = 'shopengine_filter_label_'; |
| 56 | |
| 57 | $shipping_prefix = 'shopengine_filter_shipping_'; |
| 58 | |
| 59 | $category_prefix = 'shopengine_filter_category'; |
| 60 | |
| 61 | $stock_prefix = 'shopengine_filter_stock'; |
| 62 | |
| 63 | $sale_prefix = 'shopengine_filter_onsale'; |
| 64 | |
| 65 | $meta_query = ['relation' => 'AND']; |
| 66 | |
| 67 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here |
| 68 | foreach ($_GET as $key => $value) { |
| 69 | |
| 70 | if ($key === 'rating_filter') { |
| 71 | $meta_query[] = [ |
| 72 | 'key' => '_wc_average_rating', |
| 73 | 'value' => explode(',', trim($value)), |
| 74 | 'type' => 'numeric', |
| 75 | 'compare' => 'IN' |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | if ($key === $category_prefix) { |
| 80 | |
| 81 | $query->query['product_cat'] = ''; |
| 82 | $query->query_vars['product_cat'] = ''; |
| 83 | $query = $this->query($key . 'product_cat', $category_prefix, $value, $query); |
| 84 | |
| 85 | } elseif (strpos($key, $color_prefix) !== false) { |
| 86 | |
| 87 | $query = $this->query($key, $color_prefix, $value, $query); |
| 88 | |
| 89 | } elseif (strpos($key, $attribute_prefix) !== false) { |
| 90 | |
| 91 | $query = $this->query($key, $attribute_prefix, $value, $query); |
| 92 | |
| 93 | } elseif (strpos($key, $image_prefix) !== false) { |
| 94 | |
| 95 | $query = $this->query($key, $image_prefix, $value, $query); |
| 96 | |
| 97 | } elseif (strpos($key, $label_prefix) !== false) { |
| 98 | |
| 99 | $query = $this->query($key, $label_prefix, $value, $query); |
| 100 | |
| 101 | } elseif (strpos($key, $shipping_prefix) !== false) { |
| 102 | |
| 103 | $query = $this->query($key, $shipping_prefix, $value, $query); |
| 104 | |
| 105 | } elseif ($key === $stock_prefix) { |
| 106 | |
| 107 | $meta_query[] = [ |
| 108 | 'key' => '_stock_status', |
| 109 | 'value' => $value, |
| 110 | 'compare' => 'IN' |
| 111 | ]; |
| 112 | |
| 113 | |
| 114 | } elseif ($key === $sale_prefix) { |
| 115 | |
| 116 | $s = explode(',', $value); |
| 117 | |
| 118 | foreach ($s as $v) { |
| 119 | |
| 120 | if ($v === 'on_sale') { |
| 121 | |
| 122 | $product_ids_on_sale = wc_get_product_ids_on_sale(); // including varriation products |
| 123 | $query->set( 'post__in', (array) $product_ids_on_sale ); |
| 124 | |
| 125 | } else { |
| 126 | $meta_query[] = [ |
| 127 | 'key' => '_sale_price', |
| 128 | 'compare' => 'NOT EXISTS', |
| 129 | 'operator' => 'OR', |
| 130 | ]; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!empty($meta_query)) { |
| 137 | $query->set('meta_query', $meta_query); |
| 138 | } |
| 139 | |
| 140 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 141 | $product_visibility_not_in = array( $product_visibility_terms['exclude-from-catalog'] ); |
| 142 | |
| 143 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 144 | $product_visibility_not_in[] = $product_visibility_terms['outofstock']; |
| 145 | } |
| 146 | $this->custom_query['tax_query'][] = apply_filters('shopengine-product-visibility-modifier',[ |
| 147 | 'taxonomy' => 'product_visibility', |
| 148 | 'terms' => $product_visibility_not_in, |
| 149 | 'field' => 'term_taxonomy_id', |
| 150 | 'operator' => 'NOT IN', |
| 151 | ]); |
| 152 | |
| 153 | $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query)); |
| 154 | } |
| 155 | |
| 156 | public function query($key, $prefix, $values, $query) |
| 157 | { |
| 158 | |
| 159 | $taxonomy = str_replace($prefix, '', $key); |
| 160 | |
| 161 | $values = explode(',', trim($values)); |
| 162 | |
| 163 | $this->custom_query['relation'] = 'AND'; |
| 164 | |
| 165 | $this->custom_query[] = [ |
| 166 | 'taxonomy' => $taxonomy, |
| 167 | 'field' => 'slug', |
| 168 | 'terms' => $values, |
| 169 | 'operator' => 'IN', |
| 170 | ]; |
| 171 | |
| 172 | return $query; |
| 173 | } |
| 174 | } |
| 175 |