skip to Main Content

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


  1. Chosen as BEST ANSWER

    Thanks to PrestonPHX

    I used this code for showing only stripe and bacs if a coupon is used, excluding the especfic one "paypalmsi"

    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
      $coupons = WC()->cart->get_applied_coupons();
    if( sizeof( $coupons ) > 0 && !in_array( 'paypalmsi', $coupons )){
          
            // 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;
    }

    then i used this code to show only paypal when the coupon "paypalmsi" was used.

    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. if( sizeof( WC()->cart->get_applied_coupons() ) > 0 ){
    

    Change that line to also check whether paypalmsi is one of the coupons…

    $coupons = WC()->cart->get_applied_coupons();
    if( sizeof( $coupons ) > 0 && !in_array( 'paypalmsi', $coupons )){
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search