original_query->is_post_type_archive( 'product' ) || $this->original_query->is_tax( 'product_cat' ) || $this->original_query->is_tax( 'product_tag' ); } /** * Return true if we are on the Search page * * @return bool * * @since 1.0 */ public function is_search() { return $this->original_query->is_search; } /** * Action and filter hooks */ /** * Parses a request to the 'original_query' var to access its boolean methods * This is called with highest priority to get first look at the request * * @param array $request * * @since 1.0 */ public function parse_original_request( $request ) { if ( $this->original_query ) return $request; $this->original_query = new WP_Query(); $this->original_query->parse_query( $request ); return $request; } /** * Alters the main wordpress query when on a Jigoshop product listing. * * The meta-query and tax_query can be filtered using the 'loop_shop_tax_query' and 'loop_shop_tax_meta_query' filters. * * Use the 'loop_shop_per_page' filter for adjusting the # of products to show per page on front end Product lists. * * Use the 'loop-shop-query' filter to adjust sort order and direction or other front end only arguments. * * The whole resulting request can be filtered using the 'jigoshop-request' filter * * @param array $request - the parsed request from 'parse_original_request' that sets up $this->original_query * @return array - the altered request array is returned to be submitted to 'query_posts' with all filtering done. * * @since 1.0 */ public function catalog_query_filter( $request ) { global $jigoshop_all_post_ids_in_view; // we only work on Jigoshop product lists if ( ! $this->is_product_list() ) return $request; $request['post_status'] = 'publish'; $request['posts_per_page'] = apply_filters( 'loop_shop_per_page', get_option( 'jigoshop_catalog_per_page' )); // establish any filters for orderby, order and anything else added to the filter $filters = array(); $filters = apply_filters( 'loop-shop-query', $filters ); foreach( $filters as $key => $value ) : $request[$key] = $value; endforeach; $request['tax_query'] = apply_filters( 'loop_shop_tax_query', $this->tax_query( $request )); $request['meta_query'] = apply_filters( 'loop_shop_tax_meta_query', $this->meta_query( $request )); // modify the query for specific product ID's for layered nav and price filter widgets $request['post__in'] = apply_filters( 'loop-shop-posts-in', $jigoshop_all_post_ids_in_view ); return apply_filters( 'jigoshop-request', $request ); /* give it back to WordPress for query_posts() */ } /** * This function builds the taxonomy query for Product Categories and Tags. * * @param array $request * * @since 1.0 */ private function tax_query( $request ) { $tax_query = array( 'relation' => 'AND' ); // we add 'product_cat' and 'product_tag' to the tax query so that is_tax() replies correctly if ( ! empty( $request['product_cat'] ) ) { $tax_query[] = array ( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $request['product_cat'], 'operator' => 'IN', ); } if ( ! empty( $request['product_tag'] ) ) { $tax_query[] = array ( 'taxonomy' => 'product_tag', 'field' => 'slug', 'terms' => $request['product_tag'], 'operator' => 'IN', ); } return $tax_query; } /** * This function builds the meta query for products. * * @param array $request * * @since 1.0 */ private function meta_query( $request ) { $in = array( 'visible' ); if ( $this->is_search() ) $in[] = 'search'; else $in[] = 'catalog'; $meta = $this->original_query->get( 'meta_query' ); $meta[] = array( 'key' => 'visibility', 'value' => $in, 'compare' => 'IN' ); return $meta; } /** * used by the layered_nav widget and the price filter widget as they access the global ($all_post_ids) * is run on the 'request' filter with highest priority to ensure it runs before main filter_catalog_query * gathers all product ID's into a global variable for use elsewhere ($all_post_ids) * * @param array $request - the array representing the current WordPress request eg. post_type => 'product' * @return array - unaltered array of the intial request * @since 0.9.9 **/ function jigoshop_get_product_ids_in_view( $request ) { global $jigoshop_all_post_ids_in_view; $jigoshop_all_post_ids_in_view = array(); $this_query = new WP_Query(); $this_query->parse_query( $request ); if ( $this_query->is_post_type_archive( 'product' ) || $this_query->is_tax( 'product_cat' ) || $this_query->is_tax( 'product_tag' ) ) : $args = array_merge( $this_query->query, array( 'page_id' => '', 'posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'meta_query' => self::meta_query( $this_query ) ) ); $custom_query = new WP_Query( $args ); foreach ($custom_query->posts as $p) $jigoshop_all_post_ids_in_view[] = $p->ID; endif; $jigoshop_all_post_ids_in_view[] = 0; return $request; } }