* Contains the query functions for WooCommerce which alter the front-end post queries and loops
* @package WooCommerce\Classes
use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer;
use Automattic\WooCommerce\Enums\ProductStockStatus;
defined( 'ABSPATH' ) || exit;
* Query vars to add to wp.
public $query_vars = array();
* Reference to the main product query on the page.
private static $product_query;
* Stores chosen attributes.
private static $chosen_attributes;
* The instance of the class that helps filtering with the product attributes lookup table.
* Constructor for the query class. Hooks in methods.
public function __construct() {
$this->filterer = wc_get_container()->get( Filterer::class );
add_action( 'init', array( $this, 'add_endpoints' ) );
add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_action( 'parse_request', array( $this, 'parse_request' ), 0 );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
add_filter( 'get_pagenum_link', array( $this, 'remove_add_to_cart_pagination' ), 10, 1 );
$this->init_query_vars();
* Reset the chosen attributes so that get_layered_nav_chosen_attributes will get them from the query again.
public static function reset_chosen_attributes() {
self::$chosen_attributes = null;
* Get any errors from querystring.
public function get_errors() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$error = ! empty( $_GET['wc_error'] ) ? sanitize_text_field( wp_unslash( $_GET['wc_error'] ) ) : '';
if ( $error && ! wc_has_notice( $error, 'error' ) ) {
wc_add_notice( $error, 'error' );
* Init query vars by loading options.
public function init_query_vars() {
// Query vars to add to WP.
$this->query_vars = array(
'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ),
'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ),
'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ),
'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
'delete-payment-method' => get_option( 'woocommerce_myaccount_delete_payment_method_endpoint', 'delete-payment-method' ),
'set-default-payment-method' => get_option( 'woocommerce_myaccount_set_default_payment_method_endpoint', 'set-default-payment-method' ),
* Get page title for an endpoint.
* @param string $endpoint Endpoint key.
* @param string $action Optional action or variation within the endpoint.
* @since 4.6.0 Added $action parameter.
* @return string The page title.
public function get_endpoint_title( $endpoint, $action = '' ) {
$title = __( 'Pay for order', 'woocommerce' );
$title = __( 'Order received', 'woocommerce' );
if ( ! empty( $wp->query_vars['orders'] ) ) {
/* translators: %s: page */
$title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) );
$title = __( 'Orders', 'woocommerce' );
$order = wc_get_order( $wp->query_vars['view-order'] );
/* translators: %s: order number */
$title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
$title = __( 'Downloads', 'woocommerce' );
$title = __( 'Account details', 'woocommerce' );
$title = __( 'Addresses', 'woocommerce' );
$title = __( 'Payment methods', 'woocommerce' );
case 'add-payment-method':
$title = __( 'Add payment method', 'woocommerce' );
if ( in_array( $action, array( 'rp', 'resetpass', 'newaccount' ), true ) ) {
$title = __( 'Set password', 'woocommerce' );
$title = __( 'Lost password', 'woocommerce' );
* Filters the page title used for my-account endpoints.
* @since 4.6.0 Added $action parameter.
* @see get_endpoint_title()
* @param string $title Default title.
* @param string $endpoint Endpoint key.
* @param string $action Optional action or variation within the endpoint.
return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint, $action );
* Endpoint mask describing the places the endpoint should be added.
public function get_endpoints_mask() {
if ( 'page' === get_option( 'show_on_front' ) ) {
$page_on_front = get_option( 'page_on_front' );
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
$checkout_page_id = get_option( 'woocommerce_checkout_page_id' );
if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ), true ) ) {
return EP_ROOT | EP_PAGES;
* Add endpoints for query vars.
public function add_endpoints() {
$mask = $this->get_endpoints_mask();
foreach ( $this->get_query_vars() as $key => $var ) {
add_rewrite_endpoint( $var, $mask );
* @param array $vars Query vars.
public function add_query_vars( $vars ) {
foreach ( $this->get_query_vars() as $key => $var ) {
public function get_query_vars() {
return apply_filters( 'woocommerce_get_query_vars', $this->query_vars );
* Get query current active query var.
public function get_current_endpoint() {
foreach ( $this->get_query_vars() as $key => $value ) {
if ( isset( $wp->query_vars[ $key ] ) ) {
* Parse the request and look for query vars - endpoints may not be supported.
public function parse_request() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// Map query vars to their keys, or get them if endpoints are not supported.
foreach ( $this->get_query_vars() as $key => $var ) {
if ( isset( $_GET[ $var ] ) ) {
$wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) );
} elseif ( isset( $wp->query_vars[ $var ] ) ) {
$wp->query_vars[ $key ] = $wp->query_vars[ $var ];
// phpcs:enable WordPress.Security.NonceVerification.Recommended
* Are we currently on the front page?
* @param WP_Query $q Query instance.
private function is_showing_page_on_front( $q ) {
return ( $q->is_home() && ! $q->is_posts_page ) && 'page' === get_option( 'show_on_front' );
* Is the front page a page we define?
* @param int $page_id Page ID.
private function page_on_front_is( $page_id ) {
return absint( get_option( 'page_on_front' ) ) === absint( $page_id );
* Returns a copy of `$query` with all query vars that are allowed on the front page stripped.
* Used when the shop page is also the front page.
* @param array $query The unfiltered array.
* @return array The filtered query vars.
private function filter_out_valid_front_page_query_vars( $query ) {
return ! $this->is_query_var_valid_on_front_page( $key );
* Checks whether a query var is allowed on the front page or not.
* @param string $query_var Query var name.
* @return boolean TRUE when query var is allowed on the front page. FALSE otherwise.
private function is_query_var_valid_on_front_page( $query_var ) {
return in_array( $query_var, array( 'preview', 'page', 'paged', 'cpage', 'orderby' ), true )
|| in_array( $query_var, array( 'min_price', 'max_price', 'rating_filter' ), true )
|| 0 === strpos( $query_var, 'filter_' )
|| 0 === strpos( $query_var, 'query_type_' );
* Hook into pre_get_posts to do the main product query.
* @param WP_Query $q Query instance.
public function pre_get_posts( $q ) {
// We only want to affect the main query.
if ( ! $q->is_main_query() ) {
// Fixes for queries on static homepages.
if ( $this->is_showing_page_on_front( $q ) ) {
// Fix for endpoints on the homepage.
if ( ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) {
$_query = wp_parse_args( $q->query );
if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->get_query_vars() ) ) ) {
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
add_filter( 'redirect_canonical', '__return_false' );
// When orderby is set, WordPress shows posts on the front-page. Get around that here.
if ( $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) {
$_query = $this->filter_out_valid_front_page_query_vars( wp_parse_args( $q->query ) );
if ( empty( $_query ) ) {
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
// WP supporting themes show post type archive.
if ( wc_current_theme_supports_woocommerce_or_fse() ) {
$q->set( 'post_type', 'product' );
} elseif ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) {
$q->is_comment_feed = false;
// Special check for shops with the PRODUCT POST TYPE ARCHIVE on front.
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' ) ) {
// This is a front-page shop.
$q->set( 'post_type', 'product' );
$q->set( 'page_id', '' );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
// Define a variable so we know this is the front page shop later on.
wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true );
// Get the actual WP page to avoid errors and let us use is_front_page().
// This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096.
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$wp_post_types['product']->ID = $shop_page->ID;
$wp_post_types['product']->post_title = $shop_page->post_title;
$wp_post_types['product']->post_name = $shop_page->post_name;
$wp_post_types['product']->post_type = $shop_page->post_type;
$wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type );
// Fix conditional Functions like is_front_page.
$q->is_post_type_archive = true;
// Remove post type archive name from front page title tag.
add_filter( 'post_type_archive_title', '__return_empty_string', 5 );
if ( class_exists( 'WPSEO_Meta' ) ) {
add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
} elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) {
// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies.
$this->product_query( $q );
* Handler for the 'the_posts' WP filter.
* @param array $posts Posts from WP Query.
* @param WP_Query $query Current query.
public function handle_get_posts( $posts, $query ) {
if ( 'product_query' !== $query->get( 'wc_query' ) ) {
$this->remove_product_query_filters( $posts );
* Pre_get_posts above may adjust the main query to add WooCommerce logic. When this query is done, we need to ensure
* all custom filters are removed.
* This is done here during the_posts filter. The input is not changed.
* @param array $posts Posts from WP Query.
public function remove_product_query_filters( $posts ) {
$this->remove_ordering_args();
remove_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 );
* This function used to be hooked to found_posts and adjust the posts count when the filtering by attribute
* widget was used and variable products were present. Now it isn't hooked anymore and does nothing but return
* the input unchanged, since the pull request in which it was introduced has been reverted.
* @param int $count Original posts count, as supplied by the found_posts filter.
* @param WP_Query $query The current WP_Query object.
* @return int Adjusted posts count.
public function adjust_posts_count( $count, $query ) {
* Instance version of get_layered_nav_chosen_attributes, needed for unit tests.
protected function get_layered_nav_chosen_attributes_inst() {
return self::get_layered_nav_chosen_attributes();
* Get the posts (or the ids of the posts) found in the current WP loop.
* @return array Array of posts or post ids.
protected function get_current_posts() {
return $GLOBALS['wp_query']->posts;
* WP SEO meta description.
* Hooked into wpseo_ hook already, so no need for function_exist.
public function wpseo_metadesc() {
return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) );