How to dynamically change the PayPal address in Woocommerce?

后端 未结 1 490
遥遥无期
遥遥无期 2021-01-14 07:34

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 P

1条回答
  •  别那么骄傲
    2021-01-14 08:10

    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.

    0 讨论(0)
提交回复
热议问题