WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products

831 Views Asked by At

I have a WooCommerce site and need product tags to be hidden from the product tag cloud when a product tag archive page has 0 'in stock' products available.

The code below shows my current progress and I will update as more progress is made. I believe this code could be adapted to run a check and remove any results which have a 'show count' value less than 1 which would answer this question. Additionally the hidden results will need to 302 redirect to the top level 'shop' page whilst it is temporarily hidden:

/* 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;'>";
    return $args;
    echo "</div>";
}

add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');

I also have this code which might be used as a second approach. The notes show what is missing:

/* REMOVE PRODUCT TAGS FROM THE PRODUCT TAG CLOUD WHEN THEY HAVE LESS THAN 1 IN-STOCK RESULTS */

function filter_woocommerce_product_tag_cloud_widget_args( $array ) { 
    $c = // check to see how many in stock products a product tag archive has
        if($c <= 1){
            // show array excluding empty product tag archives
        }else{
            return $array;
        } 
}

add_filter( 'woocommerce_product_tag_cloud_widget_args', 'filter_woocommerce_product_tag_cloud_widget_args', 10, 1 );

I have the following resources to help, however my lack of PHP knowledge means I am unable to put these various pieces of code into a single function to achieve my desired outcome.

I have this piece of code from How to remove empty product tags from the tag cloud on woocommerce answer code to my previous question which hides all empty product tags from the tag cloud (but it doesn't factor in stock availability and so will still show product tags with 0 'in stock' products).

I have also some others pieces of code below which may or may not help with a final solution.

This code hides the entire product tag cloud.

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

This code hides all categories which have 0 'in stock' products.

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 have tried to put various elements of the above pieces of code together to create a function that achieves my goal, but my lack of ability means I have not got anything to correctly work and therefore I need additional help.

1

There are 1 best solutions below

0
On BEST ANSWER

I have managed to piece together the following solution to this question that actually takes it a few steps further and adds some other cool functionality which I have tried to explain in the comment code.

If anyone has any feedback, additions, comments or questions about this code, please do get in touch! I will be trying to extend on this code so that it can also double up as a refine feature by only showing tags relevant to a users position within the website and removing all irrelevant tags from those levels - Any suggestions are welcomed!

Special thanks to Md. Mehedi Hasan on Facebook for helping with the middle section of code to hide the product tag archive pages from the front end.

/* 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');