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

WooCommerce结账页移除所有州/省选项代码报错解决方案

WooCommerce 结账页批量移除所有州/省相关内容方案

WooCommerce结账页默认会在HTML中输出全量州、城市列表及对应CDATA内容,若不需要展示州、城市相关字段,无需逐个国家配置移除规则。

效果截图

你之前直接unset($states)触发报错的核心原因是woocommerce_states钩子要求回调必须返回数组类型值,你把整个变量销毁后返回的是null,后续WooCommerce执行array_merge合并前端脚本参数时,就会抛出参数类型错误:

Warning: array_merge(): Expected parameter 1 to be an array, null given in /public_html/wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php on line 580

一次性清空所有州配置的正确写法是直接返回空数组,不会触发报错:

add_filter( 'woocommerce_states', function( $states ){
    // 清空所有国家绑定的州/省列表
    return [];
}, 999);

如果要彻底隐藏前端的州、城市输入字段,同时避免页面输出对应冗余CDATA数据,还需要额外加一段结账字段过滤代码:

add_filter( 'woocommerce_checkout_fields', function( $fields ) {
    // 移除账单、收货地址中的州、城市字段
    unset(
        $fields['billing']['billing_state'],
        $fields['billing']['billing_city'],
        $fields['shipping']['shipping_state'],
        $fields['shipping']['shipping_city']
    );
    return $fields;
}, 999);

两段代码都放到主题的functions.php文件中即可生效,不需要遍历逐个删除国家对应的州配置。


内容的提问来源于stack exchange,提问作者wp-ap

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 05:45:05