php - How to check if a woocommerce product has any category assigned to it? -


i creating custom plugin related products fetched per tags assigned product. facing issue. when run loop gives me warning message. want check if particular product has category assigned it. how can check if category has been assigned woocommerce product. please me. in advance.

my custom code

while ( $loop->have_posts() ) : $loop->the_post();      global $product;     global $post;      $feat_image = wp_get_attachment_url( get_post_thumbnail_id($loop->post->id) );     $terms200 = get_the_terms( $loop->post->id, 'product_cat' );           // if(!empty($terms200) || $terms200!='' || $terms200!=null){        if (get_category($loop->post->id)->category_count != 0){                 $content.='<li class="product-small  grid1 grid-normal">';             $content.='<div class="inner-wrap">';                 $content.='<a href="'.get_the_permalink($loop->post->id).'">';                     $content.='<div class="product-image hover_fade_in_back">';                         $content.='<div class="front-image">';                             $content.='<img src="'.$feat_image.'" class="attachment-shop_catalog wp-post-image" style="width:247px !important;height:300px !important;" />';                         $content.='</div>';                     $content.='</div>';                 $content.='</a>';                 $content .='<div class="info style-grid1">';                     $content .='<div class="text-center">';                         $count=1;                         foreach ( @$terms200 $term ) {                                 if($count==1){                                     $content.='<h5 class="category"><a href="'.get_site_url().'/?product_cat='.$term->slug.'" rel="tag">'.$term->name.'</a></h5>';                                       $content.='<div class="tx-div small"></div>';                                    }                             $count++;                         }                         $content .='<a href="'.get_the_permalink($loop->post->id).'"><p class="name">'.$loop->post->post_title.'</p></a>';                         if ( $price_html = $product->get_price_html() ) :                         $content .='<span class="price"><span class="amount">'.$price_html.'</span></span>';                         endif;                         $content.='<div class="mycustomcartbuttonbox">[add_to_cart id="'.$loop->post->id.'"]</div>';                                                     $content .='</div>';                 $content .='</div>';             $content.='</div>';         $content.='</li>';         echo do_shortcode($content);      }     endwhile; 

it simple that.

just type inside of loop:

if(!has_category("uncategorized")){  //do stuff } 

if doesn't work, try:

if(!has_term( 'uncategorized', 'product_cat' )){   //do stuff } 

you can learn more at: https://codex.wordpress.org/function_reference/has_category


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -