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'] = 'paypalemail@domain.com';
    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?

Fizzix
  • 23,679
  • 38
  • 110
  • 176

1 Answers1

7

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'] = 'paypalemail1@domain.com';
             break;
           case 562:
             $paypal_args['business'] = 'paypalemail2@domain.com';
             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.

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139