php - Change PayPal adress based on order total in WooCommerce

I'm looking to find a solution to my problem. I would like to change the PayPal email based on WooCommerce order total.

So, if order total is < 10$

  • Receiver PayPal = paypalemail1@domain.com

Else

  • Receiver PayPal = paypalemail2@domain.com

I tried to search here and the only code that I found was this:

add_filter( 'woocommerce_paypal_args', 'woocommerce_paypal_args', 10, 2 );
    function woocommerce_paypal_args( $paypal_args, $order ) {

    //Get the customer ID
    $user_id = $order->get_user_id();

    // Get the user data
    $user_data = get_userdata( $customer_id );

    // Adding an additional recipient for a custom user role

    if ( in_array( 'wholesale_customer', $user_data->roles )  )
        $paypal_args['business'] = 'email@email.com';

    return $paypal_args;
}

But here is not based on WooCommerce order total but on the user role. Is there any way to personalise it?

Answer

Solution:

woocommerce_paypal_args has two arguments, the settings and the $order object. So based on the order, we can get the total and based on that, change the email

function filter_woocommerce_paypal_args( $paypal_args, $order ) {
    // Get total
    $order_total = $order->get_total();
    
    // Less then 10
    if ( $order_total < 10 ) {
        $paypal_args['business'] = 'paypalemail1@domain.com';
    } else {
        $paypal_args['business'] = 'paypalemail2@domain.com';       
    }

    return $paypal_args;
}
add_filter( 'woocommerce_paypal_args', 'filter_woocommerce_paypal_args', 10, 2 );

Source