Preview:
//https://www.esthersola.com/ocultar-forma-de-pago-en-woocommerce-segun-productos/

//Hide payment method according to cart products

add_filter( 'woocommerce_available_payment_gateways', 'esl_payment_gateway_disable_by_cat' );


function esl_payment_gateway_disable_by_cat( $available_gateways) {

  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
  
    global $woocommerce; 
 
    $bool = false;
	
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
	 
        if ( has_term( 'accessories', 'product_cat', $cart_item['product_id'] ) )    $bool = true;
 
    } 
	
    if ($bool) 
	{
	    unset(  $available_gateways['cheque'] ) ;
      
	}
 
	return $available_gateways;  
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter