forked from jigoshop/jigoshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjigoshop_query.php
More file actions
235 lines (161 loc) · 5.41 KB
/
jigoshop_query.php
File metadata and controls
235 lines (161 loc) · 5.41 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Jigoshop queries
*
* DISCLAIMER
*
* Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
* versions in the future. If you wish to customise Jigoshop core for your needs,
* please use our GitHub repository to publish essential changes for consideration.
*
* @package Jigoshop
* @category Core
* @author Jigowatt
* @copyright Copyright (c) 2011 Jigowatt Ltd.
* @license http://jigoshop.com/license/commercial-edition
*/
### Get unfiltered list of posts in current view for use in loop + widgets
function jigoshop_get_products_in_view() {
global $all_post_ids;
$all_post_ids = array();
if (is_tax( 'product_cat' ) || is_post_type_archive('product') || is_page( get_option('jigoshop_shop_page_id') ) || is_tax( 'product_tag' )) :
$all_post_ids = jigoshop_get_post_ids();
endif;
$all_post_ids[] = 0;
}
add_action('wp_head', 'jigoshop_get_products_in_view', 0);
### Do Filters/Layered Nav
function jigoshop_filter_loop() {
global $wp_query, $all_post_ids;
if (is_tax( 'product_cat' ) || is_post_type_archive('product') || is_page( get_option('jigoshop_shop_page_id') ) || is_tax( 'product_tag' )) :
$filters = array();
$filters = apply_filters('loop-shop-query', $filters);
//$post_ids = jigoshop_get_post_ids();
$post_ids = $all_post_ids;
$post_ids = apply_filters('loop-shop-posts-in', $post_ids);
$filters = array_merge($filters, array(
'post__in' => $post_ids
));
$args = array_merge( $wp_query->query, $filters );
query_posts( $args );
endif;
}
add_action('wp_head', 'jigoshop_filter_loop');
### Layered Nav Init
function jigoshop_layered_nav_init() {
global $_chosen_attributes, $wpdb;
$attribute_taxonomies = jigoshop::$attribute_taxonomies;
if ( $attribute_taxonomies ) :
foreach ($attribute_taxonomies as $tax) :
$attribute = strtolower(sanitize_title($tax->attribute_name));
$taxonomy = 'product_attribute_' . $attribute;
$name = 'filter_' . $attribute;
if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) $_chosen_attributes[$taxonomy] = explode(',', $_GET[$name] );
endforeach;
endif;
}
add_action('init', 'jigoshop_layered_nav_init', 1);
### Get post ID's to filter from
function jigoshop_get_post_ids() {
global $wpdb;
$in = array('visible');
if (is_search()) $in[] = 'search';
if (!is_search()) $in[] = 'catalog';
// WP Query to get all queried post ids
global $wp_query;
$args = array_merge(
$wp_query->query,
array(
'page_id' => '',
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'visibility',
'value' => $in,
'compare' => 'IN'
)
)
)
);
$custom_query = new WP_Query( $args );
$queried_post_ids = array();
foreach ($custom_query->posts as $p) $queried_post_ids[] = $p->ID;
wp_reset_query();
return $queried_post_ids;
}
### Layered Nav
function jigoshop_layered_nav_query( $filtered_posts ) {
global $_chosen_attributes, $wpdb;
if (sizeof($_chosen_attributes)>0) :
$matched_products = array();
$filtered = false;
foreach ($_chosen_attributes as $attribute => $values) :
if (sizeof($values)>0) :
foreach ($values as $value) :
$posts = get_objects_in_term( $value, $attribute );
if (!is_wp_error($posts) && (sizeof($matched_products)>0 || $filtered)) :
$matched_products = array_intersect($posts, $matched_products);
elseif (!is_wp_error($posts)) :
$matched_products = $posts;
endif;
$filtered = true;
endforeach;
endif;
endforeach;
if ($filtered) :
$matched_products[] = 0;
$filtered_posts = array_intersect($filtered_posts, $matched_products);
endif;
endif;
return $filtered_posts;
}
add_filter('loop-shop-posts-in', 'jigoshop_layered_nav_query');
### Price Filtering
function jigoshop_price_filter( $filtered_posts ) {
if (isset($_GET['max_price']) && isset($_GET['min_price'])) :
$matched_products = array( 0 );
$matched_products_query = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'price',
'value' => array( $_GET['min_price'], $_GET['max_price'] ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'grouped',
'operator' => 'NOT IN'
)
)
));
if ($matched_products_query) :
foreach ($matched_products_query as $product) :
$matched_products[] = $product->ID;
endforeach;
endif;
// Get grouped product ids
$grouped_products = get_objects_in_term( get_term_by('slug', 'grouped', 'product_type')->term_id, 'product_type' );
if ($grouped_products) foreach ($grouped_products as $grouped_product) :
$children = get_children( 'post_parent='.$grouped_product.'&post_type=product' );
if ($children) foreach ($children as $product) :
$price = get_post_meta( $product->ID, 'price', true);
if ($price<=$_GET['max_price'] && $price>=$_GET['min_price']) :
$matched_products[] = $grouped_product;
break;
endif;
endforeach;
endforeach;
$filtered_posts = array_intersect($matched_products, $filtered_posts);
endif;
return $filtered_posts;
}
add_filter('loop-shop-posts-in', 'jigoshop_price_filter');