2011-09-16 08:10:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Contains the query functions for WooCommerce which alter the front-end post queries and loops.
|
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Query
|
2012-08-14 19:42:38 +00:00
|
|
|
* @version 1.6.4
|
2012-08-14 22:43:48 +00:00
|
|
|
* @package WooCommerce/Classes
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-08-14 19:42:38 +00:00
|
|
|
* @author WooThemes
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2013-07-23 16:05:01 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
|
|
|
if ( ! class_exists( 'WC_Query' ) ) :
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Query Class
|
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Query {
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-06-04 16:33:43 +00:00
|
|
|
/** @public array Query vars to add to wp */
|
2013-09-03 15:14:56 +00:00
|
|
|
public $query_vars = array();
|
2013-06-04 16:33:43 +00:00
|
|
|
|
2012-12-14 21:55:19 +00:00
|
|
|
/** @public array Unfiltered product ids (before layered nav etc) */
|
|
|
|
public $unfiltered_product_ids = array();
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2012-12-14 21:55:19 +00:00
|
|
|
/** @public array Filtered product ids (after layered nav) */
|
2013-01-02 20:11:17 +00:00
|
|
|
public $filtered_product_ids = array();
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
/** @public array Filtered product ids (after layered nav, per taxonomy) */
|
|
|
|
public $filtered_product_ids_for_taxonomy = array();
|
|
|
|
|
2012-12-14 21:55:19 +00:00
|
|
|
/** @public array Product IDs that match the layered nav + price filter */
|
2013-01-02 20:11:17 +00:00
|
|
|
public $post__in = array();
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2012-12-14 21:55:19 +00:00
|
|
|
/** @public array The meta query for the page */
|
2013-01-02 20:11:17 +00:00
|
|
|
public $meta_query = '';
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2012-12-14 21:55:19 +00:00
|
|
|
/** @public array Post IDs matching layered nav only */
|
2013-01-02 20:11:17 +00:00
|
|
|
public $layered_nav_post__in = array();
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2012-12-14 21:55:19 +00:00
|
|
|
/** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
|
2013-01-02 20:11:17 +00:00
|
|
|
public $layered_nav_product_ids = array();
|
2012-08-02 21:54:26 +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
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
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' ) );
|
2013-08-09 16:11:15 +00:00
|
|
|
add_action( 'init', array( $this, 'layered_nav_init' ) );
|
|
|
|
add_action( 'init', array( $this, 'price_filter_init' ) );
|
2013-07-23 16:05:01 +00:00
|
|
|
|
|
|
|
if ( ! is_admin() ) {
|
2013-07-26 14:36:28 +00:00
|
|
|
add_action( 'init', array( $this, 'get_errors' ) );
|
2013-07-23 16:05:01 +00:00
|
|
|
add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
|
|
|
|
add_action( 'parse_request', array( $this, 'parse_request'), 0 );
|
|
|
|
add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
|
|
|
|
add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 );
|
2013-10-21 12:05:34 +00:00
|
|
|
add_action( 'wp', array( $this, 'remove_product_query' ) );
|
|
|
|
add_action( 'wp', array( $this, 'remove_ordering_args' ) );
|
2013-07-23 16:05:01 +00:00
|
|
|
}
|
2013-09-03 15:14:56 +00:00
|
|
|
|
|
|
|
$this->init_query_vars();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init query vars by loading options.
|
|
|
|
*/
|
|
|
|
public function init_query_vars() {
|
|
|
|
// Query vars to add to WP
|
|
|
|
$this->query_vars = array(
|
|
|
|
// Checkout actions
|
2013-11-01 17:08:22 +00:00
|
|
|
'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
|
|
|
|
'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
|
2013-09-03 15:14:56 +00:00
|
|
|
|
|
|
|
// My account actions
|
2013-11-05 21:02:13 +00:00
|
|
|
'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
|
|
|
|
'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
|
|
|
|
'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
|
|
|
|
'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' ),
|
2013-09-03 15:14:56 +00:00
|
|
|
);
|
2013-07-23 16:05:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:36:28 +00:00
|
|
|
/**
|
|
|
|
* Get any errors from querystring
|
|
|
|
*/
|
|
|
|
public function get_errors() {
|
2014-01-26 09:19:17 +00:00
|
|
|
if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( $_GET['wc_error'] ) ) && ! wc_has_notice( $error, 'error' ) )
|
2014-01-07 13:35:01 +00:00
|
|
|
wc_add_notice( $error, 'error' );
|
2013-07-26 14:36:28 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 16:05:01 +00:00
|
|
|
/**
|
|
|
|
* Add endpoints for query vars
|
|
|
|
*/
|
|
|
|
public function add_endpoints() {
|
2013-09-03 15:14:56 +00:00
|
|
|
foreach ( $this->query_vars as $key => $var )
|
2013-07-23 16:05:01 +00:00
|
|
|
add_rewrite_endpoint( $var, EP_PAGES );
|
2011-09-16 08:10:12 +00:00
|
|
|
}
|
2011-12-22 19:55:08 +00:00
|
|
|
|
2013-05-31 15:13:14 +00:00
|
|
|
/**
|
|
|
|
* add_query_vars function.
|
|
|
|
*
|
|
|
|
* @access public
|
2013-11-29 18:50:31 +00:00
|
|
|
* @param array $vars
|
|
|
|
* @return array
|
2013-05-31 15:13:14 +00:00
|
|
|
*/
|
|
|
|
public function add_query_vars( $vars ) {
|
2013-09-03 15:14:56 +00:00
|
|
|
foreach ( $this->query_vars as $key => $var )
|
|
|
|
$vars[] = $key;
|
2013-06-04 16:33:43 +00:00
|
|
|
|
2013-05-31 15:13:14 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2014-02-12 12:18:36 +00:00
|
|
|
/**
|
|
|
|
* Get query vars
|
|
|
|
* @return array()
|
|
|
|
*/
|
|
|
|
public function get_query_vars() {
|
|
|
|
return $this->query_vars;
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:13:14 +00:00
|
|
|
/**
|
|
|
|
* Parse the request and look for query vars - endpoints may not be supported
|
|
|
|
*/
|
|
|
|
public function parse_request() {
|
|
|
|
global $wp;
|
|
|
|
|
2013-09-03 15:14:56 +00:00
|
|
|
// Map query vars to their keys, or get them if endpoints are not supported
|
|
|
|
foreach ( $this->query_vars as $key => $var ) {
|
|
|
|
if ( isset( $_GET[ $var ] ) ) {
|
|
|
|
$wp->query_vars[ $key ] = $_GET[ $var ];
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif ( isset( $wp->query_vars[ $var ] ) ) {
|
|
|
|
$wp->query_vars[ $key ] = $wp->query_vars[ $var ];
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 15:13:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
/**
|
2011-12-22 19:55:08 +00:00
|
|
|
* Hook into pre_get_posts to do the main product query
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $q query object
|
|
|
|
* @return void
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function pre_get_posts( $q ) {
|
2012-06-13 23:52:21 +00:00
|
|
|
// We only want to affect the main query
|
|
|
|
if ( ! $q->is_main_query() )
|
|
|
|
return;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-03-04 22:49:17 +00:00
|
|
|
// When orderby is set, WordPress shows posts. Get around that here.
|
2013-11-25 14:07:22 +00:00
|
|
|
if ( $q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') == wc_get_page_id('shop') ) {
|
2013-03-04 22:49:17 +00:00
|
|
|
$_query = wp_parse_args( $q->query );
|
|
|
|
if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
|
|
|
|
$q->is_page = true;
|
|
|
|
$q->is_home = false;
|
|
|
|
$q->set( 'page_id', get_option('page_on_front') );
|
|
|
|
$q->set( 'post_type', 'product' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-13 23:52:21 +00:00
|
|
|
// Special check for shops with the product archive on front
|
2013-11-25 14:07:22 +00:00
|
|
|
if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == wc_get_page_id('shop') ) {
|
2012-08-02 21:54:26 +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', '' );
|
|
|
|
if ( isset( $q->query['paged'] ) )
|
|
|
|
$q->set( 'paged', $q->query['paged'] );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-29 09:23:21 +00:00
|
|
|
// Define a variable so we know this is the front page shop later on
|
|
|
|
define( 'SHOP_IS_ON_FRONT', true );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-29 09:23:21 +00:00
|
|
|
// Get the actual WP page to avoid errors and let us use is_front_page()
|
|
|
|
// This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
|
|
|
|
global $wp_post_types;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-11-25 14:07:22 +00:00
|
|
|
$shop_page = get_post( wc_get_page_id('shop') );
|
2012-08-22 19:44:27 +00:00
|
|
|
$q->is_page = true;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-29 09:23:21 +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;
|
2014-05-21 13:30:51 +00:00
|
|
|
$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
|
|
|
|
2012-06-29 09:23:21 +00:00
|
|
|
// Fix conditional Functions like is_front_page
|
2013-01-02 20:11:17 +00:00
|
|
|
$q->is_singular = false;
|
|
|
|
$q->is_post_type_archive = true;
|
|
|
|
$q->is_archive = true;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-02 20:11:17 +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' ) );
|
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-13 23:52:21 +00:00
|
|
|
} else {
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-13 23:52:21 +00:00
|
|
|
// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
|
2012-12-20 13:57:38 +00:00
|
|
|
if ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) )
|
2012-06-13 23:52:21 +00:00
|
|
|
return;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
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 );
|
2013-01-12 10:53:08 +00:00
|
|
|
|
2013-01-02 20:47:56 +00:00
|
|
|
if ( is_search() ) {
|
2013-01-02 21:43:52 +00:00
|
|
|
add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
|
|
|
|
add_filter( 'wp', array( $this, 'remove_posts_where' ) );
|
2013-01-02 20:47:56 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-08-19 17:19:27 +00:00
|
|
|
add_filter( 'posts_where', array( $this, 'exclude_protected_products' ) );
|
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
// We're on a shop page so queue the woocommerce_get_products_in_view function
|
|
|
|
add_action( 'wp', array( $this, 'get_products_in_view' ), 2);
|
2013-01-12 10:53:08 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
// And remove the pre_get_posts hook
|
|
|
|
$this->remove_product_query();
|
2011-12-22 19:55:08 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-02 20:47:56 +00:00
|
|
|
/**
|
2013-01-12 10:53:08 +00:00
|
|
|
* search_post_excerpt function.
|
2013-01-02 20:47:56 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $where (default: '')
|
|
|
|
* @return string (modified where clause)
|
|
|
|
*/
|
|
|
|
public function search_post_excerpt( $where = '' ) {
|
|
|
|
global $wp_the_query;
|
2013-01-12 10:53:08 +00:00
|
|
|
|
2013-01-02 20:47:56 +00:00
|
|
|
// If this is not a WC Query, do not modify the query
|
2013-09-13 14:14:19 +00:00
|
|
|
if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
|
2013-01-02 20:47:56 +00:00
|
|
|
return $where;
|
|
|
|
|
|
|
|
$where = preg_replace(
|
2013-09-13 14:14:19 +00:00
|
|
|
"/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
|
2013-01-12 10:53:08 +00:00
|
|
|
"post_title LIKE $1) OR (post_excerpt LIKE $1", $where );
|
|
|
|
|
2013-01-02 20:47:56 +00:00
|
|
|
return $where;
|
|
|
|
}
|
2012-08-22 19:44:27 +00:00
|
|
|
|
2013-08-19 17:19:27 +00:00
|
|
|
/**
|
|
|
|
* Prevent password protected products appearing in the loops
|
|
|
|
*
|
|
|
|
* @param string $where
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function exclude_protected_products( $where ) {
|
2014-02-24 10:47:19 +00:00
|
|
|
global $wpdb;
|
|
|
|
$where .= " AND {$wpdb->posts}.post_password = ''";
|
2013-08-19 17:19:27 +00:00
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:44:27 +00:00
|
|
|
/**
|
|
|
|
* wpseo_metadesc function.
|
2013-12-02 11:34:27 +00:00
|
|
|
* Hooked into wpseo_ hook already, so no need for function_exist
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-08-22 19:44:27 +00:00
|
|
|
* @access public
|
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() {
|
2014-05-07 10:40:21 +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
|
|
|
/**
|
|
|
|
* wpseo_metakey function.
|
2013-12-02 11:34:27 +00:00
|
|
|
* Hooked into wpseo_ hook already, so no need for function_exist
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-08-22 19:44:27 +00:00
|
|
|
* @access public
|
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() {
|
2014-05-07 10:40:21 +00:00
|
|
|
return WPSEO_Meta::get_value( 'metakey', wc_get_page_id('shop') );
|
2012-08-22 19:44:27 +00:00
|
|
|
}
|
2012-03-26 14:34:23 +00:00
|
|
|
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2012-03-26 14:34:23 +00:00
|
|
|
/**
|
|
|
|
* Hook into the_posts to do the main product query if needed - relevanssi compatibility
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2013-11-29 18:50:31 +00:00
|
|
|
* @param array $posts
|
|
|
|
* @param WP_Query|bool $query (default: false)
|
|
|
|
* @return array
|
2012-03-26 14:34:23 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function the_posts( $posts, $query = false ) {
|
2013-03-03 17:07:31 +00:00
|
|
|
// Abort if there's no query
|
2012-06-13 23:52:21 +00:00
|
|
|
if ( ! $query )
|
|
|
|
return $posts;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-13 23:52:21 +00:00
|
|
|
// Abort if we're not filtering posts
|
|
|
|
if ( empty( $this->post__in ) )
|
|
|
|
return $posts;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-13 23:52:21 +00:00
|
|
|
// Abort if this query has already been done
|
|
|
|
if ( ! empty( $query->wc_query ) )
|
|
|
|
return $posts;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
|
|
|
// Abort if this isn't a search query
|
2012-06-13 23:52:21 +00:00
|
|
|
if ( empty( $query->query_vars["s"] ) )
|
|
|
|
return $posts;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-06-13 23:52:21 +00:00
|
|
|
// Abort if we're not on a post type archive/product taxonomy
|
2012-12-20 13:57:38 +00:00
|
|
|
if ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( get_object_taxonomies( 'product' ) ) )
|
2012-06-13 23:52:21 +00:00
|
|
|
return $posts;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
$filtered_posts = array();
|
|
|
|
$queried_post_ids = array();
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
foreach ( $posts as $post ) {
|
|
|
|
if ( in_array( $post->ID, $this->post__in ) ) {
|
|
|
|
$filtered_posts[] = $post;
|
|
|
|
$queried_post_ids[] = $post->ID;
|
|
|
|
}
|
|
|
|
}
|
2012-03-26 14:34:23 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
$query->posts = $filtered_posts;
|
|
|
|
$query->post_count = count( $filtered_posts );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
// Ensure filters are set
|
|
|
|
$this->unfiltered_product_ids = $queried_post_ids;
|
|
|
|
$this->filtered_product_ids = $queried_post_ids;
|
2012-03-26 14:34:23 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
|
|
|
|
$this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
|
|
|
|
} else {
|
|
|
|
$this->layered_nav_product_ids = $this->unfiltered_product_ids;
|
|
|
|
}
|
2012-03-26 14:34:23 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
return $filtered_posts;
|
2012-03-26 14:34:23 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2011-12-22 19:55:08 +00:00
|
|
|
/**
|
|
|
|
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $q
|
|
|
|
* @return void
|
2011-12-22 19:55:08 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function product_query( $q ) {
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-23 11:04:51 +00:00
|
|
|
// Meta query
|
2013-04-17 15:29:43 +00:00
|
|
|
$meta_query = $this->get_meta_query( $q->get( 'meta_query' ) );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// Ordering
|
2014-02-04 13:44:18 +00:00
|
|
|
$ordering = $this->get_catalog_ordering_args();
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// Get a list of post id's which match the current filters set (in the layered nav and price filter)
|
2014-02-04 13:44:18 +00:00
|
|
|
$post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// Ordering query vars
|
2011-09-23 11:04:51 +00:00
|
|
|
$q->set( 'orderby', $ordering['orderby'] );
|
|
|
|
$q->set( 'order', $ordering['order'] );
|
2012-08-02 21:54:26 +00:00
|
|
|
if ( isset( $ordering['meta_key'] ) )
|
2012-05-17 12:56:18 +00:00
|
|
|
$q->set( 'meta_key', $ordering['meta_key'] );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// Query vars that affect posts shown
|
|
|
|
$q->set( 'meta_query', $meta_query );
|
2013-01-02 20:11:17 +00:00
|
|
|
$q->set( 'post__in', $post__in );
|
|
|
|
$q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', get_option( 'posts_per_page' ) ) );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
// Set a special variable
|
|
|
|
$q->set( 'wc_query', true );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
// Store variables
|
2013-04-17 15:29:43 +00:00
|
|
|
$this->post__in = $post__in;
|
2013-01-02 20:11:17 +00:00
|
|
|
$this->meta_query = $meta_query;
|
2012-08-02 21:54:26 +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
|
|
|
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
/**
|
2011-12-22 19:55:08 +00:00
|
|
|
* Remove the query
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function remove_product_query() {
|
2012-12-15 12:00:34 +00:00
|
|
|
remove_filter( '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
|
|
|
/**
|
|
|
|
* Remove ordering queries
|
|
|
|
*/
|
|
|
|
public function remove_ordering_args() {
|
|
|
|
remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
|
|
|
|
remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
|
|
|
|
}
|
|
|
|
|
2013-01-02 21:43:52 +00:00
|
|
|
/**
|
|
|
|
* Remove the posts_where filter
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-01-12 10:53:08 +00:00
|
|
|
public function remove_posts_where() {
|
2013-01-02 21:43:52 +00:00
|
|
|
remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
|
|
|
|
}
|
2013-01-12 10:53:08 +00:00
|
|
|
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
/**
|
2011-11-13 02:15:00 +00:00
|
|
|
* Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
2011-09-16 08:10:12 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function get_products_in_view() {
|
2012-06-13 23:52:21 +00:00
|
|
|
global $wp_the_query;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
$unfiltered_product_ids = array();
|
2012-06-13 23:52:21 +00:00
|
|
|
|
|
|
|
// Get main query
|
|
|
|
$current_wp_query = $wp_the_query->query;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-11-13 02:15:00 +00:00
|
|
|
// Get WP Query for current page (without 'paged')
|
2012-05-17 12:56:18 +00:00
|
|
|
unset( $current_wp_query['paged'] );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-11-13 02:15:00 +00:00
|
|
|
// Generate a transient name based on current query
|
2012-06-13 23:52:21 +00:00
|
|
|
$transient_name = 'wc_uf_pid_' . md5( http_build_query( $current_wp_query ) );
|
2012-05-17 12:56:18 +00:00
|
|
|
$transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-03-02 21:34:17 +00:00
|
|
|
if ( false === ( $unfiltered_product_ids = get_transient( $transient_name ) ) ) {
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
// Get all visible posts, regardless of filters
|
2011-11-13 02:15:00 +00:00
|
|
|
$unfiltered_product_ids = get_posts(
|
2012-08-02 21:54:26 +00:00
|
|
|
array_merge(
|
2011-11-13 02:15:00 +00:00
|
|
|
$current_wp_query,
|
|
|
|
array(
|
|
|
|
'post_type' => 'product',
|
|
|
|
'numberposts' => -1,
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'meta_query' => $this->meta_query,
|
|
|
|
'fields' => 'ids',
|
2012-06-13 23:52:21 +00:00
|
|
|
'no_found_rows' => true,
|
|
|
|
'update_post_meta_cache' => false,
|
|
|
|
'update_post_term_cache' => false
|
2011-11-13 02:15:00 +00:00
|
|
|
)
|
2011-09-16 08:10:12 +00:00
|
|
|
)
|
2011-11-13 02:15:00 +00:00
|
|
|
);
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2014-03-12 11:58:41 +00:00
|
|
|
set_transient( $transient_name, $unfiltered_product_ids, YEAR_IN_SECONDS );
|
2012-03-02 21:34:17 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// Store the variable
|
2012-03-02 21:34:17 +00:00
|
|
|
$this->unfiltered_product_ids = $unfiltered_product_ids;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// Also store filtered posts ids...
|
2012-06-13 23:52:21 +00:00
|
|
|
if ( sizeof( $this->post__in ) > 0 )
|
2012-03-26 14:34:23 +00:00
|
|
|
$this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $this->post__in );
|
2012-06-13 23:52:21 +00:00
|
|
|
else
|
2011-09-16 08:10:12 +00:00
|
|
|
$this->filtered_product_ids = $this->unfiltered_product_ids;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-16 08:10:12 +00:00
|
|
|
// And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
|
2012-06-13 23:52:21 +00:00
|
|
|
if ( sizeof( $this->layered_nav_post__in ) > 0 )
|
2012-03-26 14:34:23 +00:00
|
|
|
$this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
|
2012-06-13 23:52:21 +00:00
|
|
|
else
|
2011-09-16 08:10:12 +00:00
|
|
|
$this->layered_nav_product_ids = $this->unfiltered_product_ids;
|
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2011-09-23 11:04:51 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of arguments for ordering products based on the selected values
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @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 = '' ) {
|
2013-01-12 10:53:08 +00:00
|
|
|
// Get ordering from query string unless defined
|
|
|
|
if ( ! $orderby ) {
|
2013-11-25 13:34:21 +00:00
|
|
|
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
|
2013-01-12 10:53:08 +00:00
|
|
|
|
|
|
|
// Get order + orderby args from string
|
|
|
|
$orderby_value = explode( '-', $orderby_value );
|
|
|
|
$orderby = esc_attr( $orderby_value[0] );
|
|
|
|
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
|
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-03-22 17:06:06 +00:00
|
|
|
$orderby = strtolower( $orderby );
|
|
|
|
$order = strtoupper( $order );
|
|
|
|
|
2012-12-18 06:57:44 +00:00
|
|
|
$args = array();
|
|
|
|
|
2013-07-17 10:21:48 +00:00
|
|
|
// default - menu_order
|
|
|
|
$args['orderby'] = 'menu_order title';
|
|
|
|
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
|
|
|
|
$args['meta_key'] = '';
|
|
|
|
|
2012-12-18 06:57:44 +00:00
|
|
|
switch ( $orderby ) {
|
2013-12-05 12:53:22 +00:00
|
|
|
case 'rand' :
|
|
|
|
$args['orderby'] = 'rand';
|
|
|
|
break;
|
2011-09-23 11:04:51 +00:00
|
|
|
case 'date' :
|
2012-12-18 06:57:44 +00:00
|
|
|
$args['orderby'] = 'date';
|
2013-03-22 17:06:06 +00:00
|
|
|
$args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
|
2011-09-23 11:04:51 +00:00
|
|
|
break;
|
|
|
|
case 'price' :
|
2012-12-18 06:57:44 +00:00
|
|
|
$args['orderby'] = 'meta_value_num';
|
2013-03-22 17:06:06 +00:00
|
|
|
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
|
2012-12-18 06:57:44 +00:00
|
|
|
$args['meta_key'] = '_price';
|
2011-09-23 11:04:51 +00:00
|
|
|
break;
|
2013-01-12 10:53:08 +00:00
|
|
|
case 'popularity' :
|
|
|
|
$args['meta_key'] = 'total_sales';
|
2013-07-17 10:21:48 +00:00
|
|
|
|
|
|
|
// Sorting handled later though a hook
|
|
|
|
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
|
2012-10-14 12:06:37 +00:00
|
|
|
break;
|
2013-01-12 10:53:08 +00:00
|
|
|
case 'rating' :
|
2013-07-17 10:21:48 +00:00
|
|
|
// Sorting handled later though a hook
|
2013-01-12 10:53:08 +00:00
|
|
|
add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
|
2011-09-23 11:04:51 +00:00
|
|
|
break;
|
2013-03-22 17:06:06 +00:00
|
|
|
case 'title' :
|
|
|
|
$args['orderby'] = 'title';
|
|
|
|
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
|
2012-05-08 19:30:18 +00:00
|
|
|
break;
|
2012-06-13 23:52:21 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2012-12-18 06:57:44 +00:00
|
|
|
return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
|
2011-09-23 11:04:51 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-07-17 10:21:48 +00:00
|
|
|
/**
|
|
|
|
* WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065
|
|
|
|
*
|
|
|
|
* This lets us sort by meta value desc, and have a second orderby param.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function order_by_popularity_post_clauses( $args ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2013-01-12 10:53:08 +00:00
|
|
|
/**
|
|
|
|
* order_by_rating_post_clauses function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function order_by_rating_post_clauses( $args ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
|
|
|
|
|
|
|
|
$args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
|
|
|
|
|
|
|
|
$args['join'] .= "
|
|
|
|
LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
|
|
|
|
LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
|
|
|
|
";
|
|
|
|
|
2013-07-17 10:21:48 +00:00
|
|
|
$args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC";
|
2013-01-12 10:53:08 +00:00
|
|
|
|
|
|
|
$args['groupby'] = "$wpdb->posts.ID";
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
2012-08-14 20:19:41 +00:00
|
|
|
|
2013-04-17 15:29:43 +00:00
|
|
|
/**
|
|
|
|
* Appends meta queries to an array.
|
|
|
|
* @access public
|
2013-10-04 16:48:50 +00:00
|
|
|
* @param array $meta_query
|
|
|
|
* @return array
|
2013-04-17 15:29:43 +00:00
|
|
|
*/
|
|
|
|
public function get_meta_query( $meta_query = array() ) {
|
|
|
|
if ( ! is_array( $meta_query ) )
|
|
|
|
$meta_query = array();
|
|
|
|
|
|
|
|
$meta_query[] = $this->visibility_meta_query();
|
|
|
|
$meta_query[] = $this->stock_status_meta_query();
|
|
|
|
|
|
|
|
return array_filter( $meta_query );
|
|
|
|
}
|
|
|
|
|
2011-09-23 11:04:51 +00:00
|
|
|
/**
|
|
|
|
* Returns a meta query to handle product visibility
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $compare (default: 'IN')
|
|
|
|
* @return array
|
2011-09-23 11:04:51 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function visibility_meta_query( $compare = 'IN' ) {
|
2013-04-17 15:29:43 +00:00
|
|
|
if ( is_search() )
|
|
|
|
$in = array( 'visible', 'search' );
|
|
|
|
else
|
|
|
|
$in = array( 'visible', 'catalog' );
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
$meta_query = array(
|
2013-04-17 15:29:43 +00:00
|
|
|
'key' => '_visibility',
|
|
|
|
'value' => $in,
|
2013-01-02 20:11:17 +00:00
|
|
|
'compare' => $compare
|
|
|
|
);
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-01-02 20:11:17 +00:00
|
|
|
return $meta_query;
|
2011-09-23 11:04:51 +00:00
|
|
|
}
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2011-09-23 11:04:51 +00:00
|
|
|
/**
|
|
|
|
* Returns a meta query to handle product stock status
|
2012-08-14 20:19:41 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $status (default: 'instock')
|
|
|
|
* @return array
|
2011-09-23 11:04:51 +00:00
|
|
|
*/
|
2012-12-14 21:55:19 +00:00
|
|
|
public function stock_status_meta_query( $status = 'instock' ) {
|
2011-09-23 11:04:51 +00:00
|
|
|
$meta_query = array();
|
2012-06-13 23:52:21 +00:00
|
|
|
if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
|
2011-09-23 11:04:51 +00:00
|
|
|
$meta_query = array(
|
2011-12-24 17:05:51 +00:00
|
|
|
'key' => '_stock_status',
|
2011-09-23 11:04:51 +00:00
|
|
|
'value' => $status,
|
|
|
|
'compare' => '='
|
|
|
|
);
|
2012-06-13 23:52:21 +00:00
|
|
|
}
|
2011-09-23 11:04:51 +00:00
|
|
|
return $meta_query;
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Layered Nav Init
|
|
|
|
*/
|
|
|
|
public function layered_nav_init( ) {
|
|
|
|
|
|
|
|
if ( is_active_widget( false, false, 'woocommerce_layered_nav', true ) && ! is_admin() ) {
|
|
|
|
|
|
|
|
global $_chosen_attributes;
|
|
|
|
|
|
|
|
$_chosen_attributes = array();
|
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
$attribute_taxonomies = wc_get_attribute_taxonomies();
|
2013-08-09 16:11:15 +00:00
|
|
|
if ( $attribute_taxonomies ) {
|
|
|
|
foreach ( $attribute_taxonomies as $tax ) {
|
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
$attribute = wc_sanitize_taxonomy_name( $tax->attribute_name );
|
2013-11-18 13:45:54 +00:00
|
|
|
$taxonomy = wc_attribute_taxonomy_name( $attribute );
|
|
|
|
$name = 'filter_' . $attribute;
|
|
|
|
$query_type_name = 'query_type_' . $attribute;
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
if ( ! empty( $_GET[ $name ] ) && taxonomy_exists( $taxonomy ) ) {
|
|
|
|
|
|
|
|
$_chosen_attributes[ $taxonomy ]['terms'] = explode( ',', $_GET[ $name ] );
|
|
|
|
|
|
|
|
if ( empty( $_GET[ $query_type_name ] ) || ! in_array( strtolower( $_GET[ $query_type_name ] ), array( 'and', 'or' ) ) )
|
|
|
|
$_chosen_attributes[ $taxonomy ]['query_type'] = apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
|
|
|
|
else
|
|
|
|
$_chosen_attributes[ $taxonomy ]['query_type'] = strtolower( $_GET[ $query_type_name ] );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add_filter('loop_shop_post_in', array( $this, 'layered_nav_query' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Layered Nav post filter
|
|
|
|
*
|
|
|
|
* @param array $filtered_posts
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function layered_nav_query( $filtered_posts ) {
|
2014-06-08 20:33:11 +00:00
|
|
|
global $_chosen_attributes;
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
if ( sizeof( $_chosen_attributes ) > 0 ) {
|
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
$matched_products = array(
|
|
|
|
'and' => array(),
|
|
|
|
'or' => array()
|
|
|
|
);
|
|
|
|
$filtered_attribute = array(
|
|
|
|
'and' => false,
|
|
|
|
'or' => false
|
|
|
|
);
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
foreach ( $_chosen_attributes as $attribute => $data ) {
|
|
|
|
$matched_products_from_attribute = array();
|
|
|
|
$filtered = false;
|
|
|
|
|
|
|
|
if ( sizeof( $data['terms'] ) > 0 ) {
|
|
|
|
foreach ( $data['terms'] as $value ) {
|
|
|
|
|
|
|
|
$posts = get_posts(
|
|
|
|
array(
|
|
|
|
'post_type' => 'product',
|
|
|
|
'numberposts' => -1,
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'fields' => 'ids',
|
|
|
|
'no_found_rows' => true,
|
|
|
|
'tax_query' => array(
|
|
|
|
array(
|
|
|
|
'taxonomy' => $attribute,
|
|
|
|
'terms' => $value,
|
|
|
|
'field' => 'id'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
if ( ! is_wp_error( $posts ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
if ( sizeof( $matched_products_from_attribute ) > 0 || $filtered )
|
|
|
|
$matched_products_from_attribute = $data['query_type'] == 'or' ? array_merge( $posts, $matched_products_from_attribute ) : array_intersect( $posts, $matched_products_from_attribute );
|
|
|
|
else
|
2013-08-09 16:11:15 +00:00
|
|
|
$matched_products_from_attribute = $posts;
|
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
$filtered = true;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
if ( sizeof( $matched_products[ $data['query_type'] ] ) > 0 || $filtered_attribute[ $data['query_type'] ] === true ) {
|
|
|
|
$matched_products[ $data['query_type'] ] = ( $data['query_type'] == 'or' ) ? array_merge( $matched_products_from_attribute, $matched_products[ $data['query_type'] ] ) : array_intersect( $matched_products_from_attribute, $matched_products[ $data['query_type'] ] );
|
|
|
|
} else {
|
|
|
|
$matched_products[ $data['query_type'] ] = $matched_products_from_attribute;
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
$filtered_attribute[ $data['query_type'] ] = true;
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
$this->filtered_product_ids_for_taxonomy[ $attribute ] = $matched_products_from_attribute;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
// Combine our AND and OR result sets
|
|
|
|
if ( $filtered_attribute['and'] && $filtered_attribute['or'] )
|
|
|
|
$results = array_intersect( $matched_products[ 'and' ], $matched_products[ 'or' ] );
|
|
|
|
else
|
|
|
|
$results = array_merge( $matched_products[ 'and' ], $matched_products[ 'or' ] );
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
if ( $filtered ) {
|
|
|
|
|
2013-09-27 08:55:04 +00:00
|
|
|
WC()->query->layered_nav_post__in = $results;
|
2013-08-09 16:11:15 +00:00
|
|
|
WC()->query->layered_nav_post__in[] = 0;
|
|
|
|
|
|
|
|
if ( sizeof( $filtered_posts ) == 0 ) {
|
2013-09-27 08:55:04 +00:00
|
|
|
$filtered_posts = $results;
|
2013-08-09 16:11:15 +00:00
|
|
|
$filtered_posts[] = 0;
|
|
|
|
} else {
|
2013-09-27 08:55:04 +00:00
|
|
|
$filtered_posts = array_intersect( $filtered_posts, $results );
|
2013-08-09 16:11:15 +00:00
|
|
|
$filtered_posts[] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (array) $filtered_posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Price filter Init
|
|
|
|
*/
|
|
|
|
public function price_filter_init() {
|
|
|
|
if ( is_active_widget( false, false, 'woocommerce_price_filter', true ) && ! is_admin() ) {
|
|
|
|
|
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
|
|
|
|
|
2014-02-12 09:23:06 +00:00
|
|
|
wp_register_script( 'wc-price-slider', WC()->plugin_url() . '/assets/js/frontend/price-slider' . $suffix . '.js', array( 'jquery-ui-slider' ), WC_VERSION, true );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
wp_localize_script( 'wc-price-slider', 'woocommerce_price_slider_params', array(
|
|
|
|
'currency_symbol' => get_woocommerce_currency_symbol(),
|
|
|
|
'currency_pos' => get_option( 'woocommerce_currency_pos' ),
|
|
|
|
'min_price' => isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : '',
|
|
|
|
'max_price' => isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : ''
|
|
|
|
) );
|
|
|
|
|
|
|
|
add_filter( 'loop_shop_post_in', array( $this, 'price_filter' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Price Filter post filter
|
|
|
|
*
|
|
|
|
* @param array $filtered_posts
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-09-30 12:58:12 +00:00
|
|
|
public function price_filter( $filtered_posts ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) {
|
|
|
|
|
|
|
|
$matched_products = array();
|
|
|
|
$min = floatval( $_GET['min_price'] );
|
|
|
|
$max = floatval( $_GET['max_price'] );
|
|
|
|
|
2013-09-30 12:58:12 +00:00
|
|
|
$matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare("
|
2013-08-09 16:11:15 +00:00
|
|
|
SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
|
|
|
|
INNER JOIN $wpdb->postmeta ON ID = post_id
|
|
|
|
WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d
|
2013-09-30 12:58:12 +00:00
|
|
|
", '_price', $min, $max ), OBJECT_K ), $min, $max );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
if ( $matched_products_query ) {
|
|
|
|
foreach ( $matched_products_query as $product ) {
|
|
|
|
if ( $product->post_type == 'product' )
|
|
|
|
$matched_products[] = $product->ID;
|
|
|
|
if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) )
|
|
|
|
$matched_products[] = $product->post_parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the id's
|
|
|
|
if ( sizeof( $filtered_posts ) == 0) {
|
|
|
|
$filtered_posts = $matched_products;
|
|
|
|
$filtered_posts[] = 0;
|
|
|
|
} else {
|
|
|
|
$filtered_posts = array_intersect( $filtered_posts, $matched_products );
|
|
|
|
$filtered_posts[] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (array) $filtered_posts;
|
|
|
|
}
|
|
|
|
|
2013-07-23 16:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endif;
|
2012-08-02 21:54:26 +00:00
|
|
|
|
2013-12-02 11:34:27 +00:00
|
|
|
return new WC_Query();
|