skip to Main Content

I am using the code for displaying the most complete product tags on the category page from here.

The code displays the product tags dynamically with refinements for selection, but an error appears in the line foreach ($tags as $tag):
"Warning: foreach() argument must be of type array|object, bool."

I don’t understand what exactly I need to fix and how to fix this warning. Any help will be appreciated.

Here is the code that I am using:

function custom_product_tag_cloud_func() {
   if (is_shop()) {
       $args= array(
     'smallest' => 13,
     'largest' => 13,
     'taxonomy' => 'product_tag',
     'unit' => 'px',
     'number' => 10,
     'format' => 'flat',
     'separator' => " ",
     'orderby' => 'count',
     'order' => 'DESC',
     'show_count' => 0,
     'number' => 0,
       );
       echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
       wp_tag_cloud($args);
       echo '</ul></div></div>';
   }
    global $wp;
    $current_slug = add_query_arg( array(), $wp->request );
    if(strpos($current_slug, '/') !== false) {
        $newtest = explode('/', $current_slug);
        $current_slug = array_pop($newtest);
    } 
    $args = array(
        'category' => array( $current_slug ),
        'limit' => -1,
    );
    $products = wc_get_products( $args );
    $tags_objects = array();
    foreach ($products as $product) {
        $tags = get_the_terms( $product->get_id(), 'product_tag' );
        foreach ($tags as $tag) { 
            $slug = $tag->slug;
            $cat_slug = add_query_arg( array(), $wp->request );
            $tag->link = get_term_link( $tag ); //get_site_url()."/".$cat_slug."?product_tag=".$slug;
            array_push($tags_objects,$tag);
        }
    }
    $tags_objects = array_unique($tags_objects, SORT_REGULAR);
    $args = array(
        'smallest' => 13,
        'largest' => 13,
           'number' => 10,
        'format' => 'flat',
        'separator' => " ",
        'orderby' => 'count',
        'order' => 'DESC',
        'unit' => 'px',
        'show_count' => 0,
        'number' => 0,
    );
    $tag_cloud = wp_generate_tag_cloud($tags_objects,$args);
    if(is_product_category()){
        echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
        echo $tag_cloud;
        echo '</ul></div></div>';
    }
} 

2

Answers


  1. In your code, for a foreach loop, it is better to replace get_the_terms() with wp_get_post_terms(), as if there are no terms found, wp_get_post_terms() returns an empty array, which is not the case for get_the_terms() that returns false and throws that warning message.

    Also, your current code can be lightened and optimized. Try the following:

    function custom_product_tag_cloud_func() {
        if ( is_shop() ) {
            echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
            
            wp_tag_cloud( array(
                'smallest' => 13,
                'largest' => 13,
                'taxonomy' => 'product_tag',
                'unit' => 'px',
                'number' => 10,
                'format' => 'flat',
                'separator' => " ",
                'orderby' => 'count',
                'order' => 'DESC',
                'show_count' => 0,
                'number' => 0,
            ) );
    
            echo '</ul></div></div>';
        } 
        elseif ( is_product_category() ) {
            global $wp;
    
            $current_slug = add_query_arg( array(), $wp->request );
    
            if(strpos($current_slug, '/') !== false) {
                $newtest = explode('/', $current_slug);
                $current_slug = array_pop($newtest);
            } 
        
            $product_ids = wc_get_products( array(
                'status'   => 'publish',
                'category' => array( $current_slug ),
                'limit'    => -1,
                'return'   => 'ids', // return only product Ids
            ) );
        
            $tags_objects = array();
        
            foreach ($product_ids as $product_id) {
                $tags = wp_get_post_terms( $product_id, 'product_tag' );
        
                if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
                    foreach ( $tags as $tag ) { 
                        $slug = $tag->slug;
                        $cat_slug = add_query_arg( array(), $wp->request );
                        $tag->link = get_term_link( $tag );
                        array_push($tags_objects, $tag);
                    }
                }
            }
        
            if( ! empty($tags_objects) ){
                $tags_objects = array_unique($tags_objects, SORT_REGULAR);
        
                echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
    
                echo wp_generate_tag_cloud($tags_objects, array(
                    'smallest' => 13,
                    'largest' => 13,
                    'number' => 10,
                    'format' => 'flat',
                    'separator' => " ",
                    'orderby' => 'count',
                    'order' => 'DESC',
                    'unit' => 'px',
                    'show_count' => 0,
                    'number' => 0,
                ) );
    
                echo '</ul></div></div>';
            }
        }
    }
    

    It should not throw anymore this warning message.

    Login or Signup to reply.
  2. WordPress is written in PHP, a polymorphic language, which is common knowledge. The negative (or fail) return is often a boolean or a WP_Error. Check the official documentation for function returns. Therefore, you need to validate your data before executing your loop.

    It’s good practice to always validate data before executing any code.

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