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

如何在WooCommerce下单后清空结账页Billing email与Billing Phone字段

WooCommerce 指定结账字段清空方案

你原来使用的__return_empty_string是全局返回空值的快捷函数,只要替换为自定义回调函数,仅对指定字段返回空即可,示例代码如下:

add_filter( 'woocommerce_checkout_get_value', 'custom_clear_specific_checkout_fields', 10, 2 );
function custom_clear_specific_checkout_fields( $value, $input ) {
    // 定义需要清空的结账字段,可按需增删
    $clear_fields = [
        'billing_email',
        'billing_phone'
    ];
    
    if ( in_array( $input, $clear_fields, true ) ) {
        return '';
    }
    return $value;
}

代码说明

  • 回调函数接收两个参数:$value是字段原本的默认值,$input是当前结账字段的名称
  • 你可以按需修改$clear_fields数组内的字段名,和WooCommerce默认结账字段名保持一致即可
  • 以上代码不会修改用户账户内已存储的字段数据,仅在结账页加载时不自动填充指定字段,如果你需要同时清空用户账户内存储的对应字段,可以额外添加以下代码:
add_action( 'woocommerce_thankyou', 'custom_clear_user_saved_fields_after_order', 10, 1 );
function custom_clear_user_saved_fields_after_order( $order_id ) {
    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();
    
    // 仅处理注册用户订单
    if ( $user_id > 0 ) {
        update_user_meta( $user_id, 'billing_email', '' );
        update_user_meta( $user_id, 'billing_phone', '' );
    }
}

内容的提问来源于stack exchange,提问作者Jonathan Hernández Lara

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 12:48:04