I am ussing this code to disable Paypal when any user use a coupon:
add_filter('woocommerce_available_payment_gateways', 'applied_coupons_hide_payment_gateways', 20, 1 );
function applied_coupons_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 ) {
// Remove all payment gateways except BACS (Bank Wire)
if( $gateway_id != 'stripe' )
if( $gateway_id != 'bacs' )
unset($available_gateways[$gateway_id]);
}
}
return $available_gateways;
}
But now I need to make an exeption for an specific coupon "PAYPALMSI" I have been testing with this other code and it works but they overide each other, how i could use them at same time? make an exception for paypalmsi on the 1st code?
add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');
function unset_gatway_by_applied_coupons($available_gateways)
{
$coupons = WC()->cart->applied_coupons;
foreach ($coupons as $coupon) {
if ($coupon == 'paypalmsi') {
unset($available_gateways['stripe']);
unset($available_gateways['bacs']);
}
}
return $available_gateways;
}
2
Answers
Thanks to PrestonPHX
I used this code for showing only stripe and bacs if a coupon is used, excluding the especfic one "paypalmsi"
then i used this code to show only paypal when the coupon "paypalmsi" was used.
Change that line to also check whether
paypalmsi
is one of the coupons…