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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-query.php
woocommerce / includes Last commit date
abstracts 4 weeks ago admin 4 days ago blocks 11 months ago cli 4 weeks ago customizer 4 months ago data-stores 4 weeks ago emails 4 days ago export 1 year ago gateways 4 weeks ago import 3 months ago integrations 2 months ago interfaces 4 months ago legacy 4 months ago libraries 4 weeks ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 months ago react-admin 4 weeks ago rest-api 4 weeks ago shipping 3 months ago shortcodes 4 weeks ago theme-support 2 years ago tracks 4 weeks ago traits 5 years ago walkers 5 years ago wccom-site 4 weeks ago widgets 2 months ago class-wc-ajax.php 4 weeks ago class-wc-auth.php 1 year ago class-wc-autoloader.php 9 months ago class-wc-background-emailer.php 2 months ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 4 weeks ago class-wc-breadcrumb.php 4 months ago class-wc-cache-helper.php 4 weeks ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 3 months ago class-wc-cart-totals.php 11 months ago class-wc-cart.php 4 weeks ago class-wc-checkout.php 4 weeks ago class-wc-cli.php 10 months ago class-wc-comments.php 4 months ago class-wc-countries.php 2 months ago class-wc-coupon.php 2 months ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 4 weeks ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 months ago class-wc-discounts.php 11 months ago class-wc-download-handler.php 4 weeks ago class-wc-emails.php 4 weeks ago class-wc-embed.php 1 year ago class-wc-form-handler.php 3 months ago class-wc-frontend-scripts.php 2 months ago class-wc-geo-ip.php 9 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 2 months ago class-wc-https.php 3 years ago class-wc-install.php 4 days ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 4 months ago class-wc-meta-data.php 4 weeks ago class-wc-order-factory.php 2 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 5 months ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 4 weeks ago class-wc-order-item-shipping.php 5 months ago class-wc-order-item-tax.php 4 weeks ago class-wc-order-item.php 4 weeks ago class-wc-order-query.php 4 weeks ago class-wc-order-refund.php 1 year ago class-wc-order.php 4 weeks ago class-wc-payment-gateways.php 4 weeks ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 2 months ago class-wc-post-types.php 4 weeks ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 10 months ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 months ago class-wc-product-download.php 4 months ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 3 months ago class-wc-product-grouped.php 3 months ago class-wc-product-query.php 4 months ago class-wc-product-simple.php 11 months ago class-wc-product-variable.php 4 weeks ago class-wc-product-variation.php 4 weeks ago class-wc-query.php 4 weeks ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 2 years ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 2 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 3 months ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 7 months ago class-wc-shipping.php 2 months ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 4 weeks ago class-wc-tax.php 2 months ago class-wc-template-loader.php 7 months ago class-wc-tracker.php 4 weeks ago class-wc-validation.php 2 years ago class-wc-webhook.php 4 weeks ago class-woocommerce.php 4 days ago wc-account-functions.php 4 weeks ago wc-attribute-functions.php 4 weeks ago wc-brands-functions.php 1 year ago wc-cart-functions.php 4 weeks ago wc-conditional-functions.php 11 months ago wc-core-functions.php 2 months ago wc-coupon-functions.php 5 months ago wc-deprecated-functions.php 4 weeks ago wc-formatting-functions.php 4 weeks ago wc-interactivity-api-functions.php 2 months ago wc-notice-functions.php 5 months ago wc-order-functions.php 1 month ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 4 months ago wc-page-functions.php 1 month ago wc-product-functions.php 2 months ago wc-rest-functions.php 7 months ago wc-stock-functions.php 7 months ago wc-template-functions.php 4 weeks ago wc-template-hooks.php 10 months ago wc-term-functions.php 2 months ago wc-update-functions.php 4 days ago wc-user-functions.php 4 weeks ago wc-webhook-functions.php 4 weeks ago wc-widget-functions.php 5 years ago
class-wc-query.php
1163 lines
1 <?php
2 /**
3 * Contains the query functions for WooCommerce which alter the front-end post queries and loops
4 *
5 * @version 3.2.0
6 * @package WooCommerce\Classes
7 */
8
9 use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer;
10 use Automattic\WooCommerce\Enums\ProductStockStatus;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * WC_Query Class.
16 */
17 class WC_Query {
18
19 /**
20 * Query vars to add to wp.
21 *
22 * @var array
23 */
24 public $query_vars = array();
25
26 /**
27 * Reference to the main product query on the page.
28 *
29 * @var WP_Query
30 */
31 private static $product_query;
32
33 /**
34 * Stores chosen attributes.
35 *
36 * @var array
37 */
38 private static $chosen_attributes;
39
40 /**
41 * The instance of the class that helps filtering with the product attributes lookup table.
42 *
43 * @var Filterer
44 */
45 private $filterer;
46
47 /**
48 * Constructor for the query class. Hooks in methods.
49 */
50 public function __construct() {
51 $this->filterer = wc_get_container()->get( Filterer::class );
52
53 add_action( 'init', array( $this, 'add_endpoints' ) );
54 if ( ! is_admin() ) {
55 add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 );
56 add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
57 add_action( 'parse_request', array( $this, 'parse_request' ), 0 );
58 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
59 add_filter( 'get_pagenum_link', array( $this, 'remove_add_to_cart_pagination' ), 10, 1 );
60 }
61 $this->init_query_vars();
62 }
63
64 /**
65 * Reset the chosen attributes so that get_layered_nav_chosen_attributes will get them from the query again.
66 */
67 public static function reset_chosen_attributes() {
68 self::$chosen_attributes = null;
69 }
70
71 /**
72 * Get any errors from querystring.
73 */
74 public function get_errors() {
75 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
76 $error = ! empty( $_GET['wc_error'] ) ? sanitize_text_field( wp_unslash( $_GET['wc_error'] ) ) : '';
77
78 if ( $error && ! wc_has_notice( $error, 'error' ) ) {
79 wc_add_notice( $error, 'error' );
80 }
81 }
82
83 /**
84 * Init query vars by loading options.
85 */
86 public function init_query_vars() {
87 // Query vars to add to WP.
88 $this->query_vars = array(
89 // Checkout actions.
90 'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
91 'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
92 // My account actions.
93 'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ),
94 'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
95 'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ),
96 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
97 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
98 'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ),
99 'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
100 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
101 'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
102 'delete-payment-method' => get_option( 'woocommerce_myaccount_delete_payment_method_endpoint', 'delete-payment-method' ),
103 'set-default-payment-method' => get_option( 'woocommerce_myaccount_set_default_payment_method_endpoint', 'set-default-payment-method' ),
104 );
105 }
106
107 /**
108 * Get page title for an endpoint.
109 *
110 * @param string $endpoint Endpoint key.
111 * @param string $action Optional action or variation within the endpoint.
112 *
113 * @since 2.3.0
114 * @since 4.6.0 Added $action parameter.
115 * @return string The page title.
116 */
117 public function get_endpoint_title( $endpoint, $action = '' ) {
118 global $wp;
119
120 switch ( $endpoint ) {
121 case 'order-pay':
122 $title = __( 'Pay for order', 'woocommerce' );
123 break;
124 case 'order-received':
125 $title = __( 'Order received', 'woocommerce' );
126 break;
127 case 'orders':
128 if ( ! empty( $wp->query_vars['orders'] ) ) {
129 /* translators: %s: page */
130 $title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) );
131 } else {
132 $title = __( 'Orders', 'woocommerce' );
133 }
134 break;
135 case 'view-order':
136 $order = wc_get_order( $wp->query_vars['view-order'] );
137 /* translators: %s: order number */
138 $title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
139 break;
140 case 'downloads':
141 $title = __( 'Downloads', 'woocommerce' );
142 break;
143 case 'edit-account':
144 $title = __( 'Account details', 'woocommerce' );
145 break;
146 case 'edit-address':
147 $title = __( 'Addresses', 'woocommerce' );
148 break;
149 case 'payment-methods':
150 $title = __( 'Payment methods', 'woocommerce' );
151 break;
152 case 'add-payment-method':
153 $title = __( 'Add payment method', 'woocommerce' );
154 break;
155 case 'lost-password':
156 if ( in_array( $action, array( 'rp', 'resetpass', 'newaccount' ), true ) ) {
157 $title = __( 'Set password', 'woocommerce' );
158 } else {
159 $title = __( 'Lost password', 'woocommerce' );
160 }
161 break;
162 default:
163 $title = '';
164 break;
165 }
166
167 /**
168 * Filters the page title used for my-account endpoints.
169 *
170 * @since 2.6.0
171 * @since 4.6.0 Added $action parameter.
172 *
173 * @see get_endpoint_title()
174 *
175 * @param string $title Default title.
176 * @param string $endpoint Endpoint key.
177 * @param string $action Optional action or variation within the endpoint.
178 */
179 return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint, $action );
180 }
181
182 /**
183 * Endpoint mask describing the places the endpoint should be added.
184 *
185 * @since 2.6.2
186 * @return int
187 */
188 public function get_endpoints_mask() {
189 if ( 'page' === get_option( 'show_on_front' ) ) {
190 $page_on_front = get_option( 'page_on_front' );
191 $myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
192 $checkout_page_id = get_option( 'woocommerce_checkout_page_id' );
193
194 if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ), true ) ) {
195 return EP_ROOT | EP_PAGES;
196 }
197 }
198
199 return EP_PAGES;
200 }
201
202 /**
203 * Add endpoints for query vars.
204 */
205 public function add_endpoints() {
206 $mask = $this->get_endpoints_mask();
207
208 foreach ( $this->get_query_vars() as $key => $var ) {
209 if ( ! empty( $var ) ) {
210 add_rewrite_endpoint( $var, $mask );
211 }
212 }
213 }
214
215 /**
216 * Add query vars.
217 *
218 * @param array $vars Query vars.
219 * @return array
220 */
221 public function add_query_vars( $vars ) {
222 foreach ( $this->get_query_vars() as $key => $var ) {
223 $vars[] = $key;
224 }
225 return $vars;
226 }
227
228 /**
229 * Get query vars.
230 *
231 * @return array
232 */
233 public function get_query_vars() {
234 return apply_filters( 'woocommerce_get_query_vars', $this->query_vars );
235 }
236
237 /**
238 * Get query current active query var.
239 *
240 * @return string
241 */
242 public function get_current_endpoint() {
243 global $wp;
244
245 foreach ( $this->get_query_vars() as $key => $value ) {
246 if ( isset( $wp->query_vars[ $key ] ) ) {
247 return $key;
248 }
249 }
250 return '';
251 }
252
253 /**
254 * Parse the request and look for query vars - endpoints may not be supported.
255 */
256 public function parse_request() {
257 global $wp;
258
259 // phpcs:disable WordPress.Security.NonceVerification.Recommended
260 // Map query vars to their keys, or get them if endpoints are not supported.
261 foreach ( $this->get_query_vars() as $key => $var ) {
262 if ( isset( $_GET[ $var ] ) ) {
263 $wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) );
264 } elseif ( isset( $wp->query_vars[ $var ] ) ) {
265 $wp->query_vars[ $key ] = $wp->query_vars[ $var ];
266 }
267 }
268 // phpcs:enable WordPress.Security.NonceVerification.Recommended
269 }
270
271 /**
272 * Are we currently on the front page?
273 *
274 * @param WP_Query $q Query instance.
275 * @return bool
276 */
277 private function is_showing_page_on_front( $q ) {
278 return ( $q->is_home() && ! $q->is_posts_page ) && 'page' === get_option( 'show_on_front' );
279 }
280
281 /**
282 * Is the front page a page we define?
283 *
284 * @param int $page_id Page ID.
285 * @return bool
286 */
287 private function page_on_front_is( $page_id ) {
288 return absint( get_option( 'page_on_front' ) ) === absint( $page_id );
289 }
290
291 /**
292 * Returns a copy of `$query` with all query vars that are allowed on the front page stripped.
293 * Used when the shop page is also the front page.
294 *
295 * @param array $query The unfiltered array.
296 * @return array The filtered query vars.
297 */
298 private function filter_out_valid_front_page_query_vars( $query ) {
299 return array_filter(
300 $query,
301 function ( $key ) {
302 return ! $this->is_query_var_valid_on_front_page( $key );
303 },
304 ARRAY_FILTER_USE_KEY
305 );
306 }
307
308 /**
309 * Checks whether a query var is allowed on the front page or not.
310 *
311 * @param string $query_var Query var name.
312 * @return boolean TRUE when query var is allowed on the front page. FALSE otherwise.
313 */
314 private function is_query_var_valid_on_front_page( $query_var ) {
315 return in_array( $query_var, array( 'preview', 'page', 'paged', 'cpage', 'orderby' ), true )
316 || in_array( $query_var, array( 'min_price', 'max_price', 'rating_filter' ), true )
317 || 0 === strpos( $query_var, 'filter_' )
318 || 0 === strpos( $query_var, 'query_type_' );
319 }
320
321 /**
322 * Hook into pre_get_posts to do the main product query.
323 *
324 * @param WP_Query $q Query instance.
325 */
326 public function pre_get_posts( $q ) {
327 // We only want to affect the main query.
328 if ( ! $q->is_main_query() ) {
329 return;
330 }
331
332 // Fixes for queries on static homepages.
333 if ( $this->is_showing_page_on_front( $q ) ) {
334
335 // Fix for endpoints on the homepage.
336 if ( ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) {
337 $_query = wp_parse_args( $q->query );
338 if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->get_query_vars() ) ) ) {
339 $q->is_page = true;
340 $q->is_home = false;
341 $q->is_singular = true;
342 $q->set( 'page_id', (int) get_option( 'page_on_front' ) );
343 add_filter( 'redirect_canonical', '__return_false' );
344 }
345 }
346
347 // When orderby is set, WordPress shows posts on the front-page. Get around that here.
348 if ( $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) {
349 $_query = $this->filter_out_valid_front_page_query_vars( wp_parse_args( $q->query ) );
350
351 if ( empty( $_query ) ) {
352 $q->set( 'page_id', (int) get_option( 'page_on_front' ) );
353 $q->is_page = true;
354 $q->is_home = false;
355
356 // WP supporting themes show post type archive.
357 if ( wc_current_theme_supports_woocommerce_or_fse() ) {
358 $q->set( 'post_type', 'product' );
359 } else {
360 $q->is_singular = true;
361 }
362 }
363 } elseif ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
364 $q->set( 'page_id', (int) get_option( 'page_on_front' ) );
365 $q->is_page = true;
366 $q->is_home = false;
367 $q->is_singular = true;
368 }
369 }
370
371 // Fix product feeds.
372 if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) {
373 $q->is_comment_feed = false;
374 }
375
376 // Special check for shops with the PRODUCT POST TYPE ARCHIVE on front.
377 if ( wc_current_theme_supports_woocommerce_or_fse() && $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) {
378 // This is a front-page shop.
379 $q->set( 'post_type', 'product' );
380 $q->set( 'page_id', '' );
381
382 if ( isset( $q->query['paged'] ) ) {
383 $q->set( 'paged', $q->query['paged'] );
384 }
385
386 // Define a variable so we know this is the front page shop later on.
387 wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true );
388
389 // Get the actual WP page to avoid errors and let us use is_front_page().
390 // This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096.
391 global $wp_post_types;
392
393 $shop_page = get_post( wc_get_page_id( 'shop' ) );
394
395 $wp_post_types['product']->ID = $shop_page->ID;
396 $wp_post_types['product']->post_title = $shop_page->post_title;
397 $wp_post_types['product']->post_name = $shop_page->post_name;
398 $wp_post_types['product']->post_type = $shop_page->post_type;
399 $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type );
400
401 // Fix conditional Functions like is_front_page.
402 $q->is_singular = false;
403 $q->is_post_type_archive = true;
404 $q->is_archive = true;
405 $q->is_page = true;
406
407 // Remove post type archive name from front page title tag.
408 add_filter( 'post_type_archive_title', '__return_empty_string', 5 );
409
410 // Fix WP SEO.
411 if ( class_exists( 'WPSEO_Meta' ) ) {
412 add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
413 add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
414 }
415 } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) {
416 // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies.
417 if ( $q->is_search() ) {
418 // Exclude products flagged as hidden from search (catalog_visibility = hidden or catalog).
419 // Applied inline rather than via get_tax_query() to avoid pulling in layered nav filters
420 // and the woocommerce_product_query_tax_query hook, which are scoped to product archives.
421 $product_visibility_terms = wc_get_product_visibility_term_ids();
422 $exclude_term_id = isset( $product_visibility_terms['exclude-from-search'] ) ? (int) $product_visibility_terms['exclude-from-search'] : 0;
423
424 if ( $exclude_term_id > 0 ) {
425 $existing_tax_query = $q->get( 'tax_query' );
426 $existing_tax_query = is_array( $existing_tax_query ) ? $existing_tax_query : array();
427 $existing_tax_query[] = array(
428 'taxonomy' => 'product_visibility',
429 'field' => 'term_taxonomy_id',
430 'terms' => array( $exclude_term_id ),
431 'operator' => 'NOT IN',
432 );
433 $q->set( 'tax_query', $existing_tax_query );
434 }
435 }
436 return;
437 }
438
439 $this->product_query( $q );
440 }
441
442 /**
443 * Handler for the 'the_posts' WP filter.
444 *
445 * @param array $posts Posts from WP Query.
446 * @param WP_Query $query Current query.
447 *
448 * @return array
449 */
450 public function handle_get_posts( $posts, $query ) {
451 if ( 'product_query' !== $query->get( 'wc_query' ) ) {
452 return $posts;
453 }
454 $this->remove_product_query_filters( $posts );
455 return $posts;
456 }
457
458 /**
459 * Prime featured image caches for product queries to avoid individual
460 * queries during rendering.
461 *
462 * @since 10.8.0
463 *
464 * @param array $posts Posts from WP Query.
465 * @param WP_Query $query Current query.
466 * @return array
467 */
468 public function prime_thumbnail_caches( $posts, $query ) {
469 if ( 'product_query' === $query->get( 'wc_query' ) ) {
470 update_post_thumbnail_cache( $query );
471 }
472 return $posts;
473 }
474
475 /**
476 * Pre_get_posts above may adjust the main query to add WooCommerce logic. When this query is done, we need to ensure
477 * all custom filters are removed.
478 *
479 * This is done here during the_posts filter. The input is not changed.
480 *
481 * @param array $posts Posts from WP Query.
482 * @return array
483 */
484 public function remove_product_query_filters( $posts ) {
485 $this->remove_ordering_args();
486 remove_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 );
487 return $posts;
488 }
489
490 /**
491 * This function used to be hooked to found_posts and adjust the posts count when the filtering by attribute
492 * widget was used and variable products were present. Now it isn't hooked anymore and does nothing but return
493 * the input unchanged, since the pull request in which it was introduced has been reverted.
494 *
495 * @since 4.4.0
496 * @param int $count Original posts count, as supplied by the found_posts filter.
497 * @param WP_Query $query The current WP_Query object.
498 *
499 * @return int Adjusted posts count.
500 */
501 public function adjust_posts_count( $count, $query ) {
502 return $count;
503 }
504
505 /**
506 * Instance version of get_layered_nav_chosen_attributes, needed for unit tests.
507 *
508 * @return array
509 */
510 protected function get_layered_nav_chosen_attributes_inst() {
511 return self::get_layered_nav_chosen_attributes();
512 }
513
514 /**
515 * Get the posts (or the ids of the posts) found in the current WP loop.
516 *
517 * @return array Array of posts or post ids.
518 */
519 protected function get_current_posts() {
520 return $GLOBALS['wp_query']->posts;
521 }
522
523 /**
524 * WP SEO meta description.
525 *
526 * Hooked into wpseo_ hook already, so no need for function_exist.
527 *
528 * @return string
529 */
530 public function wpseo_metadesc() {
531 return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) );
532 }
533
534 /**
535 * WP SEO meta key.
536 *
537 * Hooked into wpseo_ hook already, so no need for function_exist.
538 *
539 * @return string
540 */
541 public function wpseo_metakey() {
542 return WPSEO_Meta::get_value( 'metakey', wc_get_page_id( 'shop' ) );
543 }
544
545 /**
546 * Query the products, applying sorting/ordering etc.
547 * This applies to the main WordPress loop.
548 *
549 * @param WP_Query $q Query instance.
550 */
551 public function product_query( $q ) {
552 if ( ! is_feed() ) {
553 $ordering = $this->get_catalog_ordering_args();
554 $q->set( 'orderby', $ordering['orderby'] );
555 $q->set( 'order', $ordering['order'] );
556
557 if ( isset( $ordering['meta_key'] ) ) {
558 $q->set( 'meta_key', $ordering['meta_key'] );
559 }
560 }
561
562 // Query vars that affect posts shown.
563 $q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) );
564 $q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) );
565 $q->set( 'wc_query', 'product_query' );
566 $q->set( 'post__in', array_unique( (array) apply_filters( 'loop_shop_post_in', array() ) ) );
567
568 // Work out how many products to query.
569 $q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ) );
570
571 // Store reference to this query.
572 self::$product_query = $q;
573
574 // Additional hooks to change WP Query.
575 add_filter( 'posts_clauses', array( $this, 'product_query_post_clauses' ), 10, 2 );
576 add_filter( 'the_posts', array( $this, 'handle_get_posts' ), 10, 2 );
577 add_filter( 'the_posts', array( $this, 'prime_thumbnail_caches' ), 10, 2 );
578
579 do_action( 'woocommerce_product_query', $q, $this );
580 }
581
582 /**
583 * Add extra clauses to the product query.
584 *
585 * @param array $args Product query clauses.
586 * @param WP_Query $wp_query The current product query.
587 * @return array The updated product query clauses array.
588 *
589 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
590 */
591 public function product_query_post_clauses( $args, $wp_query ) {
592 $args = $this->price_filter_post_clauses( $args, $wp_query );
593 $args = $this->filterer->filter_by_attribute_post_clauses( $args, $wp_query, self::get_layered_nav_chosen_attributes() );
594
595 return $args;
596 }
597
598 /**
599 * Remove the query.
600 */
601 public function remove_product_query() {
602 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
603 }
604
605 /**
606 * Remove ordering queries.
607 */
608 public function remove_ordering_args() {
609 remove_filter( 'posts_clauses', array( $this, 'order_by_price_asc_post_clauses' ) );
610 remove_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) );
611 remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
612 remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
613 }
614
615 /**
616 * Check whether the current search query contains at least one positive (non-exclusion) term.
617 *
618 * WordPress relevance ordering requires positive search terms to build a valid ORDER BY clause.
619 * Searches that are empty or contain only exclusion terms (e.g. "-condebug") produce no positive
620 * terms, which results in invalid SQL when relevance ordering is used.
621 *
622 * This method delegates tokenization to WP_Query so it correctly handles WordPress's search
623 * term parsing (splitting on spaces, commas, and +) and respects the
624 * wp_query_search_exclusion_prefix filter.
625 *
626 * @return bool
627 */
628 private function has_positive_search_terms(): bool {
629 $search_string = get_query_var( 's' );
630 $search_string = is_array( $search_string ) ? '' : trim( (string) $search_string );
631
632 if ( '' === $search_string ) {
633 return false;
634 }
635
636 // Use WP_Query to parse search terms using core's tokenization rules.
637 $search_query = new class( array( 's' => $search_string ) ) extends \WP_Query {
638 /**
639 * This constructor is overridden to avoid triggering a database query while allowing access to search term parsing routines.
640 * Using public query APIs such as the `parse_query` method leads to test regressions, so an anonymous class approach is used instead.
641 *
642 * @param string|array $query URL query string or array of vars.
643 */
644 public function __construct( $query = '' ) {
645 $this->query_vars = (array) $query;
646 $this->parse_search( $this->query_vars );
647 }
648 };
649 $search_terms = $search_query->query_vars['search_terms'] ?? array();
650
651 if ( empty( $search_terms ) ) {
652 return false;
653 }
654
655 /** This filter is documented in wp-includes/class-wp-query.php */
656 $exclusion_prefix = (string) apply_filters( 'wp_query_search_exclusion_prefix', '-' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment
657
658 if ( '' !== $exclusion_prefix ) {
659 $search_terms = array_filter(
660 $search_terms,
661 static fn( $term ) => ! str_starts_with( $term, $exclusion_prefix )
662 );
663 }
664
665 return ! empty( $search_terms );
666 }
667
668 /**
669 * Returns an array of arguments for ordering products based on the selected values.
670 *
671 * @param string $orderby Order by param.
672 * @param string $order Order param.
673 * @return array
674 */
675 public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
676 // Get ordering from query string unless defined.
677 if ( ! $orderby ) {
678 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
679 if ( isset( $_GET['orderby'] ) ) {
680 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
681 $orderby_value = wc_clean( wp_unslash( $_GET['orderby'] ) );
682 if ( is_array( $orderby_value ) ) {
683 $orderby_value = $orderby_value[0];
684 }
685 } else {
686 $orderby_value = wc_clean( get_query_var( 'orderby' ) );
687 }
688
689 if ( ! $orderby_value ) {
690 if ( is_search() && $this->has_positive_search_terms() ) {
691 $orderby_value = 'relevance';
692 } else {
693 $orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );
694 }
695 }
696
697 // Get order + orderby args from string.
698 $orderby_value = is_array( $orderby_value ) ? $orderby_value : explode( '-', $orderby_value );
699 $orderby = esc_attr( $orderby_value[0] );
700 $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
701 }
702
703 // Convert to correct format.
704 $orderby = strtolower( is_array( $orderby ) ? (string) current( $orderby ) : (string) $orderby );
705 // ID can already be added and requires to be capitalized.
706 $orderby = str_replace( ' id', ' ID', $orderby );
707 $order = strtoupper( is_array( $order ) ? (string) current( $order ) : (string) $order );
708
709 $args = array(
710 'orderby' => $orderby,
711 'order' => ( 'DESC' === $order ) ? 'DESC' : 'ASC',
712 'meta_key' => '', // @codingStandardsIgnoreLine
713 );
714
715 switch ( $orderby ) {
716 case 'id':
717 $args['orderby'] = 'ID';
718 break;
719 case 'menu_order':
720 $args['orderby'] = 'menu_order title';
721 break;
722 case 'title':
723 $args['orderby'] = 'title';
724 $args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
725 break;
726 case 'relevance':
727 $args['orderby'] = 'relevance';
728 $args['order'] = 'DESC';
729 break;
730 case 'rand':
731 $args['orderby'] = 'rand'; // @codingStandardsIgnoreLine
732 break;
733 case 'modified':
734 case 'date':
735 $args['orderby'] = $orderby . ' ID';
736 $args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
737 break;
738 case 'price':
739 $callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses';
740 add_filter( 'posts_clauses', array( $this, $callback ) );
741 break;
742 case 'popularity':
743 add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
744 break;
745 case 'rating':
746 add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
747 break;
748 }
749
750 return apply_filters( 'woocommerce_get_catalog_ordering_args', $args, $orderby, $order );
751 }
752
753 /**
754 * Custom query used to filter products by price.
755 *
756 * @since 3.6.0
757 *
758 * @param array $args Query args.
759 * @param WP_Query $wp_query WP_Query object.
760 *
761 * @return array
762 */
763 public function price_filter_post_clauses( $args, $wp_query ) {
764 global $wpdb;
765
766 /**
767 * Filter whether to add the filter post clauses
768 *
769 * @param bool $is_main_query Whether the current query is 'is_main_query'.
770 * @param WP_Query $wp_query The current WP_Query object.
771 *
772 * @since 9.9.0
773 */
774 $enable_filtering = apply_filters( 'woocommerce_enable_post_clause_filtering', $wp_query->is_main_query(), $wp_query );
775
776 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
777 if ( ! $enable_filtering || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) {
778 return $args;
779 }
780
781 // phpcs:disable WordPress.Security.NonceVerification.Recommended
782 $current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0;
783 $current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX;
784 // phpcs:enable WordPress.Security.NonceVerification.Recommended
785
786 /**
787 * Adjust if the store taxes are not displayed how they are stored.
788 * Kicks in when prices excluding tax are displayed including tax.
789 */
790 if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
791 $tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class.
792 $tax_rates = WC_Tax::get_rates( $tax_class );
793
794 if ( $tax_rates ) {
795 $current_min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_min_price, $tax_rates ) );
796 $current_max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_max_price, $tax_rates ) );
797 }
798 }
799
800 $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
801 $args['where'] .= $wpdb->prepare(
802 ' AND NOT (%f<wc_product_meta_lookup.min_price OR %f>wc_product_meta_lookup.max_price ) ',
803 $current_max_price,
804 $current_min_price
805 );
806 return $args;
807 }
808
809 /**
810 * Handle numeric price sorting.
811 *
812 * @param array $args Query args.
813 * @return array
814 */
815 public function order_by_price_asc_post_clauses( $args ) {
816 $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
817 $args['orderby'] = ' wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC ';
818 return $args;
819 }
820
821 /**
822 * Handle numeric price sorting.
823 *
824 * @param array $args Query args.
825 * @return array
826 */
827 public function order_by_price_desc_post_clauses( $args ) {
828 $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
829 $args['orderby'] = ' wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC ';
830 return $args;
831 }
832
833 /**
834 * WP Core does not let us change the sort direction for individual orderby params - https://core.trac.wordpress.org/ticket/17065.
835 *
836 * This lets us sort by meta value desc, and have a second orderby param.
837 *
838 * @param array $args Query args.
839 * @return array
840 */
841 public function order_by_popularity_post_clauses( $args ) {
842 $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
843 $args['orderby'] = ' wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC ';
844 return $args;
845 }
846
847 /**
848 * Order by rating post clauses.
849 *
850 * @param array $args Query args.
851 * @return array
852 */
853 public function order_by_rating_post_clauses( $args ) {
854 $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
855 $args['orderby'] = ' wc_product_meta_lookup.average_rating DESC, wc_product_meta_lookup.rating_count DESC, wc_product_meta_lookup.product_id DESC ';
856 return $args;
857 }
858
859 /**
860 * Join wc_product_meta_lookup to posts if not already joined.
861 *
862 * @param string $sql SQL join.
863 * @return string
864 */
865 private function append_product_sorting_table_join( $sql ) {
866 global $wpdb;
867
868 if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {
869 $sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
870 }
871 return $sql;
872 }
873
874 /**
875 * Appends meta queries to an array.
876 *
877 * @param array $meta_query Meta query.
878 * @param bool $main_query If is main query.
879 * @return array
880 */
881 public function get_meta_query( $meta_query = array(), $main_query = false ) {
882 if ( ! is_array( $meta_query ) ) {
883 $meta_query = array();
884 }
885 return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) );
886 }
887
888 /**
889 * Appends tax queries to an array.
890 *
891 * @param array $tax_query Tax query.
892 * @param bool $main_query If is main query.
893 * @return array
894 */
895 public function get_tax_query( $tax_query = array(), $main_query = false ) {
896 if ( ! is_array( $tax_query ) ) {
897 $tax_query = array(
898 'relation' => 'AND',
899 );
900 }
901
902 if ( $main_query && ! $this->filterer->filtering_via_lookup_table_is_active() ) {
903 // Layered nav filters on terms.
904 foreach ( self::get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
905 $tax_query[] = array(
906 'taxonomy' => $taxonomy,
907 'field' => 'slug',
908 'terms' => $data['terms'],
909 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
910 'include_children' => false,
911 );
912 }
913 }
914
915 $product_visibility_terms = wc_get_product_visibility_term_ids();
916 $product_visibility_not_in = array( is_search() && $main_query ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] );
917
918 // Hide out of stock products.
919 if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
920 $product_visibility_not_in[] = $product_visibility_terms[ ProductStockStatus::OUT_OF_STOCK ];
921 }
922
923 // phpcs:disable WordPress.Security.NonceVerification.Recommended
924 // Filter by rating.
925 if ( isset( $_GET['rating_filter'] ) ) {
926 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
927 $rating_filter = array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) );
928 $rating_terms = array();
929 for ( $i = 1; $i <= 5; $i++ ) {
930 if ( in_array( $i, $rating_filter, true ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) {
931 $rating_terms[] = $product_visibility_terms[ 'rated-' . $i ];
932 }
933 }
934 if ( ! empty( $rating_terms ) ) {
935 $tax_query[] = array(
936 'taxonomy' => 'product_visibility',
937 'field' => 'term_taxonomy_id',
938 'terms' => $rating_terms,
939 'operator' => 'IN',
940 'rating_filter' => true,
941 );
942 }
943 }
944 // phpcs:enable WordPress.Security.NonceVerification.Recommended
945
946 if ( ! empty( $product_visibility_not_in ) ) {
947 $tax_query[] = array(
948 'taxonomy' => 'product_visibility',
949 'field' => 'term_taxonomy_id',
950 'terms' => $product_visibility_not_in,
951 'operator' => 'NOT IN',
952 );
953 }
954
955 return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) );
956 }
957
958 /**
959 * Get the main query which product queries ran against.
960 *
961 * @return WP_Query
962 */
963 public static function get_main_query() {
964 return self::$product_query;
965 }
966
967 /**
968 * Get the tax query which was used by the main query.
969 *
970 * @return array
971 */
972 public static function get_main_tax_query() {
973 $tax_query = isset( self::$product_query->tax_query, self::$product_query->tax_query->queries ) ? self::$product_query->tax_query->queries : array();
974
975 return $tax_query;
976 }
977
978 /**
979 * Get the meta query which was used by the main query.
980 *
981 * @return array
982 */
983 public static function get_main_meta_query() {
984 // PHPStan infers $product_query as non-nullable from other call sites. See https://github.com/woocommerce/woocommerce/pull/64360#issuecomment-4360066970.
985 // @phpstan-ignore-next-line isset.property See above.
986 if ( ! isset( self::$product_query ) ) {
987 return array();
988 }
989 $args = self::$product_query->query_vars;
990 $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
991
992 return $meta_query;
993 }
994
995 /**
996 * Based on WP_Query::parse_search
997 */
998 public static function get_main_search_query_sql() {
999 global $wpdb;
1000
1001 // PHPStan infers $product_query as non-nullable from other call sites. See https://github.com/woocommerce/woocommerce/pull/64360#issuecomment-4360066970.
1002 // @phpstan-ignore-next-line isset.property See above.
1003 if ( ! isset( self::$product_query ) ) {
1004 return '';
1005 }
1006 $args = self::$product_query->query_vars;
1007 $search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array();
1008 $sql = array();
1009
1010 foreach ( $search_terms as $term ) {
1011 // Terms prefixed with '-' should be excluded.
1012 $include = '-' !== substr( $term, 0, 1 );
1013
1014 if ( $include ) {
1015 $like_op = 'LIKE';
1016 $andor_op = 'OR';
1017 } else {
1018 $like_op = 'NOT LIKE';
1019 $andor_op = 'AND';
1020 $term = substr( $term, 1 );
1021 }
1022
1023 $like = '%' . $wpdb->esc_like( $term ) . '%';
1024 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1025 $sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like );
1026 }
1027
1028 if ( ! empty( $sql ) && ! is_user_logged_in() ) {
1029 $sql[] = "($wpdb->posts.post_password = '')";
1030 }
1031
1032 return implode( ' AND ', $sql );
1033 }
1034
1035 /**
1036 * Get an array of attributes and terms selected with the layered nav widget.
1037 *
1038 * @return array
1039 */
1040 public static function get_layered_nav_chosen_attributes() {
1041 // phpcs:disable WordPress.Security.NonceVerification.Recommended
1042 if ( ! is_array( self::$chosen_attributes ) ) {
1043 self::$chosen_attributes = array();
1044
1045 if ( ! empty( $_GET ) ) {
1046 foreach ( $_GET as $key => $value ) {
1047 if ( 0 === strpos( $key, 'filter_' ) ) {
1048 if ( ! is_string( $value ) ) {
1049 continue;
1050 }
1051
1052 $attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) );
1053 $taxonomy = wc_attribute_taxonomy_name( $attribute );
1054 $filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array();
1055
1056 if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) {
1057 continue;
1058 }
1059
1060 $query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $_GET[ 'query_type_' . $attribute ] ) ) : '';
1061 self::$chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding.
1062 self::$chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
1063 }
1064 }
1065 }
1066 }
1067 return self::$chosen_attributes;
1068 // phpcs:disable WordPress.Security.NonceVerification.Recommended
1069 }
1070
1071 /**
1072 * Remove the add-to-cart param from pagination urls.
1073 *
1074 * @param string $url URL.
1075 * @return string
1076 */
1077 public function remove_add_to_cart_pagination( $url ) {
1078 return remove_query_arg( 'add-to-cart', $url );
1079 }
1080
1081 /**
1082 * Return a meta query for filtering by rating.
1083 *
1084 * @deprecated 3.0.0 Replaced with taxonomy.
1085 * @return array
1086 */
1087 public function rating_filter_meta_query() {
1088 return array();
1089 }
1090
1091 /**
1092 * Returns a meta query to handle product visibility.
1093 *
1094 * @deprecated 3.0.0 Replaced with taxonomy.
1095 * @param string $compare (default: 'IN').
1096 * @return array
1097 */
1098 public function visibility_meta_query( $compare = 'IN' ) {
1099 return array();
1100 }
1101
1102 /**
1103 * Returns a meta query to handle product stock status.
1104 *
1105 * @deprecated 3.0.0 Replaced with taxonomy.
1106 * @param string $status (default: 'instock').
1107 * @return array
1108 */
1109 public function stock_status_meta_query( $status = ProductStockStatus::IN_STOCK ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
1110 return array();
1111 }
1112
1113 /**
1114 * Layered nav init.
1115 *
1116 * @deprecated 2.6.0
1117 */
1118 public function layered_nav_init() {
1119 wc_deprecated_function( 'layered_nav_init', '2.6' );
1120 }
1121
1122 /**
1123 * Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients.
1124 *
1125 * @deprecated 2.6.0 due to performance concerns
1126 */
1127 public function get_products_in_view() {
1128 wc_deprecated_function( 'get_products_in_view', '2.6' );
1129 }
1130
1131 /**
1132 * Layered Nav post filter.
1133 *
1134 * @deprecated 2.6.0 due to performance concerns
1135 *
1136 * @param mixed $deprecated Deprecated.
1137 */
1138 public function layered_nav_query( $deprecated ) {
1139 wc_deprecated_function( 'layered_nav_query', '2.6' );
1140 }
1141
1142 /**
1143 * Search post excerpt.
1144 *
1145 * @param string $where Where clause.
1146 *
1147 * @deprecated 3.2.0 - Not needed anymore since WordPress 4.5.
1148 */
1149 public function search_post_excerpt( $where = '' ) {
1150 wc_deprecated_function( 'WC_Query::search_post_excerpt', '3.2.0', 'Excerpt added to search query by default since WordPress 4.5.' );
1151 return $where;
1152 }
1153
1154 /**
1155 * Remove the posts_where filter.
1156 *
1157 * @deprecated 3.2.0 - Nothing to remove anymore because search_post_excerpt() is deprecated.
1158 */
1159 public function remove_posts_where() {
1160 wc_deprecated_function( 'WC_Query::remove_posts_where', '3.2.0', 'Nothing to remove anymore because search_post_excerpt() is deprecated.' );
1161 }
1162 }
1163