Last active
June 23, 2022 02:01
-
-
Save msaari/10b1b251eb826ebdc97fee478fe8c249 to your computer and use it in GitHub Desktop.
Relevanssi WooCommerce filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add this to theme functions.php or in a code snippet: | |
add_filter( 'relevanssi_modify_wp_query', 'rlv_woocommerce_filters' ); | |
function rlv_woocommerce_filters( $query ) { | |
// phpcs:disable WordPress.Security.NonceVerification.Recommended | |
$min_price = isset( $_REQUEST['min_price'] ) ? intval( $_REQUEST['min_price'] ) : false; | |
$max_price = isset( $_REQUEST['max_price'] ) ? intval( $_REQUEST['max_price'] ) : false; | |
$meta_query = $query->get( 'meta_query' ); | |
if ( $min_price ) { | |
$meta_query[] = array( | |
'key' => '_price', | |
'value' => $min_price, | |
'compare' => '>=', | |
'type' => 'NUMERIC', | |
); | |
} | |
if ( $max_price ) { | |
$meta_query[] = array( | |
'key' => '_price', | |
'value' => $max_price, | |
'compare' => '<=', | |
'type' => 'NUMERIC', | |
); | |
} | |
if ( $meta_query ) { | |
$query->set( 'meta_query', $meta_query ); | |
} | |
foreach ( array( 'product_tag', 'product_cat', 'product_brand' ) as $taxonomy ) { | |
$value = isset( $_REQUEST[ $taxonomy ] ) ? intval( $_REQUEST[ $taxonomy ] ) : false; | |
if ( $value ) { | |
$tax_query = $query->get( 'tax_query' ); | |
if ( ! is_array( $tax_query ) ) { | |
$tax_query = array(); | |
} | |
$tax_query[] = array( | |
'taxonomy' => $taxonomy, | |
'field' => 'term_id', | |
'terms' => $value, | |
); | |
$query->set( 'tax_query', $tax_query ); | |
} | |
} | |
if ( 'no' === get_option( 'woocommerce_attribute_lookup_enabled' ) ) { | |
return $query; | |
} | |
$chosen_attributes = array(); | |
if ( ! empty( $_GET ) ) { | |
foreach ( $_GET as $key => $value ) { | |
if ( 0 === strpos( $key, 'filter_' ) ) { | |
$attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) ); | |
$taxonomy = wc_attribute_taxonomy_name( $attribute ); | |
$filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array(); | |
if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) { | |
continue; | |
} | |
$query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) | |
? wc_clean( wp_unslash( $_GET[ 'query_type_' . $attribute ] ) ) | |
: ''; | |
$chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); | |
$chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' ); | |
} | |
} | |
} | |
$tax_query = $query->get( 'tax_query' ); | |
if ( ! is_array( $tax_query ) ) { | |
$tax_query = array(); | |
} | |
foreach ( $chosen_attributes as $taxonomy => $data ) { | |
$tax_query[] = array( | |
'taxonomy' => $taxonomy, | |
'field' => 'slug', | |
'terms' => $data['terms'], | |
'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN', | |
'include_children' => false, | |
); | |
} | |
if ( ! empty( $tax_query ) ) { | |
$query->set( 'tax_query', $tax_query ); | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment