woocommerce/woocommerce_query.php

132 lines
4.0 KiB
PHP
Raw Normal View History

2011-08-10 17:11:11 +00:00
<?php
/**
* WooCommerce Queries
*
* Handles front end queries and loops.
*
* @package WooCommerce
* @category Core
* @author WooThemes
*/
2011-08-31 00:29:02 +00:00
/**
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
*/
2011-09-04 00:02:44 +00:00
add_filter( 'parse_query', 'woocommerce_parse_query' );
2011-08-31 00:29:02 +00:00
function woocommerce_parse_query( $q ) {
2011-08-10 17:11:11 +00:00
2011-09-04 00:02:44 +00:00
if (is_admin()) return;
// Apply to main loop only
remove_filter( 'parse_query', 'woocommerce_parse_query' );
2011-08-31 00:29:02 +00:00
// Only apply to product categories, the product post archive, the shop page, and product tags
2011-09-01 08:33:22 +00:00
if (true == $q->query_vars['suppress_filters'] || (!$q->is_tax( 'product_cat' ) && !$q->is_post_type_archive( 'product' ) && !$q->is_page( get_option('woocommerce_shop_page_id') ) && !$q->is_tax( 'product_tag' ))) return;
2011-08-10 17:11:11 +00:00
2011-09-04 00:02:44 +00:00
$meta_query = (array) $q->get( 'meta_query' );
2011-08-10 17:11:11 +00:00
2011-08-31 00:29:02 +00:00
// Visibility
if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
2011-09-04 00:02:44 +00:00
$meta_query[] = array(
2011-08-31 00:29:02 +00:00
'key' => 'visibility',
'value' => $in,
'compare' => 'IN'
);
// In stock
if (get_option('woocommerce_hide_out_of_stock_items')=='yes') :
2011-09-04 00:02:44 +00:00
$meta_query[] = array(
2011-08-31 00:29:02 +00:00
'key' => 'stock_status',
'value' => 'instock',
'compare' => '='
);
2011-08-10 17:11:11 +00:00
endif;
2011-08-31 00:29:02 +00:00
// Ordering
2011-09-01 08:33:22 +00:00
if (isset($_POST['orderby']) && $_POST['orderby'] != '') $_SESSION['orderby'] = $_POST['orderby'];
$current_order = (isset($_SESSION['orderby'])) ? $_SESSION['orderby'] : 'title';
2011-08-31 00:29:02 +00:00
switch ($current_order) :
case 'date' :
$orderby = 'date';
$order = 'desc';
$meta_key = '';
break;
case 'price' :
$orderby = 'meta_value_num';
$order = 'asc';
$meta_key = 'price';
break;
default :
$orderby = 'title';
$order = 'asc';
$meta_key = '';
break;
endswitch;
// Get a list of post id's which match the current filters set (in the layered nav and price filter)
2011-09-04 00:02:44 +00:00
$post__in = array_unique(apply_filters('loop-shop-posts-in', array()));
2011-08-31 00:29:02 +00:00
// Ordering query vars
$q->set( 'orderby', $orderby );
$q->set( 'order', $order );
$q->set( 'meta_key', $meta_key );
2011-08-10 17:11:11 +00:00
2011-08-31 00:29:02 +00:00
// Query vars that affect posts shown
$q->set( 'post_type', 'product' );
2011-09-04 00:02:44 +00:00
$q->set( 'meta_query', $meta_query );
$q->set( 'post__in', $post__in );
2011-09-01 09:08:33 +00:00
$q->set( 'posts_per_page', apply_filters('loop_shop_per_page', get_option('posts_per_page')) );
2011-09-04 00:02:44 +00:00
// Store variables
woocommerce::$query['post__in'] = $post__in;
woocommerce::$query['meta_query'] = $meta_query;
2011-08-10 17:11:11 +00:00
2011-08-31 00:29:02 +00:00
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action('wp', 'woocommerce_get_products_in_view', 2);
}
2011-08-10 17:11:11 +00:00
/**
2011-08-31 00:29:02 +00:00
* Get an unpaginated list all product ID's (both filtered and unfiltered)
2011-08-10 17:11:11 +00:00
*/
2011-08-31 00:29:02 +00:00
function woocommerce_get_products_in_view() {
2011-08-10 17:11:11 +00:00
2011-09-04 00:02:44 +00:00
global $wp_query;
$unfiltered_product_ids = array();
2011-08-10 17:11:11 +00:00
2011-08-31 00:29:02 +00:00
// Get all visible posts, regardless of filters
$products = get_posts(
array_merge(
$wp_query->query,
array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
2011-09-04 00:02:44 +00:00
'meta_query' => woocommerce::$query['meta_query']
2011-08-31 00:29:02 +00:00
)
)
);
2011-08-31 10:51:50 +00:00
// Add posts to array
2011-09-04 00:02:44 +00:00
foreach ($products as $p) $unfiltered_product_ids[] = $p->ID;
// Store the variable
woocommerce::$query['unfiltered_product_ids'] = $unfiltered_product_ids;
2011-08-31 00:29:02 +00:00
2011-08-31 10:51:50 +00:00
// Also store filtered posts ids...
2011-09-04 00:02:44 +00:00
if (sizeof(woocommerce::$query['post__in'])>0) :
woocommerce::$query['filtered_product_ids'] = array_intersect(woocommerce::$query['unfiltered_product_ids'], woocommerce::$query['post__in']);
2011-08-31 10:51:50 +00:00
else :
2011-09-04 00:02:44 +00:00
woocommerce::$query['filtered_product_ids'] = woocommerce::$query['unfiltered_product_ids'];
2011-08-31 10:51:50 +00:00
endif;
// And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
2011-09-04 00:02:44 +00:00
if (sizeof(woocommerce::$query['layered_nav_post__in'])>0) :
woocommerce::$query['layered_nav_product_ids'] = array_intersect(woocommerce::$query['unfiltered_product_ids'], woocommerce::$query['layered_nav_post__in']);
2011-08-31 10:51:50 +00:00
else :
2011-09-04 00:02:44 +00:00
woocommerce::$query['layered_nav_product_ids'] = woocommerce::$query['unfiltered_product_ids'];
2011-08-10 17:11:11 +00:00
endif;
2011-08-31 00:29:02 +00:00
}