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

如何在WooCommerce各处移除账单与配送字段中的电话?

嘿,我猜你尝试用这段代码移除WooCommerce结账流程里的电话字段,但可能没达到预期效果?别担心,我帮你梳理下正确的做法,顺便优化你的代码~

解决WooCommerce结账页面移除电话字段的问题

你的思路是对的,但有时候只通过unset移除字段可能不够——比如WooCommerce默认的验证规则、第三方主题/插件的干扰,都可能让电话字段“死灰复燃”。这里给你完善后的方案:

方法1:优化代码实现(彻底移除字段+解除验证)

下面是调整后的代码,不仅移除字段,还能避免出现不必要的验证错误:

add_filter( 'woocommerce_checkout_fields', 'pbj_woocommerce_checkout_fields' );
function pbj_woocommerce_checkout_fields( $fields ) {
    // 移除账单和配送模块的电话字段
    unset( $fields['billing']['billing_phone'] );
    unset( $fields['shipping']['shipping_phone'] );
    
    // 让邮箱字段占满整行(和你原本的需求保持一致)
    $fields['billing']['billing_email']['class'] = array('form-row-wide');
    
    return $fields;
}

// 额外添加:解除账单电话的必填验证(防止后台默认规则干扰)
add_filter( 'woocommerce_billing_fields', 'pbj_remove_billing_phone_required' );
function pbj_remove_billing_phone_required( $fields ) {
    if ( isset( $fields['billing_phone'] ) ) {
        $fields['billing_phone']['required'] = false;
    }
    return $fields;
}

// 解除配送电话的必填验证(按需添加)
add_filter( 'woocommerce_shipping_fields', 'pbj_remove_shipping_phone_required' );
function pbj_remove_shipping_phone_required( $fields ) {
    if ( isset( $fields['shipping_phone'] ) ) {
        $fields['shipping_phone']['required'] = false;
    }
    return $fields;
}

方法2:无需代码,通过后台设置搞定

如果你不想碰代码,其实WooCommerce自带了调整字段的功能:

  • 进入后台「WooCommerce」→「设置」→「高级」(部分版本在「账户与隐私」里)
  • 找到「结账字段」选项卡,分别找到账单电话和配送电话字段
  • 取消“必填”勾选,再选择“隐藏”状态即可

小提醒

  • 如果你用了第三方结账插件(比如Checkout Field Editor)或者定制主题,优先在插件/主题的专属设置里调整字段,避免代码和插件规则冲突
  • 修改代码前记得备份主题的functions.php,或者用子主题添加代码,防止主题更新后代码丢失

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:29:35