Hide some products form google on woocommerce

The code below hide from google a product that have a value “b2b” on a custom field.
Note #1: I used a ACF function to call the custom field, but you can use get_post_meta
Note#2: Required Yoast seo

// run the action
add_filter('wpseo_robots', 'yoast_noidex', 999);
function yoast_noindex($string) {
         if(is_product()){
           $limitazioni = get_field( "product_visibility" ); //ACF 
           if ($limitazioni == 'b2b'){
            $string= "noindex, nofollow";
         }
      }
      return $string;
}

Will return <meta name=”robots” content=”noindex, nofollow” /> if true.
If false <meta name=”robots” content=”index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1″ />

This code will have priority on the yoast configuration in the product page

Questions? Suggestions? Please leave a comment below.

Leave a comment