WooCommerce: Multiple PayPal Accounts (Solved)

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer, BusinessBloomer.com
 * @testedwith    WooCommerce 4.6
 * @community     https://businessbloomer.com/club/
 */

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

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

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

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 πŸ™‚

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

23 thoughts on “WooCommerce: Multiple PayPal Accounts (Solved)

  1. I’m looking for the code to switch PayPal accounts during the WooCommerce payment process based on the customer. Specifically, I want to rotate PayPal accounts so that each customer uses a different account when making a payment, with only one payment button visible in the store interface, if possible

    1. Abdo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. I need the same plugin ..
        if available let me know to purchase

        1. Which PayPal plugin are you on?

  2. I’ve been using this snippet for a few years now without problems, thanks so much for this, it has been flawless. However PayPal are not supporting PayPal Standard anymore and woocommerce are replacing it with the PayPal Payments plugin, so this snippet will no longer work. Can you publish an update that works with PayPal Payments?

    1. Please send me a reminder in a few months, as I still use PP standard on this same website and won’t drop it until it keeps working (the new solution is full of bugs).

      1. Here’s the reminder as requested. I am still getting emails from PayPal asking me to move over to their new API code. I can see that the PayPal payments plugin appears a bit buggy but there is a more popular, less buggy plugin here https://en-gb.wordpress.org/plugins/pymntpl-paypal-woocommerce/

        I spoke with them about providing a way to use multiple user accounts in one woocommerce setup and they said it was more complex than the paypal standard approach and beyond the scope of the support forums.

        Could you help? Otherwise I am going to drop PayPal and just use Stripe which still works.

        1. Thank you Jon. Each plugin is coded differently, especially those that integrate WooCommerce with a payment provider such as PayPal. It’s impossible for me to provide a free solution for each possible PayPal plugin out there, so for now I won’t change this as it still works perfectly on this same website with the PayPal Standard method that comes with WooCommerce.

          If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi Rodolfo,

    I have no doubt that your “Switch PayPal Account @ WooCommerce Checkout” works on single purchases. Do you know if it will work for WooCommerce Subscriptions / Recurring Payments? With of course Billing Agreements / Reference Transactions for PayPal activated.

    Thanks,

    Hans

    1. No idea! Please always test on staging

  4. I couldn’t get this to work.

    The first error I got, was due to namespacing (

    WC_Gateway_Paypal_IPN_Handler

    had to be replaced with

    \WC_Gateway_Paypal_IPN_Handler

    ).

    After I did that, then the page loaded. But when I clicked ‘Proceed to PayPal’, then I get directed to sandbox… And if I click ‘Login’ then I just log in to the Sandbox-PayPal-account – I don’t get redirected to any payment page. It’s like it forgot that I was in the middle of something πŸ™‚

    I am using an sandbox-email, I don’t know.

    I do find it weird that the API-username, password and signature doesn’t need to be changed, so it matches this new email.

    When all has been said, then PayPal’s dashboard has been really flakey to work with.

    1. Thanks for that, I’m not 100% sure, this would require custom troubleshooting

  5. Hello!
    Is it possible to use Different PayPal Standard Account For a shipping address @ WooCommerce Checkout?
    1 account for EU shipping
    1 account for US/Rest of World shipping

    Thank you!

    Gian Luca

    1. Hey Gian Luca, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. By an amazing coincidence I was working on this problem yesterday but your code has showed that I don’t understand filters properly and I would like your help.

    This filter does not appear in the WooCommerce Code Reference.

    * https://woocommerce.github.io/code-reference/hooks/hooks.html

    * How did you find it?

    It does appear here.

    * https://hookr.io/filters/woocommerce_paypal_args/

    and the definition is

    “add_filter(‘woocommerce_paypal_args’, array($this, ‘filter_paypal_args’));”

    It seems to have a single parameter. But your code

    “function bbloomer_switch_paypal_email_based_product( $paypal_args, $order )”

    MAGICALLY knows that $order exists as second parameter. How did you know this and why does it work?

    1. I solved part of this problem. I now understand to find the arguments passed to a filter call you have to look at the “apply_filters” call not the add_filter syntax.

      apply_filters( 'woocommerce_paypal_args',  $var,  $order ); 

      I still don’t know how you found the woocommerce_paypal_args filter because I can’t find it in the documentation.

      1. Nice, good trial and error there! I found that filter by looking at other snippets online and then double checking it still existed by searching through the WooCommerce plugin code.

  7. Hi,

    Wow, Works like charm!
    Thanks
    Ahir

    1. Great!

  8. How do i modify the code to work for a particular / multiple product categories?

    1. Hello Debashish, thanks so much for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy πŸ™‚

      1. Hello Rodolfo, thanks a lot for your useful tutorials. I would like to achieve something similar to this with the exception that I need not to change paypal basing on product id but basing on checkout price. If below 20$ should use emailpaypal1 if above 20$ I should use emailpaypal2

        Is it possible? thanks a lot

        1. Hi Jo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *