3

I am trying to change the PayPal address that Woocommerce uses, depending on what page they are on. I only have 5 products at the moment, and all 5 need to use a different PayPal address.

I found this hook that can change the Paypal address, although not too sure how to add it in exactly (code is 3 years old apparently).

$paypal_args = apply_filters( 'woocommerce_paypal_args', $paypal_args );
add_filter( 'woocommerce_paypal_args' , 'custom_override_paypal_email' );

function custom_override_paypal_email( $paypal_args ) {
    $paypal_args['business'] = '[email protected]';
    print_r( $paypal_args['business'] );
    return $paypal_args;
}

How can I use this hook to change the Paypal address depending on which page/product the user is on?

4
  • and what if all of the products are in cart? Mar 21, 2016 at 1:17
  • @Reigel - I direct the user straight to checkout instead of the cart
    – Fizzix
    Mar 21, 2016 at 1:19
  • but even though you directed them to checkout, they can still visit another product and add it to cart... Mar 21, 2016 at 1:22
  • 1
    @Reigel - Very true. I'll need to clear the cart each time a new product is added then.
    – Fizzix
    Mar 21, 2016 at 1:23

1 Answer 1

7
+50

I've checked and found out that woocommerce_paypal_args has two arguments, the settings and the order. So based on the order, we can check what product it has and use the appropriate email.

add_filter( 'woocommerce_paypal_args' , 'custom_override_paypal_email', 10, 2 );

function custom_override_paypal_email( $paypal_args, $order ) {
    foreach ( $order->get_items() as $item ) {
       switch( $item['product_id'] ) {
           case 561:
             $paypal_args['business'] = '[email protected]';
             break;
           case 562:
             $paypal_args['business'] = '[email protected]';
             break;
       }
    }

    return $paypal_args;
}

please take note that you have to make sure that there can only be one item on the cart. If there are more than 1 product in the cart, this will use the last found product in the foreach loop. The code above is just for guidance, please change accordingly.

5
  • Looks very good. Would i just place that in my Wordpress functions.php file?
    – Fizzix
    Mar 21, 2016 at 2:04
  • Just tested it with sandbox accounts and works perfectly. Will award the bounty in 21 hours. Thank you.
    – Fizzix
    Mar 21, 2016 at 2:18
  • Are you aware of a hook that sends the confirmation email to that particular email address to?
    – Fizzix
    Mar 21, 2016 at 2:22
  • 1
    @Reigel In switch( $item['prouct_id'] ) { isn't switch( $item['product_id'] ) {? :) Apr 6, 2016 at 21:00
  • Is this method safe to use?
    – Shaun
    Dec 11, 2017 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.