This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Elementor WooCommerce hide empty product filters

Elementor WooCommerce hide empty product filters

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Elementor WooCommerce hide empty product filters

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #55668
    office_BxoMoffice_BxoM
    Participant

    Paul here, we wrote via email before.

    We fixed the main issue but have one remaining.

    The filters in the “Product-Archive” Elementor Template always show all terms, even if there are no products in the respective category with those terms. The setting that it should hide empty terms is activated.

    Is it possible to filter out the empty product filters so it e.g. doesn’t show material filters for metal products in the textile category?

    Cheers,
    Paul

    #55669
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Paul,

    This might get a bit tricky, but it is likely possible to some extent via custom code. I have an experimental solution, which hooks into the filters and tries to restrict them to the current possible archive result set.

    This is an extract from a possible feature in the upcoming version, but it should do the trick to some extent. Instructions below, but let me know if you need help with it:

    function get_available_taxonomy_terms( $taxonomy ) {
    	global $wpdb;
    
    	// Initialize terms array
    	$terms = [];
    	$cache_key = 'terms_' . $taxonomy;
    
    	// Get the current queried object
    	$current_queried_object = get_queried_object();
    
    	// Check if we're on a term archive
    	if ( is_a( $current_queried_object, 'WP_Term' ) ) {
    		// Update cache key to include the term ID
    		$cache_key .= '_' . $current_queried_object->term_id;
    
    		// Check transient cache
    		$cached_terms = get_transient( $cache_key );
    		if ( false !== $cached_terms ) {
    			return $cached_terms;
    		}
    
    		// Get post IDs in the current category using a custom SQL query
    		$query = $wpdb->prepare(
    				"
                SELECT DISTINCT tr.object_id
                FROM {$wpdb->term_relationships} tr
                INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                WHERE tt.taxonomy = %s
                AND tt.term_id = %d
                ",
    				$current_queried_object->taxonomy, // e.g., 'category'
    				$current_queried_object->term_id
    		);
    
    		$post_ids = $wpdb->get_col( $query );
    
    		if ( empty( $post_ids ) ) {
    			return []; // No posts in this category, return empty array
    		}
    
    		// Use get_terms() to get terms associated with these post IDs
    		$terms = get_terms( [
    				'taxonomy'   => $taxonomy,
    				'hide_empty' => false, // Include terms even if empty in other contexts
    				'object_ids' => $post_ids, // Filter by post IDs
    				'fields'     => 'all',
    		] );
    
    		if ( is_wp_error( $terms ) ) {
    			$terms = [];
    		}
    	} else {
    		// Not a term archive, fall back to all terms for the taxonomy
    		$cached_terms = get_transient( $cache_key );
    		if ( false !== $cached_terms ) {
    			return $cached_terms;
    		}
    
    		// Get all terms for the taxonomy
    		$terms = get_terms( [
    				'taxonomy'   => $taxonomy,
    				'hide_empty' => true, // Only include terms with associated posts
    				'fields'     => 'all',
    		] );
    
    		if ( is_wp_error( $terms ) ) {
    			$terms = [];
    		}
    	}
    
    	// Cache the results for 1 hour
    	set_transient( $cache_key, $terms, HOUR_IN_SECONDS );
    
    	return $terms;
    }
    
    add_filter('asp_fontend_get_taxonomy_terms', function( $terms, $taxonomy, $args, $needed_all ){
    	return get_available_taxonomy_terms($taxonomy);
    }, 10, 4);

    Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    #55670
    office_BxoMoffice_BxoM
    Participant

    Awesome, it works perfectly!

    Thanks for your time and swift responses, Ernest!

    #55671
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Great! Thank you very much for the feedback, I will make sure to integrate this to the upcoming release 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.