Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce - Setup another PayPal email address if customer is a certain role

I'm trying to add another PayPal email address into Woocommerce if the customer is within a certain role, in this case a Wholesale Customer. Woocommerce by default only allows you to setup one PayPal account but I have been able to find the woocommerce_paypal_args function to change the arguments which are sent to PayPal. I can see the business field is responsible for holding the email address the payments are sent to.

I've got the code below which should intercept this and change it if the user is a wholesale_customer.

The question is.. How secure is this? Is there a better method of doing what I want?

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 protected]';

    return $paypal_args;
}
like image 514
Shaun Avatar asked Dec 12 '17 12:12

Shaun


1 Answers

I can tell you from experience building and modding WooCommerce gateways that this is both a perfectly secure and reasonable way of achieving this using your current plugin. That said... use some brackets when writing an if statement... this isn't python.

like image 102
Nicholas Koskowski Avatar answered Sep 28 '22 09:09

Nicholas Koskowski