You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce订单接收页的BACS信息中插入订单号

WooCommerce BACS付款方式自动填充订单号作为转账参考字段的实现方案

我们的WooCommerce订单号已显示在订单接收页顶部“您的订单已收到...”提示下方,多数客户采用Direct Bank Transfer(即BACS)付款方式,需要将订单号作为银行转账的参考字段填写。现在希望在展示BACS银行详情时,自动把带前缀的WooCommerce订单号填入新增的Reference字段中。目前已有能正常显示BACS详情的代码,但找不到插入订单号的正确语法;尝试将订单号传入自定义函数时,发现参数是BACS数组而非订单对象,寻求正确实现方法。

第一次尝试代码

add_filter('woocommerce_bacs_account_fields','custom_bacs_fields');

function custom_bacs_fields() {
global $wpdb;
$account_details = get_option( 'woocommerce_bacs_accounts',
array(
   array(
    'account_name'   => get_option( 'account_name' ),
    'sort_code'      => get_option( 'sort_code' ),
    'bank_name'      => get_option( 'bank_name' ),
    'account_number' => get_option( 'account_number' ),
    'iban'           => get_option( 'iban' ),
    'bic'            => get_option( 'bic' )
   )
)
);

$account_fields = array(
'bank_name'      => array(
    'label' => 'Bank',
    'value' => $account_details[0]['bank_name']
),
'sort_code' => array(
    'label' => __( 'Sort Code', 'woocommerce' ),
    'value' => $account_details[0]['sort_code']
),
'account_number' => array(
    'label' => __( 'Account Number', 'woocommerce' ),
    'value' => $account_details[0]['account_number']
),
'ref' => array(
    'label' => __( 'Ref', 'woocommerce' ),
    'value' => 'qwerty' //  $order[0]->get_order_number()  << 此处需要正确语法
),  
   
'iban' => array(
    'label' => __( 'iban', 'woocommerce' ),
    'value' => $account_details[0]['iban']
),
'bic' => array(
    'label' => __( 'bic', 'woocommerce' ),
    'value' => $account_details[0]['bic']
)
);
 return $account_fields;
}

第二次尝试代码

add_filter('woocommerce_bacs_account_fields','custom_bacs_fields');

function custom_bacs_fields( $order ) {
   print_r( $order );
   //$order_id  = $order->get_id(); // 获取订单ID
global $wpdb;
...

打印结果

Array ( [bank_name] => Array ( [label] => Bank [value] => MyBank ) [account_number] => Array ( [label] => Account number [value] => 01234567 ) [sort_code] => Array ( [label] => Sort code [value] => 00-00-00 ) [iban] => Array ( [label] ... 等等 ) )


解决方案

核心思路

woocommerce_bacs_account_fields过滤器仅传递BACS账户字段数组,无法直接获取订单对象。需通过WooCommerce内置的查询机制获取当前订单ID,再实例化订单对象来获取订单号。

完整实现代码

add_filter('woocommerce_bacs_account_fields', 'custom_bacs_fields_with_order_ref');

function custom_bacs_fields_with_order_ref($account_fields) {
    // 获取当前订单接收页的订单ID
    $order_id = WC()->query->get('order_id');
    
    if ($order_id) {
        // 实例化订单对象
        $order = wc_get_order($order_id);
        
        if ($order) {
            // 获取带前缀的完整订单号
            $order_number = $order->get_order_number();
            
            // 添加/更新参考字段
            $account_fields['ref'] = array(
                'label' => __('Ref', 'woocommerce'),
                'value' => $order_number
            );
        }
    }

    // 返回修改后的BACS字段数组
    return $account_fields;
}

代码说明

  1. 获取订单ID:通过WC()->query->get('order_id')直接在订单接收页获取当前订单ID,无需依赖全局变量,兼容性更强;
  2. 实例化订单对象:使用官方推荐的wc_get_order()函数获取订单对象,确保能调用官方提供的订单方法;
  3. 获取订单号:调用get_order_number()方法获取带前缀的完整订单号,符合WooCommerce的订单号显示逻辑;
  4. 修改字段数组:直接在原有BACS字段数组中添加ref字段,避免重新构造所有字段导致的默认配置丢失问题。

内容的提问来源于stack exchange,提问作者user801347

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 19:30:55