Home » WooCommerce: Multiple PayPal Accounts (Solved)

WooCommerce: Multiple PayPal Accounts (Solved)

by Tutor Aspire

The WooCommerce plugin comes with its own free version of PayPal Standard. PayPal can be enabled via the WooCommerce settings and once your PayPal email is entered your WooCommerce shop is ready to take PayPal payments.

Now, there is extensive documentation online which explains, with a little bit of code, how to switch PayPal account programmatically and conditionally i.e. for a given product ID or product category slug. For example, you may want to use a PayPal account for consulting services, another for online courses and another for physical products.

By adding this simple code and hooking into woocommerce_paypal_args is indeed possible to use different / multiple PayPal Standard accounts in a single WooCommerce installation.

However, there is an outstanding problem with “IPN Validation“: once you tell WooCommerce to use a different PayPal email account, the WooCommerce order is correctly placed, but its status goes “on hold” because IPN validation on the PayPal end fails (and that’s because you’re using a different PayPal account).

So, here’s the fully working version, included the IPN validation fix. Please read the disclaimer below and – only then – enjoy!

My goal is to use my PayPal email for all payments apart from a given product ID which will need to deposit funds into a different PayPal Standard account.

PHP Snippet: Use Different PayPal Standard Account For a Product ID @ WooCommerce Checkout

Disclaimer: the snippet below MAY CAUSE side effects – use at your own risk. For example, it can mess with PayPal refund handling (better if you remove PayPal API keys from WooCommerce as refunds will now need to be done on your PayPal account) or generate other unknown errors. Customizing payment gateways is very dangerous and should be done only if you are aware of the potential consequences. Please test this thoroughly.
Please note: the snippet below looks for a Product ID inside the order and switches PayPal email if successful. In case the order contains several products you’d need to find a workaround, for example by allowing only 1 product in the Cart.
/**
 * @snippet       Switch PayPal Account @ WooCommerce Checkout
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire, BusinessBloomer.com
 * @testedwith    WooCommerce 4.6
 * @donate $9     https://www.tutoraspire.com
 */

// -------------------
// 1. Switch PayPal email for Product ID

add_filter( 'woocommerce_paypal_args' , 'tutoraspire_switch_paypal_email_based_product', 9999, 2 );

function tutoraspire_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'] = '[email protected]';
            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 != '[email protected]' ) {

                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();

Is There a (Reliable) Plugin For That?

If you’d love to code but don’t feel 100% confident with PHP, I decided to look for a reliable plugin that achieves the same result (well, actually it gives you more).

In this case, I recommend the WooCommerce Multiple PayPal Accounts Plugin. On top of sending the whole order total to a different PayPal account based on total, billing country, category, tag, user role, shipping class, currency, you can also split payments between PayPal accounts, keep a commission as a store owner, integrate with WC Vendors and Dokan and much more.

But in case you hate plugins and wish to code (or wish to try that), then keep reading 🙂

You may also like