如何在 Woocommerce 中动态更改 PayPal 地址? [英] How to dynamically change the PayPal address in Woocommerce?

查看:95
本文介绍了如何在 Woocommerce 中动态更改 PayPal 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 Woocommerce 使用的 PayPal 地址,具体取决于它们所在的页面.我目前只有 5 个产品,所有 5 个产品都需要使用不同的 PayPal 地址.

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.

我发现这个钩子可以更改 Paypal 地址,虽然不太确定如何准确添加它(代码显然已经 3 年了).

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;
}

如何使用此钩子根据用户所在的页面/产品更改 Paypal 地址?

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

推荐答案

我检查并发现 woocommerce_paypal_args 有两个参数,设置和顺序.所以根据订单,我们可以检查它有什么产品并使用相应的电子邮件.

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;
}

请注意,您必须确保购物车上只能有一件商品.如果购物车中有 1 个以上的产品,这将使用 foreach 循环中最后找到的产品.以上代码仅供参考,请自行修改.

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.

这篇关于如何在 Woocommerce 中动态更改 PayPal 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆