forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce_query.php
139 lines (114 loc) · 4.15 KB
/
woocommerce_query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* WooCommerce Queries
*
* Handles front end queries and loops.
*
* @package WooCommerce
* @category Core
* @author WooThemes
*/
/**
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
*/
add_filter( 'parse_query', 'woocommerce_parse_query' );
function woocommerce_parse_query( $q ) {
global $woocommerce;
if (is_admin()) return;
// Only apply to product categories, the product post archive, the shop page, and product tags
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;
$meta_query = (array) $q->get( 'meta_query' );
// Visibility
if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
$meta_query[] = array(
'key' => 'visibility',
'value' => $in,
'compare' => 'IN'
);
// In stock
if (get_option('woocommerce_hide_out_of_stock_items')=='yes') :
$meta_query[] = array(
'key' => 'stock_status',
'value' => 'instock',
'compare' => '='
);
endif;
// Ordering
if (isset($_POST['orderby']) && $_POST['orderby'] != '') $_SESSION['orderby'] = $_POST['orderby'];
$current_order = (isset($_SESSION['orderby'])) ? $_SESSION['orderby'] : 'title';
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)
$post__in = array_unique(apply_filters('loop-shop-posts-in', array()));
// Ordering query vars
$q->set( 'orderby', $orderby );
$q->set( 'order', $order );
$q->set( 'meta_key', $meta_key );
// Query vars that affect posts shown
$q->set( 'post_type', 'product' );
$q->set( 'meta_query', $meta_query );
$q->set( 'post__in', $post__in );
$q->set( 'posts_per_page', apply_filters('loop_shop_per_page', get_option('posts_per_page')) );
// Store variables
$woocommerce->query['post__in'] = $post__in;
$woocommerce->query['meta_query'] = $meta_query;
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action('wp', 'woocommerce_get_products_in_view', 2);
}
/**
* Remove parse_query so it only applies to main loop
*/
add_action('wp', 'woocommerce_remove_parse_query');
function woocommerce_remove_parse_query() {
remove_filter( 'parse_query', 'woocommerce_parse_query' );
}
/**
* Get an unpaginated list all product ID's (both filtered and unfiltered)
*/
function woocommerce_get_products_in_view() {
global $wp_query, $woocommerce;
$unfiltered_product_ids = array();
// Get all visible posts, regardless of filters
$products = get_posts(
array_merge(
$wp_query->query,
array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'meta_query' => $woocommerce->query['meta_query']
)
)
);
// Add posts to array
foreach ($products as $p) $unfiltered_product_ids[] = $p->ID;
// Store the variable
$woocommerce->query['unfiltered_product_ids'] = $unfiltered_product_ids;
// Also store filtered posts ids...
if (sizeof($woocommerce->query['post__in'])>0) :
$woocommerce->query['filtered_product_ids'] = array_intersect($woocommerce->query['unfiltered_product_ids'], $woocommerce->query['post__in']);
else :
$woocommerce->query['filtered_product_ids'] = $woocommerce->query['unfiltered_product_ids'];
endif;
// And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
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']);
else :
$woocommerce->query['layered_nav_product_ids'] = $woocommerce->query['unfiltered_product_ids'];
endif;
}