2011-09-16 08:10:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Contains the query functions for WooCommerce which alter the front-end post queries and loops
|
2011-09-16 08:10:12 +00:00
|
|
|
*
|
2017-09-27 18:01:50 +00:00
|
|
|
* @version 3.2.0
|
2017-11-20 22:19:09 +00:00
|
|
|
* @package WooCommerce\Classes
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2013-07-23 16:05:01 +00:00
|
|
|
|
2018-03-21 03:13:22 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2013-07-23 16:05:01 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* WC_Query Class.
|
2013-07-23 16:05:01 +00:00
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Query {
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-09-27 18:01:50 +00:00
|
|
|
/**
|
|
|
|
* Query vars to add to wp.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-09-03 15:14:56 +00:00
|
|
|
public $query_vars = array();
|
2013-06-04 16:33:43 +00:00
|
|
|
|
2017-11-09 12:01:27 +00:00
|
|
|
/**
|
|
|
|
* Reference to the main product query on the page.
|
|
|
|
*
|
2020-09-05 14:14:57 +00:00
|
|
|
* @var WP_Query
|
2017-11-09 12:01:27 +00:00
|
|
|
*/
|
|
|
|
private static $product_query;
|
|
|
|
|
2016-02-10 10:19:32 +00:00
|
|
|
/**
|
2017-09-27 18:01:50 +00:00
|
|
|
* Stores chosen attributes.
|
|
|
|
*
|
2016-02-10 10:19:32 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-21 13:52:40 +00:00
|
|
|
private static $chosen_attributes;
|
2016-02-10 10:19:32 +00:00
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2012-08-15 17:08:42 +00:00
|
|
|
* Constructor for the query class. Hooks in methods.
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function __construct() {
|
2013-07-23 16:05:01 +00:00
|
|
|
add_action( 'init', array( $this, 'add_endpoints' ) );
|
|
|
|
if ( ! is_admin() ) {
|
2015-01-05 14:43:01 +00:00
|
|
|
add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 );
|
2016-08-27 02:23:54 +00:00
|
|
|
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
|
|
|
|
add_action( 'parse_request', array( $this, 'parse_request' ), 0 );
|
2014-06-30 10:41:21 +00:00
|
|
|
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
|
2017-11-29 08:52:55 +00:00
|
|
|
add_filter( 'get_pagenum_link', array( $this, 'remove_add_to_cart_pagination' ), 10, 1 );
|
2013-07-23 16:05:01 +00:00
|
|
|
}
|
2013-09-03 15:14:56 +00:00
|
|
|
$this->init_query_vars();
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:42:19 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get any errors from querystring.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
public function get_errors() {
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
|
|
$error = ! empty( $_GET['wc_error'] ) ? sanitize_text_field( wp_unslash( $_GET['wc_error'] ) ) : '';
|
2017-11-20 22:19:09 +00:00
|
|
|
|
|
|
|
if ( $error && ! wc_has_notice( $error, 'error' ) ) {
|
2014-12-05 13:42:19 +00:00
|
|
|
wc_add_notice( $error, 'error' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:14:56 +00:00
|
|
|
/**
|
|
|
|
* Init query vars by loading options.
|
|
|
|
*/
|
|
|
|
public function init_query_vars() {
|
2016-01-19 19:47:17 +00:00
|
|
|
// Query vars to add to WP.
|
2013-09-03 15:14:56 +00:00
|
|
|
$this->query_vars = array(
|
2016-01-19 19:47:17 +00:00
|
|
|
// Checkout actions.
|
2017-12-06 13:13:43 +00:00
|
|
|
'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
|
|
|
|
'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
|
2016-01-19 19:47:17 +00:00
|
|
|
// My account actions.
|
2016-02-09 20:19:34 +00:00
|
|
|
'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' ),
|
2013-09-03 15:14:56 +00:00
|
|
|
);
|
2013-07-23 16:05:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:36:28 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get page title for an endpoint.
|
2017-11-09 11:50:50 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param string $endpoint Endpoint key.
|
2014-12-05 13:42:19 +00:00
|
|
|
* @return string
|
2013-07-26 14:36:28 +00:00
|
|
|
*/
|
2014-12-05 13:42:19 +00:00
|
|
|
public function get_endpoint_title( $endpoint ) {
|
|
|
|
global $wp;
|
|
|
|
|
|
|
|
switch ( $endpoint ) {
|
2017-11-20 22:19:09 +00:00
|
|
|
case 'order-pay':
|
2016-10-12 10:16:30 +00:00
|
|
|
$title = __( 'Pay for order', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'order-received':
|
2016-10-12 10:16:30 +00:00
|
|
|
$title = __( 'Order received', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'orders':
|
2016-01-20 15:46:59 +00:00
|
|
|
if ( ! empty( $wp->query_vars['orders'] ) ) {
|
2016-10-29 17:32:38 +00:00
|
|
|
/* translators: %s: page */
|
2016-01-20 15:46:59 +00:00
|
|
|
$title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) );
|
2016-01-19 21:02:26 +00:00
|
|
|
} else {
|
|
|
|
$title = __( 'Orders', 'woocommerce' );
|
|
|
|
}
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'view-order':
|
2014-12-05 13:42:19 +00:00
|
|
|
$order = wc_get_order( $wp->query_vars['view-order'] );
|
2016-10-29 17:32:38 +00:00
|
|
|
/* translators: %s: order number */
|
2015-06-10 15:09:18 +00:00
|
|
|
$title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'downloads':
|
2016-01-19 19:47:17 +00:00
|
|
|
$title = __( 'Downloads', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'edit-account':
|
2016-10-12 10:16:30 +00:00
|
|
|
$title = __( 'Account details', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'edit-address':
|
2016-05-24 18:09:07 +00:00
|
|
|
$title = __( 'Addresses', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'payment-methods':
|
2016-10-12 10:16:30 +00:00
|
|
|
$title = __( 'Payment methods', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'add-payment-method':
|
2016-10-12 10:16:30 +00:00
|
|
|
$title = __( 'Add payment method', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
case 'lost-password':
|
2016-10-12 10:16:30 +00:00
|
|
|
$title = __( 'Lost password', 'woocommerce' );
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-12-22 14:25:22 +00:00
|
|
|
$title = '';
|
2017-11-20 22:19:09 +00:00
|
|
|
break;
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
2016-01-19 19:47:17 +00:00
|
|
|
|
2016-12-25 22:43:25 +00:00
|
|
|
return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint );
|
2013-07-26 14:36:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 20:12:42 +00:00
|
|
|
/**
|
|
|
|
* Endpoint mask describing the places the endpoint should be added.
|
|
|
|
*
|
|
|
|
* @since 2.6.2
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-02-15 14:30:23 +00:00
|
|
|
public function get_endpoints_mask() {
|
2016-06-17 20:12:42 +00:00
|
|
|
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' );
|
|
|
|
|
2017-11-09 11:50:50 +00:00
|
|
|
if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ), true ) ) {
|
2016-06-17 20:12:42 +00:00
|
|
|
return EP_ROOT | EP_PAGES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EP_PAGES;
|
|
|
|
}
|
|
|
|
|
2013-07-23 16:05:01 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add endpoints for query vars.
|
2013-07-23 16:05:01 +00:00
|
|
|
*/
|
|
|
|
public function add_endpoints() {
|
2016-06-17 20:12:42 +00:00
|
|
|
$mask = $this->get_endpoints_mask();
|
|
|
|
|
2017-11-20 16:40:56 +00:00
|
|
|
foreach ( $this->get_query_vars() as $key => $var ) {
|
2016-06-30 16:58:10 +00:00
|
|
|
if ( ! empty( $var ) ) {
|
|
|
|
add_rewrite_endpoint( $var, $mask );
|
|
|
|
}
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
2011-09-16 08:10:12 +00:00
|
|
|
}
|
2011-12-22 19:55:08 +00:00
|
|
|
|
2013-05-31 15:13:14 +00:00
|
|
|
/**
|
2016-01-06 15:17:26 +00:00
|
|
|
* Add query vars.
|
2013-05-31 15:13:14 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param array $vars Query vars.
|
2013-11-29 18:50:31 +00:00
|
|
|
* @return array
|
2013-05-31 15:13:14 +00:00
|
|
|
*/
|
|
|
|
public function add_query_vars( $vars ) {
|
2017-02-13 18:22:05 +00:00
|
|
|
foreach ( $this->get_query_vars() as $key => $var ) {
|
2013-09-03 15:14:56 +00:00
|
|
|
$vars[] = $key;
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
2013-05-31 15:13:14 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2014-02-12 12:18:36 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get query vars.
|
2014-09-02 19:50:19 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2014-02-12 12:18:36 +00:00
|
|
|
*/
|
|
|
|
public function get_query_vars() {
|
2017-02-13 18:22:05 +00:00
|
|
|
return apply_filters( 'woocommerce_get_query_vars', $this->query_vars );
|
2014-02-12 12:18:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 13:42:19 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get query current active query var.
|
2014-12-05 13:42:19 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_current_endpoint() {
|
|
|
|
global $wp;
|
2017-11-09 12:01:27 +00:00
|
|
|
|
2014-12-05 13:42:19 +00:00
|
|
|
foreach ( $this->get_query_vars() as $key => $value ) {
|
|
|
|
if ( isset( $wp->query_vars[ $key ] ) ) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:13:14 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Parse the request and look for query vars - endpoints may not be supported.
|
2013-05-31 15:13:14 +00:00
|
|
|
*/
|
|
|
|
public function parse_request() {
|
|
|
|
global $wp;
|
|
|
|
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
2017-11-20 22:19:09 +00:00
|
|
|
// Map query vars to their keys, or get them if endpoints are not supported.
|
2017-02-13 18:22:05 +00:00
|
|
|
foreach ( $this->get_query_vars() as $key => $var ) {
|
2020-04-21 13:52:40 +00:00
|
|
|
if ( isset( $_GET[ $var ] ) ) {
|
|
|
|
$wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) );
|
2016-09-02 03:15:49 +00:00
|
|
|
} elseif ( isset( $wp->query_vars[ $var ] ) ) {
|
2013-09-03 15:14:56 +00:00
|
|
|
$wp->query_vars[ $key ] = $wp->query_vars[ $var ];
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
2013-05-31 15:13:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 12:37:51 +00:00
|
|
|
/**
|
|
|
|
* Are we currently on the front page?
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param WP_Query $q Query instance.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @return bool
|
2016-07-26 12:37:51 +00:00
|
|
|
*/
|
|
|
|
private function is_showing_page_on_front( $q ) {
|
2019-09-24 21:41:57 +00:00
|
|
|
return ( $q->is_home() && ! $q->is_posts_page ) && 'page' === get_option( 'show_on_front' );
|
2016-07-26 12:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the front page a page we define?
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param int $page_id Page ID.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @return bool
|
2016-07-26 12:37:51 +00:00
|
|
|
*/
|
|
|
|
private function page_on_front_is( $page_id ) {
|
|
|
|
return absint( get_option( 'page_on_front' ) ) === absint( $page_id );
|
|
|
|
}
|
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hook into pre_get_posts to do the main product query.
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param WP_Query $q Query instance.
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function pre_get_posts( $q ) {
|
2017-11-09 11:17:02 +00:00
|
|
|
// We only want to affect the main query.
|
2014-06-25 10:10:05 +00:00
|
|
|
if ( ! $q->is_main_query() ) {
|
2012-06-13 23:52:21 +00:00
|
|
|
return;
|
2014-06-25 10:10:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 16:38:18 +00:00
|
|
|
// 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->is_page = true;
|
|
|
|
$q->is_home = false;
|
|
|
|
$q->is_singular = true;
|
|
|
|
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
|
|
|
|
add_filter( 'redirect_canonical', '__return_false' );
|
|
|
|
}
|
2014-06-25 10:10:05 +00:00
|
|
|
}
|
2014-08-29 09:26:21 +00:00
|
|
|
|
2019-03-27 16:38:18 +00:00
|
|
|
// 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 = wp_parse_args( $q->query );
|
|
|
|
if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
|
|
|
|
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
|
|
|
|
$q->is_page = true;
|
|
|
|
$q->is_home = false;
|
|
|
|
|
|
|
|
// WP supporting themes show post type archive.
|
|
|
|
if ( current_theme_supports( 'woocommerce' ) ) {
|
|
|
|
$q->set( 'post_type', 'product' );
|
|
|
|
} else {
|
|
|
|
$q->is_singular = true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 19:58:14 +00:00
|
|
|
} elseif ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2018-02-01 11:15:46 +00:00
|
|
|
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
|
2019-12-20 17:21:08 +00:00
|
|
|
$q->is_page = true;
|
|
|
|
$q->is_home = false;
|
2019-03-27 16:38:18 +00:00
|
|
|
$q->is_singular = true;
|
2013-03-04 22:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:50:50 +00:00
|
|
|
// Fix product feeds.
|
2016-06-21 11:46:39 +00:00
|
|
|
if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) {
|
|
|
|
$q->is_comment_feed = false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:15:46 +00:00
|
|
|
// Special check for shops with the PRODUCT POST TYPE ARCHIVE on front.
|
|
|
|
if ( current_theme_supports( 'woocommerce' ) && $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) {
|
2017-11-09 11:50:50 +00:00
|
|
|
// This is a front-page shop.
|
2012-06-13 23:52:21 +00:00
|
|
|
$q->set( 'post_type', 'product' );
|
|
|
|
$q->set( 'page_id', '' );
|
2015-12-10 10:42:54 +00:00
|
|
|
|
2014-07-03 14:14:00 +00:00
|
|
|
if ( isset( $q->query['paged'] ) ) {
|
2012-06-13 23:52:21 +00:00
|
|
|
$q->set( 'paged', $q->query['paged'] );
|
2014-07-03 14:14:00 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-11-09 11:17:02 +00:00
|
|
|
// Define a variable so we know this is the front page shop later on.
|
2017-09-05 19:52:39 +00:00
|
|
|
wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-11-20 22:19:09 +00:00
|
|
|
// 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.
|
2012-06-29 09:23:21 +00:00
|
|
|
global $wp_post_types;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2017-11-20 22:19:09 +00:00
|
|
|
$shop_page = get_post( wc_get_page_id( 'shop' ) );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-11-20 22:19:09 +00:00
|
|
|
$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 );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-11-09 11:17:02 +00:00
|
|
|
// Fix conditional Functions like is_front_page.
|
2014-07-03 14:14:00 +00:00
|
|
|
$q->is_singular = false;
|
2013-01-02 20:11:17 +00:00
|
|
|
$q->is_post_type_archive = true;
|
2014-07-03 14:14:00 +00:00
|
|
|
$q->is_archive = true;
|
|
|
|
$q->is_page = true;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2017-11-09 11:17:02 +00:00
|
|
|
// Remove post type archive name from front page title tag.
|
2015-12-10 10:42:54 +00:00
|
|
|
add_filter( 'post_type_archive_title', '__return_empty_string', 5 );
|
|
|
|
|
2017-11-09 11:17:02 +00:00
|
|
|
// Fix WP SEO.
|
2014-05-07 10:40:21 +00:00
|
|
|
if ( class_exists( 'WPSEO_Meta' ) ) {
|
2013-01-02 20:11:17 +00:00
|
|
|
add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
|
|
|
|
add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
|
|
|
|
}
|
2014-06-25 10:10:05 +00:00
|
|
|
} elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) {
|
2017-11-20 22:19:09 +00:00
|
|
|
// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies.
|
2014-06-25 10:10:05 +00:00
|
|
|
return;
|
2012-06-13 23:52:21 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
$this->product_query( $q );
|
2018-03-21 17:41:14 +00:00
|
|
|
}
|
2013-01-12 10:53:08 +00:00
|
|
|
|
2020-07-31 14:21:22 +00:00
|
|
|
/**
|
|
|
|
* Handler for the 'the_posts' WP filter.
|
|
|
|
*
|
2020-08-19 08:01:57 +00:00
|
|
|
* @param array $posts Posts from WP Query.
|
|
|
|
* @param WP_Query $query Current query.
|
2020-07-31 14:21:22 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-08-19 08:01:57 +00:00
|
|
|
public function handle_get_posts( $posts, $query ) {
|
|
|
|
if ( 'product_query' !== $query->get( 'wc_query' ) ) {
|
|
|
|
return $posts;
|
|
|
|
}
|
2020-07-31 14:21:22 +00:00
|
|
|
$this->remove_product_query_filters( $posts );
|
|
|
|
return $posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-21 17:41:14 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function remove_product_query_filters( $posts ) {
|
|
|
|
$this->remove_ordering_args();
|
2019-02-11 15:45:30 +00:00
|
|
|
remove_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 );
|
2018-03-21 17:41:14 +00:00
|
|
|
return $posts;
|
2011-12-22 19:55:08 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2020-09-10 13:17:43 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @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 ) {
|
|
|
|
return $count;
|
|
|
|
}
|
2020-04-23 13:07:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance version of get_layered_nav_chosen_attributes, needed for unit tests.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_layered_nav_chosen_attributes_inst() {
|
|
|
|
return self::get_layered_nav_chosen_attributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-14 14:22:16 +00:00
|
|
|
* Get the posts (or the ids of the posts) found in the current WP loop.
|
2020-04-23 13:07:09 +00:00
|
|
|
*
|
2020-05-14 14:22:16 +00:00
|
|
|
* @return array Array of posts or post ids.
|
2020-04-23 13:07:09 +00:00
|
|
|
*/
|
2020-05-14 14:22:16 +00:00
|
|
|
protected function get_current_posts() {
|
2020-04-23 13:07:09 +00:00
|
|
|
return $GLOBALS['wp_query']->posts;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:44:27 +00:00
|
|
|
/**
|
2016-01-06 15:17:26 +00:00
|
|
|
* WP SEO meta description.
|
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hooked into wpseo_ hook already, so no need for function_exist.
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2013-11-29 18:50:31 +00:00
|
|
|
* @return string
|
2012-08-22 19:44:27 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function wpseo_metadesc() {
|
2016-02-09 21:14:55 +00:00
|
|
|
return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) );
|
2012-08-22 19:44:27 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-08-22 19:44:27 +00:00
|
|
|
/**
|
2016-01-06 15:17:26 +00:00
|
|
|
* WP SEO meta key.
|
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hooked into wpseo_ hook already, so no need for function_exist.
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2013-11-29 18:50:31 +00:00
|
|
|
* @return string
|
2012-08-22 19:44:27 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function wpseo_metakey() {
|
2016-02-09 21:14:55 +00:00
|
|
|
return WPSEO_Meta::get_value( 'metakey', wc_get_page_id( 'shop' ) );
|
2012-03-26 14:34:23 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-12-22 19:55:08 +00:00
|
|
|
/**
|
2017-11-20 22:19:09 +00:00
|
|
|
* Query the products, applying sorting/ordering etc.
|
|
|
|
* This applies to the main WordPress loop.
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param WP_Query $q Query instance.
|
2011-12-22 19:55:08 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function product_query( $q ) {
|
2017-09-25 12:08:08 +00:00
|
|
|
if ( ! is_feed() ) {
|
2018-03-21 03:13:22 +00:00
|
|
|
$ordering = $this->get_catalog_ordering_args();
|
2017-09-25 12:08:08 +00:00
|
|
|
$q->set( 'orderby', $ordering['orderby'] );
|
|
|
|
$q->set( 'order', $ordering['order'] );
|
|
|
|
|
|
|
|
if ( isset( $ordering['meta_key'] ) ) {
|
|
|
|
$q->set( 'meta_key', $ordering['meta_key'] );
|
|
|
|
}
|
2014-12-09 08:33:29 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-11-09 11:17:02 +00:00
|
|
|
// Query vars that affect posts shown.
|
2016-12-08 10:56:45 +00:00
|
|
|
$q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) );
|
|
|
|
$q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) );
|
2015-03-13 10:33:36 +00:00
|
|
|
$q->set( 'wc_query', 'product_query' );
|
2016-10-24 06:51:48 +00:00
|
|
|
$q->set( 'post__in', array_unique( (array) apply_filters( 'loop_shop_post_in', array() ) ) );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2017-11-09 12:37:14 +00:00
|
|
|
// Work out how many products to query.
|
2017-11-09 17:34:54 +00:00
|
|
|
$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() ) );
|
2017-11-09 12:37:14 +00:00
|
|
|
|
2017-11-09 12:01:27 +00:00
|
|
|
// Store reference to this query.
|
|
|
|
self::$product_query = $q;
|
|
|
|
|
2019-02-11 15:45:30 +00:00
|
|
|
// Additonal hooks to change WP Query.
|
|
|
|
add_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 );
|
2020-08-19 08:01:57 +00:00
|
|
|
add_filter( 'the_posts', array( $this, 'handle_get_posts' ), 10, 2 );
|
2020-09-09 10:24:41 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
do_action( 'woocommerce_product_query', $q, $this );
|
2011-09-16 08:10:12 +00:00
|
|
|
}
|
2011-12-22 19:55:08 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Remove the query.
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function remove_product_query() {
|
2014-06-30 10:41:21 +00:00
|
|
|
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
|
2011-09-16 08:10:12 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-10-21 12:05:34 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Remove ordering queries.
|
2013-10-21 12:05:34 +00:00
|
|
|
*/
|
|
|
|
public function remove_ordering_args() {
|
2017-04-24 17:11:16 +00:00
|
|
|
remove_filter( 'posts_clauses', array( $this, 'order_by_price_asc_post_clauses' ) );
|
|
|
|
remove_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) );
|
2013-10-21 12:05:34 +00:00
|
|
|
remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
|
|
|
|
remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
|
|
|
|
}
|
|
|
|
|
2011-09-23 11:04:51 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Returns an array of arguments for ordering products based on the selected values.
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
2017-11-09 11:50:50 +00:00
|
|
|
* @param string $orderby Order by param.
|
|
|
|
* @param string $order Order param.
|
2012-08-14 20:19:41 +00:00
|
|
|
* @return array
|
2011-09-23 11:04:51 +00:00
|
|
|
*/
|
2012-12-18 06:57:44 +00:00
|
|
|
public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
|
2017-11-09 11:50:50 +00:00
|
|
|
// Get ordering from query string unless defined.
|
2013-01-12 10:53:08 +00:00
|
|
|
if ( ! $orderby ) {
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
|
|
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( (string) wp_unslash( $_GET['orderby'] ) ) : wc_clean( get_query_var( 'orderby' ) );
|
2017-11-10 17:45:01 +00:00
|
|
|
|
|
|
|
if ( ! $orderby_value ) {
|
|
|
|
if ( is_search() ) {
|
|
|
|
$orderby_value = 'relevance';
|
|
|
|
} else {
|
2018-03-21 17:24:00 +00:00
|
|
|
$orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );
|
2017-11-10 17:45:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-12 10:53:08 +00:00
|
|
|
|
2017-11-09 11:50:50 +00:00
|
|
|
// Get order + orderby args from string.
|
2018-11-28 12:51:05 +00:00
|
|
|
$orderby_value = is_array( $orderby_value ) ? $orderby_value : explode( '-', $orderby_value );
|
2013-01-12 10:53:08 +00:00
|
|
|
$orderby = esc_attr( $orderby_value[0] );
|
|
|
|
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
|
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2019-04-23 10:53:07 +00:00
|
|
|
// Convert to correct format.
|
|
|
|
$orderby = strtolower( is_array( $orderby ) ? (string) current( $orderby ) : (string) $orderby );
|
|
|
|
$order = strtoupper( is_array( $order ) ? (string) current( $order ) : (string) $order );
|
2017-08-30 22:30:46 +00:00
|
|
|
$args = array(
|
2017-11-10 17:45:01 +00:00
|
|
|
'orderby' => $orderby,
|
|
|
|
'order' => ( 'DESC' === $order ) ? 'DESC' : 'ASC',
|
2017-11-09 11:50:50 +00:00
|
|
|
'meta_key' => '', // @codingStandardsIgnoreLine
|
2017-08-30 22:30:46 +00:00
|
|
|
);
|
2012-12-18 06:57:44 +00:00
|
|
|
|
|
|
|
switch ( $orderby ) {
|
2018-09-27 17:51:10 +00:00
|
|
|
case 'id':
|
|
|
|
$args['orderby'] = 'ID';
|
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'menu_order':
|
2018-03-21 03:13:22 +00:00
|
|
|
$args['orderby'] = 'menu_order title';
|
2017-11-10 17:45:01 +00:00
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'title':
|
|
|
|
$args['orderby'] = 'title';
|
|
|
|
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
|
|
|
|
break;
|
|
|
|
case 'relevance':
|
|
|
|
$args['orderby'] = 'relevance';
|
|
|
|
$args['order'] = 'DESC';
|
2017-11-10 17:45:01 +00:00
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'rand':
|
|
|
|
$args['orderby'] = 'rand'; // @codingStandardsIgnoreLine
|
2016-09-07 22:32:24 +00:00
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'date':
|
|
|
|
$args['orderby'] = 'date ID';
|
|
|
|
$args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
|
2016-09-07 22:32:24 +00:00
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'price':
|
2019-02-11 15:04:05 +00:00
|
|
|
$callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses';
|
|
|
|
add_filter( 'posts_clauses', array( $this, $callback ) );
|
2016-09-07 22:32:24 +00:00
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'popularity':
|
2013-07-17 10:21:48 +00:00
|
|
|
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
|
2016-09-07 22:32:24 +00:00
|
|
|
break;
|
2017-12-14 09:52:12 +00:00
|
|
|
case 'rating':
|
2019-02-11 15:04:05 +00:00
|
|
|
add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
|
2016-09-07 22:32:24 +00:00
|
|
|
break;
|
2012-06-13 23:52:21 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2018-12-18 05:07:18 +00:00
|
|
|
return apply_filters( 'woocommerce_get_catalog_ordering_args', $args, $orderby, $order );
|
2011-09-23 11:04:51 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2019-02-11 15:45:30 +00:00
|
|
|
/**
|
2019-02-12 14:04:22 +00:00
|
|
|
* Custom query used to filter products by price.
|
2019-02-11 15:45:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.6.0
|
|
|
|
*
|
|
|
|
* @param array $args Query args.
|
2020-07-05 12:39:26 +00:00
|
|
|
* @param WP_Query $wp_query WP_Query object.
|
2019-02-11 15:45:30 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function price_filter_post_clauses( $args, $wp_query ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2019-02-11 15:45:30 +00:00
|
|
|
if ( ! $wp_query->is_main_query() || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) {
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
|
|
|
$current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0;
|
|
|
|
$current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX;
|
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
2019-02-11 15:45:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust if the store taxes are not displayed how they are stored.
|
|
|
|
* Kicks in when prices excluding tax are displayed including tax.
|
|
|
|
*/
|
|
|
|
if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
|
2019-03-07 12:49:25 +00:00
|
|
|
$tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class.
|
|
|
|
$tax_rates = WC_Tax::get_rates( $tax_class );
|
2019-02-11 15:45:30 +00:00
|
|
|
|
2019-03-07 12:49:25 +00:00
|
|
|
if ( $tax_rates ) {
|
|
|
|
$current_min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_min_price, $tax_rates ) );
|
|
|
|
$current_max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_max_price, $tax_rates ) );
|
2019-02-11 15:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:01:45 +00:00
|
|
|
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
|
|
|
|
$args['where'] .= $wpdb->prepare(
|
2019-02-15 12:37:45 +00:00
|
|
|
' AND wc_product_meta_lookup.min_price >= %f AND wc_product_meta_lookup.max_price <= %f ',
|
2019-03-07 12:49:25 +00:00
|
|
|
$current_min_price,
|
|
|
|
$current_max_price
|
2019-02-11 15:45:30 +00:00
|
|
|
);
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2017-04-24 17:11:16 +00:00
|
|
|
/**
|
|
|
|
* Handle numeric price sorting.
|
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param array $args Query args.
|
2017-04-24 17:11:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function order_by_price_asc_post_clauses( $args ) {
|
2019-02-11 15:45:30 +00:00
|
|
|
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
|
2019-02-15 12:37:45 +00:00
|
|
|
$args['orderby'] = ' wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC ';
|
2017-04-24 17:11:16 +00:00
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle numeric price sorting.
|
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param array $args Query args.
|
2017-04-24 17:11:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function order_by_price_desc_post_clauses( $args ) {
|
2019-02-11 15:45:30 +00:00
|
|
|
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
|
2019-02-15 12:37:45 +00:00
|
|
|
$args['orderby'] = ' wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC ';
|
2017-04-24 17:11:16 +00:00
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2013-07-17 10:21:48 +00:00
|
|
|
/**
|
2019-02-11 15:04:05 +00:00
|
|
|
* WP Core does not let us change the sort direction for individual orderby params - https://core.trac.wordpress.org/ticket/17065.
|
2013-07-17 10:21:48 +00:00
|
|
|
*
|
|
|
|
* This lets us sort by meta value desc, and have a second orderby param.
|
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param array $args Query args.
|
2013-07-17 10:21:48 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function order_by_popularity_post_clauses( $args ) {
|
2019-02-11 15:45:30 +00:00
|
|
|
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
|
2019-02-15 12:37:45 +00:00
|
|
|
$args['orderby'] = ' wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC ';
|
2019-02-11 15:04:05 +00:00
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Order by rating post clauses.
|
|
|
|
*
|
|
|
|
* @param array $args Query args.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function order_by_rating_post_clauses( $args ) {
|
2019-02-11 15:45:30 +00:00
|
|
|
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
|
2020-07-06 14:19:17 +00:00
|
|
|
$args['orderby'] = ' wc_product_meta_lookup.average_rating DESC, wc_product_meta_lookup.rating_count DESC, wc_product_meta_lookup.product_id DESC ';
|
2013-07-17 10:21:48 +00:00
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:45:30 +00:00
|
|
|
/**
|
2019-02-15 12:37:45 +00:00
|
|
|
* Join wc_product_meta_lookup to posts if not already joined.
|
2019-02-11 15:45:30 +00:00
|
|
|
*
|
|
|
|
* @param string $sql SQL join.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function append_product_sorting_table_join( $sql ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2019-02-15 12:37:45 +00:00
|
|
|
if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {
|
|
|
|
$sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
|
2019-02-11 15:45:30 +00:00
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2013-04-17 15:29:43 +00:00
|
|
|
/**
|
|
|
|
* Appends meta queries to an array.
|
2016-07-11 13:48:47 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param array $meta_query Meta query.
|
|
|
|
* @param bool $main_query If is main query.
|
2013-10-04 16:48:50 +00:00
|
|
|
* @return array
|
2013-04-17 15:29:43 +00:00
|
|
|
*/
|
2016-12-08 10:56:45 +00:00
|
|
|
public function get_meta_query( $meta_query = array(), $main_query = false ) {
|
2016-02-09 21:14:55 +00:00
|
|
|
if ( ! is_array( $meta_query ) ) {
|
2013-04-17 15:29:43 +00:00
|
|
|
$meta_query = array();
|
2016-02-09 21:14:55 +00:00
|
|
|
}
|
|
|
|
return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) );
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:43:25 +00:00
|
|
|
/**
|
|
|
|
* Appends tax queries to an array.
|
2017-11-09 11:50:50 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param array $tax_query Tax query.
|
|
|
|
* @param bool $main_query If is main query.
|
2016-12-09 15:43:25 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_tax_query( $tax_query = array(), $main_query = false ) {
|
|
|
|
if ( ! is_array( $tax_query ) ) {
|
2017-11-20 22:19:09 +00:00
|
|
|
$tax_query = array(
|
|
|
|
'relation' => 'AND',
|
|
|
|
);
|
2016-12-09 15:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layered nav filters on terms.
|
2017-11-20 22:19:09 +00:00
|
|
|
if ( $main_query ) {
|
|
|
|
foreach ( $this->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
|
2016-12-09 15:43:25 +00:00
|
|
|
$tax_query[] = array(
|
|
|
|
'taxonomy' => $taxonomy,
|
|
|
|
'field' => 'slug',
|
|
|
|
'terms' => $data['terms'],
|
|
|
|
'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 16:32:00 +00:00
|
|
|
$product_visibility_terms = wc_get_product_visibility_term_ids();
|
2016-12-09 17:04:44 +00:00
|
|
|
$product_visibility_not_in = array( is_search() && $main_query ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] );
|
2016-12-09 15:43:25 +00:00
|
|
|
|
|
|
|
// Hide out of stock products.
|
|
|
|
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
|
2016-12-09 17:04:44 +00:00
|
|
|
$product_visibility_not_in[] = $product_visibility_terms['outofstock'];
|
2016-12-09 15:43:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:47:14 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
2016-12-09 15:43:25 +00:00
|
|
|
// Filter by rating.
|
2020-04-21 13:52:40 +00:00
|
|
|
if ( isset( $_GET['rating_filter'] ) ) {
|
|
|
|
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
2020-07-09 14:47:14 +00:00
|
|
|
$rating_filter = array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) );
|
2016-12-09 17:04:44 +00:00
|
|
|
$rating_terms = array();
|
2016-12-09 15:43:25 +00:00
|
|
|
for ( $i = 1; $i <= 5; $i ++ ) {
|
2017-11-09 11:50:50 +00:00
|
|
|
if ( in_array( $i, $rating_filter, true ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) {
|
2016-12-09 17:04:44 +00:00
|
|
|
$rating_terms[] = $product_visibility_terms[ 'rated-' . $i ];
|
2016-12-09 15:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! empty( $rating_terms ) ) {
|
|
|
|
$tax_query[] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'field' => 'term_taxonomy_id',
|
|
|
|
'terms' => $rating_terms,
|
|
|
|
'operator' => 'IN',
|
|
|
|
'rating_filter' => true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 14:47:14 +00:00
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
2016-12-09 15:43:25 +00:00
|
|
|
|
|
|
|
if ( ! empty( $product_visibility_not_in ) ) {
|
|
|
|
$tax_query[] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'field' => 'term_taxonomy_id',
|
|
|
|
'terms' => $product_visibility_not_in,
|
|
|
|
'operator' => 'NOT IN',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) );
|
|
|
|
}
|
|
|
|
|
2017-11-09 12:01:27 +00:00
|
|
|
/**
|
|
|
|
* Get the main query which product queries ran against.
|
|
|
|
*
|
2020-07-05 12:39:26 +00:00
|
|
|
* @return WP_Query
|
2017-11-09 12:01:27 +00:00
|
|
|
*/
|
|
|
|
public static function get_main_query() {
|
|
|
|
return self::$product_query;
|
|
|
|
}
|
|
|
|
|
2016-02-10 10:02:50 +00:00
|
|
|
/**
|
|
|
|
* Get the tax query which was used by the main query.
|
2017-11-09 11:50:50 +00:00
|
|
|
*
|
2016-02-10 10:02:50 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_main_tax_query() {
|
2017-11-09 12:01:27 +00:00
|
|
|
$tax_query = isset( self::$product_query->tax_query, self::$product_query->tax_query->queries ) ? self::$product_query->tax_query->queries : array();
|
2016-02-10 10:02:50 +00:00
|
|
|
|
|
|
|
return $tax_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the meta query which was used by the main query.
|
2017-11-09 11:50:50 +00:00
|
|
|
*
|
2016-02-10 10:02:50 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_main_meta_query() {
|
2017-11-09 12:01:27 +00:00
|
|
|
$args = self::$product_query->query_vars;
|
2016-02-10 10:02:50 +00:00
|
|
|
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
|
|
|
|
|
|
|
|
return $meta_query;
|
|
|
|
}
|
|
|
|
|
2016-07-19 13:09:31 +00:00
|
|
|
/**
|
|
|
|
* Based on WP_Query::parse_search
|
|
|
|
*/
|
|
|
|
public static function get_main_search_query_sql() {
|
2017-11-09 12:01:27 +00:00
|
|
|
global $wpdb;
|
2016-07-19 13:09:31 +00:00
|
|
|
|
2017-11-09 12:01:27 +00:00
|
|
|
$args = self::$product_query->query_vars;
|
2016-07-19 13:09:31 +00:00
|
|
|
$search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array();
|
|
|
|
$sql = array();
|
|
|
|
|
|
|
|
foreach ( $search_terms as $term ) {
|
|
|
|
// Terms prefixed with '-' should be excluded.
|
|
|
|
$include = '-' !== substr( $term, 0, 1 );
|
|
|
|
|
|
|
|
if ( $include ) {
|
|
|
|
$like_op = 'LIKE';
|
|
|
|
$andor_op = 'OR';
|
|
|
|
} else {
|
|
|
|
$like_op = 'NOT LIKE';
|
|
|
|
$andor_op = 'AND';
|
|
|
|
$term = substr( $term, 1 );
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:52:40 +00:00
|
|
|
$like = '%' . $wpdb->esc_like( $term ) . '%';
|
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
|
|
|
$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 );
|
2016-07-19 13:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $sql ) && ! is_user_logged_in() ) {
|
|
|
|
$sql[] = "($wpdb->posts.post_password = '')";
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode( ' AND ', $sql );
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
2017-12-25 08:16:10 +00:00
|
|
|
* Get an array of attributes and terms selected with the layered nav widget.
|
|
|
|
*
|
|
|
|
* @return array
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
2016-02-10 10:19:32 +00:00
|
|
|
public static function get_layered_nav_chosen_attributes() {
|
2020-04-21 13:52:40 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
|
|
|
if ( ! is_array( self::$chosen_attributes ) ) {
|
|
|
|
self::$chosen_attributes = array();
|
2016-02-10 10:19:32 +00:00
|
|
|
|
2020-04-21 13:52:40 +00:00
|
|
|
if ( ! empty( $_GET ) ) {
|
|
|
|
foreach ( $_GET as $key => $value ) {
|
2018-06-15 17:26:57 +00:00
|
|
|
if ( 0 === strpos( $key, 'filter_' ) ) {
|
|
|
|
$attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) );
|
|
|
|
$taxonomy = wc_attribute_taxonomy_name( $attribute );
|
|
|
|
$filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array();
|
2016-02-10 10:19:32 +00:00
|
|
|
|
2018-06-15 17:26:57 +00:00
|
|
|
if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2020-04-21 13:52:40 +00:00
|
|
|
$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 ] ) ) : '';
|
|
|
|
self::$chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding.
|
|
|
|
self::$chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
|
2018-06-15 17:26:57 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
2015-06-09 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 13:52:40 +00:00
|
|
|
return self::$chosen_attributes;
|
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 21:56:59 +00:00
|
|
|
/**
|
|
|
|
* Remove the add-to-cart param from pagination urls.
|
|
|
|
*
|
|
|
|
* @param string $url URL.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function remove_add_to_cart_pagination( $url ) {
|
|
|
|
return remove_query_arg( 'add-to-cart', $url );
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:50:50 +00:00
|
|
|
/**
|
|
|
|
* Return a meta query for filtering by rating.
|
|
|
|
*
|
|
|
|
* @deprecated 3.0.0 Replaced with taxonomy.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rating_filter_meta_query() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a meta query to handle product visibility.
|
|
|
|
*
|
|
|
|
* @deprecated 3.0.0 Replaced with taxonomy.
|
2020-04-21 13:52:40 +00:00
|
|
|
* @param string $compare (default: 'IN').
|
2017-11-09 11:50:50 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function visibility_meta_query( $compare = 'IN' ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a meta query to handle product stock status.
|
|
|
|
*
|
|
|
|
* @deprecated 3.0.0 Replaced with taxonomy.
|
2020-04-21 13:52:40 +00:00
|
|
|
* @param string $status (default: 'instock').
|
2017-11-09 11:50:50 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function stock_status_meta_query( $status = 'instock' ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2016-02-10 10:19:32 +00:00
|
|
|
/**
|
2017-11-20 22:19:09 +00:00
|
|
|
* Layered nav init.
|
|
|
|
*
|
2016-02-10 10:19:32 +00:00
|
|
|
* @deprecated 2.6.0
|
|
|
|
*/
|
2016-02-11 11:13:48 +00:00
|
|
|
public function layered_nav_init() {
|
2016-11-23 16:15:00 +00:00
|
|
|
wc_deprecated_function( 'layered_nav_init', '2.6' );
|
2016-02-11 11:13:48 +00:00
|
|
|
}
|
2016-02-10 10:19:32 +00:00
|
|
|
|
2015-11-13 09:41:44 +00:00
|
|
|
/**
|
2017-03-20 15:47:51 +00:00
|
|
|
* Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients.
|
2017-11-20 22:19:09 +00:00
|
|
|
*
|
2016-02-09 21:14:55 +00:00
|
|
|
* @deprecated 2.6.0 due to performance concerns
|
2015-11-13 09:41:44 +00:00
|
|
|
*/
|
2016-02-09 21:14:55 +00:00
|
|
|
public function get_products_in_view() {
|
2016-11-23 16:15:00 +00:00
|
|
|
wc_deprecated_function( 'get_products_in_view', '2.6' );
|
2015-11-13 09:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-09 21:14:55 +00:00
|
|
|
* Layered Nav post filter.
|
2017-11-20 22:19:09 +00:00
|
|
|
*
|
2016-02-09 21:14:55 +00:00
|
|
|
* @deprecated 2.6.0 due to performance concerns
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2017-11-20 22:19:09 +00:00
|
|
|
* @param mixed $deprecated Deprecated.
|
2015-11-13 09:41:44 +00:00
|
|
|
*/
|
2017-11-20 22:19:09 +00:00
|
|
|
public function layered_nav_query( $deprecated ) {
|
2016-11-23 16:15:00 +00:00
|
|
|
wc_deprecated_function( 'layered_nav_query', '2.6' );
|
2016-02-09 15:18:27 +00:00
|
|
|
}
|
2017-11-09 11:50:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search post excerpt.
|
|
|
|
*
|
2020-04-21 13:52:40 +00:00
|
|
|
* @param string $where Where clause.
|
|
|
|
*
|
2017-11-09 11:50:50 +00:00
|
|
|
* @deprecated 3.2.0 - Not needed anymore since WordPress 4.5.
|
|
|
|
*/
|
|
|
|
public function search_post_excerpt( $where = '' ) {
|
|
|
|
wc_deprecated_function( 'WC_Query::search_post_excerpt', '3.2.0', 'Excerpt added to search query by default since WordPress 4.5.' );
|
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the posts_where filter.
|
2020-04-21 13:52:40 +00:00
|
|
|
*
|
2017-11-09 11:50:50 +00:00
|
|
|
* @deprecated 3.2.0 - Nothing to remove anymore because search_post_excerpt() is deprecated.
|
|
|
|
*/
|
|
|
|
public function remove_posts_where() {
|
|
|
|
wc_deprecated_function( 'WC_Query::remove_posts_where', '3.2.0', 'Nothing to remove anymore because search_post_excerpt() is deprecated.' );
|
|
|
|
}
|
2013-07-23 16:05:01 +00:00
|
|
|
}
|