Skip to content

Instantly share code, notes, and snippets.

@dev-w3
Last active July 21, 2021 09:06
Show Gist options
  • Save dev-w3/2179c1e03434da13bf90d1a26a20eb24 to your computer and use it in GitHub Desktop.
Save dev-w3/2179c1e03434da13bf90d1a26a20eb24 to your computer and use it in GitHub Desktop.
To hide payment method if coupon is applied in woo-commerce
<?php
add_filter('woocommerce_available_payment_gateways', '_hide_payment_gateways', 20, 1 );
function _hide_payment_gateways( $available_gateways){
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// If at least a coupon is applied
if( sizeof( WC()->cart->get_applied_coupons() ) > 0 ){
// Loop through payment gateways
foreach ( $available_gateways as $gateway_id => $gateway ) {
//echo $gateway_id;
// Remove all payment gateways except BACS (Bank Wire)
if( $gateway_id != 'bacs' )
unset($available_gateways[$gateway_id]);
// or you can simply pass any specific gateway id if you want to hide any specific
}
}
return $available_gateways;
}
@dev-w3
Copy link
Author

dev-w3 commented Jul 21, 2021

Hide specific payment method if coupon is applied in Woocommerce
Eg: if you want to use the coupon code for the Bank Wire method only.
Need to put this code in the functions.php in the theme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment