如何同时向PayPal传递WooCommerce账单地址与配送地址?
同时向PayPal传递配送地址与账单地址的解决方案
当然可以同时给PayPal传递配送地址和账单地址!PayPal原生支持区分这两套地址的字段,你之前只是把账单地址映射到了配送地址的SHIPTO前缀字段,现在只需要补充账单地址对应的BILLTO前缀字段就能实现需求。
核心思路
PayPal的参数体系里,配送地址用SHIPTO开头的字段(就是你现在在用的那些),账单地址则对应BILLTO开头的同名字段:BILLTOSTREET、BILLTOSTREET2、BILLTOCITY、BILLTOSTATE、BILLTOZIP、BILLTOCOUNTRYCODE。
在WooCommerce中,你可以通过woocommerce_paypal_args这个钩子来修改传给PayPal的参数,同时注入账单地址和配送地址的信息。
代码实现
把下面的代码添加到你的主题functions.php文件或者自定义功能插件中即可:
add_filter('woocommerce_paypal_args', 'custom_paypal_add_billing_and_shipping_address'); function custom_paypal_add_billing_and_shipping_address($paypal_args) { // 获取当前下单用户的信息 $customer = WC()->customer; // 注入账单地址参数(BILLTO前缀) $paypal_args['BILLTOSTREET'] = $customer->get_billing_address(); $paypal_args['BILLTOSTREET2'] = $customer->get_billing_address_2(); $paypal_args['BILLTOCITY'] = $customer->get_billing_city(); $paypal_args['BILLTOSTATE'] = $customer->get_billing_state(); $paypal_args['BILLTOZIP'] = $customer->get_billing_postcode(); $paypal_args['BILLTOCOUNTRYCODE'] = $customer->get_billing_country(); // 注入配送地址参数(SHIPTO前缀) $paypal_args['SHIPTOSTREET'] = $customer->get_shipping_address(); $paypal_args['SHIPTOSTREET2'] = $customer->get_shipping_address_2(); $paypal_args['SHIPTOCITY'] = $customer->get_shipping_city(); $paypal_args['SHIPTOSTATE'] = $customer->get_shipping_state(); $paypal_args['SHIPTOZIP'] = $customer->get_shipping_postcode(); $paypal_args['SHIPTOCOUNTRYCODE'] = $customer->get_shipping_country(); return $paypal_args; }
注意事项
- 这个方法适用于WooCommerce官方的PayPal标准支付插件,以及大多数基于官方API开发的PayPal扩展;
- 如果用户下单时选择了和账单地址不同的配送地址,这套代码会自动传递对应的不同信息;如果用户选择同地址,两个地址字段会传递相同内容;
- 添加代码前记得备份主题文件,避免出现意外问题。
内容的提问来源于stack exchange,提问作者Fasil Palanthodi
相关产品推荐
相关产品推荐

