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

如何隐藏WooCommerce配送表单中的姓名等字段?

隐藏WooCommerce结账页面配送表单字段的方法

要直接隐藏配送表单中的姓名(或其他配送字段),你可以在现有的woocommerce_shipping_fields过滤器函数里,通过unset()移除整个字段,而不是仅取消必填属性。

基础实现代码

替换你当前的代码为以下内容,即可直接移除配送名字和姓氏字段:

add_filter( 'woocommerce_shipping_fields', 'shipping_remove_fields' );
function shipping_remove_fields( $fields ) {
    // 移除配送姓氏字段
    unset( $fields['shipping_last_name'] );
    // 移除配送名字字段
    unset( $fields['shipping_first_name'] );
    
    // 如需隐藏其他配送字段,可继续添加unset语句
    // 示例:移除配送公司字段 -> unset( $fields['shipping_company'] );
    // 示例:移除配送地址字段 -> unset( $fields['shipping_address_1'] );
    
    return $fields;
}

注意事项

  • 代码需添加到当前主题的functions.php文件,或使用自定义代码插件(如Code Snippets)添加,避免主题更新导致代码丢失。
  • 若需仅在特定条件下隐藏字段(比如仅虚拟商品订单隐藏配送信息),可添加条件判断逻辑:
add_filter( 'woocommerce_shipping_fields', 'shipping_remove_fields' );
function shipping_remove_fields( $fields ) {
    // 判断购物车是否全为虚拟商品
    $all_virtual = true;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( ! $cart_item['data']->is_virtual() ) {
            $all_virtual = false;
            break;
        }
    }
    
    // 仅全虚拟商品时移除配送姓名字段
    if ( $all_virtual ) {
        unset( $fields['shipping_last_name'] );
        unset( $fields['shipping_first_name'] );
    }
    
    return $fields;
}

内容的提问来源于stack exchange,提问作者Victor Daniel Witoszek

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 05:42:06