Search code examples
phpwordpresswoocommercepaypalpayment-method

Disable specific plugins for unlogged users in WooCommerce


I am trying to disable WooCommerce PayPal Checkout Payment Gateway for unlogged users. How can I do that?


Solution

  • Updated - The following will disable Paypal Checkout Payment gateway for unlogged users:

    add_filter( 'woocommerce_available_payment_gateways', 'conditionally_disable_paypal_payment_gateways', 10, 1 );
    function conditionally_disable_paypal_payment_gateways( $available_gateways ) {
        if ( is_admin() ) return $available_gateways; // Only on frontend
    
        // Loop through payment gateways
        foreach( $available_gateways as $gateways_id => $gateways ){
            if ( ! is_user_logged_in() && $gateways_id === 'ppec_paypal' ) {
                unset($available_gateways[$gateways_id]);
            }
        }
        return $available_gateways;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    For Woocommerce default Paypal payment gateway, replace 'ppec_paypal' by 'paypal'.