Learn here about how to enable or disable a payment gateway based on the conditions like country, shipping method, order total, User Role and Product Category.
add_filter( 'woocommerce_available_payment_gateways', codeamend_payment_gateway_disable_country);
function codeamend_payment_gateway_disable_country( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( isset( $available_gateways['authorize'] ) && WC()->customer->get_billing_country() <> 'US' ) {
unset( $available_gateways['authorize'] );
} else {
if ( isset( $available_gateways['paypal'] ) && WC()->customer->get_billing_country() == 'US' ) {
unset( $available_gateways['paypal'] );
}
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', codeamend_paypal_disable_manager);
function codeamend_paypal_disable_manager( $available_gateways ) {
if ( isset( $available_gateways['paypal'] ) && current_user_can( 'manage_woocommerce' ) ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', codeamend_paypal_enable_manager);
function codeamend_paypal_enable_manager( $available_gateways ) {
if ( isset( $available_gateways['paypal'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', codeamend_disable_paypal);
function codeamend_disable_paypal( $available_gateways ) {
$maximum = 10;
if ( WC()->cart->total > $maximum ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', codeamend_gateway_disable_for_shipping_rate);
function codeamend_gateway_disable_for_shipping_rate( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
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: 2,128