Take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case:
- Find list on product categories based on all the products in the Cart
- Disable a specific payment ID if the target product category is in the Cart.
Disable Payment Method for Specific Category – WooCommerce Checkout
add_filter( 'woocommerce_available_payment_gateways', codeamend_unset_gateway_by_category);
function codeamend_unset_gateway_by_category( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$category_id = 8; // TARGET CATEGORY
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( $term->term_id == $category_id ) {
$unset = true;
// CATEGORY IS IN THE CART
break;
}
}
}
if ( $unset == true ) unset( $available_gateways['cod'] );
return $available_gateways;
}
Note to find payment gateway slug: When you hover the payment gateway name in Woocommerce payment settings you can find refer: https://example.com/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=paypal
Total Views: 1,658