builders
1 year ago
elementor-controls
2 years ago
export-import
3 years ago
multi-language
3 years ago
onboard
11 months ago
page-templates
9 months ago
register
11 months ago
sample-designs
9 months ago
service-providers
3 years ago
settings
9 months ago
theme-support
3 years ago
wc-customizer
2 years ago
query-modifier.php
9 months ago
service-provider-manager.php
3 years ago
template-cpt.php
2 years ago
query-modifier.php
185 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 | $brand_prefix = 'shopengine_filter_brand'; |
| 66 | |
| 67 | $meta_query = ['relation' => 'AND']; |
| 68 | |
| 69 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here |
| 70 | foreach ($_GET as $key => $value) { |
| 71 | |
| 72 | if ($key === 'rating_filter') { |
| 73 | $meta_query[] = [ |
| 74 | 'key' => '_wc_average_rating', |
| 75 | 'value' => explode(',', trim($value)), |
| 76 | 'type' => 'numeric', |
| 77 | 'compare' => 'IN' |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | if ($key === $category_prefix) { |
| 82 | |
| 83 | $query->query['product_cat'] = ''; |
| 84 | $query->query_vars['product_cat'] = ''; |
| 85 | $query = $this->query($key . 'product_cat', $category_prefix, $value, $query); |
| 86 | |
| 87 | } elseif (strpos($key, $color_prefix) !== false) { |
| 88 | |
| 89 | $query = $this->query($key, $color_prefix, $value, $query); |
| 90 | |
| 91 | } elseif (strpos($key, $attribute_prefix) !== false) { |
| 92 | |
| 93 | $query = $this->query($key, $attribute_prefix, $value, $query); |
| 94 | |
| 95 | } elseif (strpos($key, $image_prefix) !== false) { |
| 96 | |
| 97 | $query = $this->query($key, $image_prefix, $value, $query); |
| 98 | |
| 99 | } elseif (strpos($key, $label_prefix) !== false) { |
| 100 | |
| 101 | $query = $this->query($key, $label_prefix, $value, $query); |
| 102 | |
| 103 | } elseif (strpos($key, $shipping_prefix) !== false) { |
| 104 | |
| 105 | $query = $this->query($key, $shipping_prefix, $value, $query); |
| 106 | |
| 107 | } elseif ($key === $brand_prefix) { |
| 108 | |
| 109 | $query = $this->query($key, $brand_prefix, $value, $query); |
| 110 | |
| 111 | } elseif ($key === $stock_prefix) { |
| 112 | |
| 113 | $meta_query[] = [ |
| 114 | 'key' => '_stock_status', |
| 115 | 'value' => $value, |
| 116 | 'compare' => 'IN' |
| 117 | ]; |
| 118 | |
| 119 | |
| 120 | } elseif ($key === $sale_prefix) { |
| 121 | |
| 122 | $s = explode(',', $value); |
| 123 | |
| 124 | foreach ($s as $v) { |
| 125 | |
| 126 | if ($v === 'on_sale') { |
| 127 | |
| 128 | $product_ids_on_sale = wc_get_product_ids_on_sale(); // including varriation products |
| 129 | $query->set( 'post__in', (array) $product_ids_on_sale ); |
| 130 | |
| 131 | } else { |
| 132 | $meta_query[] = [ |
| 133 | 'key' => '_sale_price', |
| 134 | 'compare' => 'NOT EXISTS', |
| 135 | 'operator' => 'OR', |
| 136 | ]; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (!empty($meta_query)) { |
| 143 | $query->set('meta_query', $meta_query); |
| 144 | } |
| 145 | |
| 146 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 147 | $product_visibility_not_in = array( $product_visibility_terms['exclude-from-catalog'] ); |
| 148 | |
| 149 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 150 | $product_visibility_not_in[] = $product_visibility_terms['outofstock']; |
| 151 | } |
| 152 | $this->custom_query['tax_query'][] = apply_filters('shopengine-product-visibility-modifier',[ |
| 153 | 'taxonomy' => 'product_visibility', |
| 154 | 'terms' => $product_visibility_not_in, |
| 155 | 'field' => 'term_taxonomy_id', |
| 156 | 'operator' => 'NOT IN', |
| 157 | ]); |
| 158 | |
| 159 | $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query)); |
| 160 | } |
| 161 | |
| 162 | public function query($key, $prefix, $values, $query) |
| 163 | { |
| 164 | // Handle brand filter specifically |
| 165 | if ($prefix === 'shopengine_filter_brand') { |
| 166 | $taxonomy = 'product_brand'; |
| 167 | } else { |
| 168 | $taxonomy = str_replace($prefix, '', $key); |
| 169 | } |
| 170 | |
| 171 | $values = explode(',', trim($values)); |
| 172 | |
| 173 | $this->custom_query['relation'] = 'AND'; |
| 174 | |
| 175 | $this->custom_query[] = [ |
| 176 | 'taxonomy' => $taxonomy, |
| 177 | 'field' => 'slug', |
| 178 | 'terms' => $values, |
| 179 | 'operator' => 'IN', |
| 180 | ]; |
| 181 | |
| 182 | return $query; |
| 183 | } |
| 184 | } |
| 185 |