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
相关产品推荐
相关产品推荐

