Is there a conditional function or another solution to check if the products are currently filtered?
Something like this would be great:
if( is_filtered() ) echo 'Filters active';
Amazing would be if the function returns the number of active filters (or a array) or false.
Thanks to David Chandra Purnama who pushed me in the right direction here's a very simple function to use:
function active_woocommerce_filters() {
// for older WC versions
// global $_chosen_attributes;
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
return count( $_chosen_attributes );
}
The function returns the number of active filters so it can be used like this:
if( active_woocommerce_filters() ) {
echo str_replace( '%s', active_woocommerce_filters(), 'There are %s filters active' );
} else {
echo 'There are no filters active';
}
EDIT:
As Artur Czyżewski pointed out the $_chosen_attributes
global variable is not available in his installation. This is most likely due to changes to WooCommerce and probably affect all newer versions, so i've updated the active_woocommerce_filters
function above.
You can check using global $_chosen_attributes;
WooCommerce "Layered Nav Filters" only displayed if filters is active. you can check the code "includes/widgets/class-wc-widget-layered-nav-filters.php":
global $_chosen_attributes;
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
return;
}
// Price
$min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : 0;
$max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : 0;
if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price ) {
/* Your Code Here. */
}