skip to Main Content

I have seen a number of similar posts to this, but not one that specifically solves the issue I am having.

I need to create a function that allows me to remove empty tag archive pages from the tag cloud on my woocommerce site so they do not lead users onto empty pages.

The only code I have found that allows removal of the entire tag cloud is:

add_action( 'widgets_init', 'misha_remove_product_tag_cloud_widget' );
 
function misha_remove_product_tag_cloud_widget(){
    unregister_widget('WC_Widget_Product_Tag_Cloud');
}

I believe this could be used in conjunction with something like this code which allows for the removal of all unused categories from various locations:

add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
    global $wpdb;
    $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
    foreach ( $items as $key => $item ) {
        if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
            unset( $items[$key] );
        }
    }
    return $items;
}

I am unsure how to work the two together, but the end result should allow for any unused product tag archive pages to be hidden from the tag cloud until they are either used again or removed entirely from the website – but always preventing users from accessing empty tag pages!

2

Answers


  1. Chosen as BEST ANSWER

    This question was answered in more depth over here: WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products

    The code used as the final solution is as follows:

    /* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */
    
    function woocommerce_product_tag_cloud_widget_filter($args) {
        $args = array(
            'smallest' => 14, 
            'largest' => 14, 
            'format' => 'list', 
            'taxonomy' => 'product_tag', 
            'unit' => 'px',
            'show_count' => 1,
            'number' => 0,
        );
    
        echo "<div style='padding-left: 20px; min-height: 50px; max-height: 400px; overflow: auto;'>";
        return $args;
        echo "</div>";
    }
    
    add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');
    
    /* HIDE PRODUCT TAG ARCHIVE PAGES WHEN THEY HAVE NO 'IN STOCK' PRODUCTS */
    
    function hide_empty_tags( $terms, $taxonomies) {
        $new_terms = array();
        
        if ( in_array( 'product_tag', $taxonomies ) && ! is_admin() ) {
            foreach ( $terms as $key => $term ) {
                if ($term->count >0){
                    $new_terms[] = $term;
                }
            }
            $terms = $new_terms;
        }
        return $terms;
    }
    
    add_filter( 'get_terms', 'hide_empty_tags', 10, 3 );
    
    /* REDIRECTS TO SHOP IF THERE ARE NO 'IN STOCK' PRODUCTS IN THE PRODUCT TAG ARCHIVE PAGE */
    
    function redirect_to_shop(){
        global $wp_query;
    
        if( is_woocommerce() && $wp_query->post_count == 0 ){
            the_post();
        $post_url = "/shop";
        wp_safe_redirect($post_url , 302 );
        exit;
        }
    } 
    
    add_action('template_redirect', 'redirect_to_shop');
    

  2. Normally by default empty tags are hidden from WooCommerce Product Tag Cloud Widget… If it’s not the case you can use:

    add_filter('woocommerce_product_tag_cloud_widget_args','hide_empty_terms_from_product_tag_cloud_widget');
    function hide_empty_terms_from_product_tag_cloud_widget( $args ) {
        $args['hide_empty'] = true; // Empty tags not visible
        return $args;
    }
    

    To make empty product tags visible in WooCommerce Product Tag Cloud Widget use:

    add_filter('woocommerce_product_tag_cloud_widget_args','show_empty_terms_from_product_tag_cloud_widget');
    function show_empty_terms_from_product_tag_cloud_widget( $args ) {
        $args['hide_empty'] = false; // Empty tags are visible
        return $args;
    }
    

    See wp_tag_cloud() (used by WooCommerce tag Cloud Widget) and get_terms() function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search