How to switch WooCommerce Multiple PayPal Accounts?

WooCommerce comes with PayPal standard. Once this free PayPal version is enabled, your customers can make PayPal payments. You just need to enter a PayPal email in your WooCommerce shop. In this article, you will learn to switch WooCommerce Multiple PayPal Accounts. With this, you can also read our guide regarding how to enable WooCommerce Multiple Stripe Accounts.

One can use different PayPal accounts for different purposes. For instance, PayPal accounts can be unique namely for digital goods, physical goods, and consultancy services. 

To activate this function, you need to switch PayPal accounts programmatically and conditionally. You simply need to do some coding and hook into woocommerce_paypal_args. After enabling this, you can use multiple paypal accounts in a single WooCommerce installation.  

While doing so, you may face an ‘IPN Validation’ issue. So, whenever an order is placed on WooCommerce, the order status goes ‘on hold’. As you are using different PayPal accounts, IPN validation fails on the PayPal end.

Further, you will get a complete version that has fixed the IPN validation issue. Please read the disclaimer and enjoy the guide!

Image showing PayPal email for enabling WooCommerce Multiple PayPal Accounts

PHP Snippet: Switch WooCommerce Multiple PayPal Accounts for different product ID

Caution: This snippet can have potential threats. For instance, it can disturb the PayPal refund handling system or cause other issues. I highly recommend removing PayPal API keys from WooCommerce. The reason being refunds will be made on PayPal account. It can be trouble-causing to customize payment gateways. So, verify the snippet thoroughly and be aware of possible consequences. 

Please note that the snippet firstly searches for product ID inside the order. After that, it switches your PayPal email. If an order contains several products, then this snippet may not work. You will need to fix it other way round i.e. enabling only 1 product in the cart. 

// -------------------
// 1. Switch PayPal email for Product ID
 
add_filter( 'woocommerce_paypal_args' , 'phpsof_switch_paypal_email_based_product', 9999, 2 );
 
function phpsof_switch_paypal_email_based_product( $paypal_args, $order ) {
 
    foreach ( $order->get_items() as $item_id => $item ) {
 
        // ENTER PRODUCT ID HERE
        if ( 123456 == $item->get_product_id() ) {
 
            // ENTER OTHER PAYPAL EMAIL HERE
            $paypal_args['business'] = 'another-paypal@example.com';
            break;
 
        }
 
    }
 
    return $paypal_args;
 
}
 
// -------------------
// 2. Avoid IPN Failure after switching PayPal email for Product ID
 
require_once WC()->plugin_path() . '/includes/gateways/paypal/includes/class-wc-gateway-paypal-ipn-handler.php';
 
class WC_Gateway_Paypal_IPN_Handler_Switch extends WC_Gateway_Paypal_IPN_Handler { 
    
    protected function validate_receiver_email( $order, $receiver_email ) {
 
        if ( strcasecmp( trim( $receiver_email ), trim( $this->receiver_email ) ) !== 0 ) {
 
            // ENTER HERE SAME PAYPAL EMAIL USED ABOVE
            if ( $receiver_email != 'another-paypal@example.com' ) {
 
                WC_Gateway_Paypal::log( "IPN Response is for another account: {$receiver_email}. Your email is {$this->receiver_email}" );
                $order->update_status( 'on-hold', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'woocommerce' ), $receiver_email ) );
                exit;
 
            }
 
        }
 
    }
    
}
 
new WC_Gateway_Paypal_IPN_Handler_Switch();

Where do you add this snippet?

So, this is how you switch WooCommerce Multiple PayPal Accounts for a product ID. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this way, you can switch WooCommerce Multiple PayPal AccountsI have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Alter WooCommerce Order Field After Checkout?

How to Exclude Product from Discount Coupons in WooCommerce?

Important links: WooCommerce – https://woocommerce.com/