How to disable payment method by WooCommerce category?

How to disable payment methods by category

In this bit code snippet you’ll learn how to disable a payment method based on a category.

// Disable payment method by Product Category 
function nerodev_unset_gateway_by_category( $available_gateways ) {
 if ( is_admin() ) return $available_gateways;
 if ( ! is_checkout() ) return $available_gateways;
  $unset = false;
  $category_ids = array( 8 ); // Category ID => 8
  foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
   $terms = get_the_terms( $values['product_id'], 'product_cat' ); 
   foreach ( $terms as $term ) { 
    if ( in_array( $term->term_id, $category_ids ) ) {
     $unset = true;
     break;
    }
  }
 }
  if ( $unset == true ) unset( $available_gateways['cheque'] );
  return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'nerodev_unset_gateway_by_category' );

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *