如何在WooCommerce区块式结账国家选择栏置顶美国?
调整WooCommerce区块式结账的国家下拉列表排序(将美国置顶)
传统的WooCommerce结账页面钩子(比如woocommerce_countries)对**区块式结账(Block Checkout)**不生效,因为WooCommerce Blocks使用了独立的过滤器来控制国家选择器的排序。你可以用下面的代码实现将美国移至下拉列表首位:
代码实现
将以下代码添加到当前WordPress主题的functions.php文件,或者自定义功能插件中:
add_filter( 'woocommerce_blocks_country_selector_order', 'custom_reorder_blocks_country_selector' ); function custom_reorder_blocks_country_selector( $ordered_countries ) { // 先从原有列表中移除美国 if ( isset( $ordered_countries['US'] ) ) { $us_country = $ordered_countries['US']; unset( $ordered_countries['US'] ); // 将美国插入到列表最开头 $ordered_countries = array_merge( array( 'US' => $us_country ), $ordered_countries ); } return $ordered_countries; }
注意事项
- 优先推荐使用自定义功能插件添加代码,避免主题更新时代码丢失
- 修改完成后,清空WooCommerce的缓存(WooCommerce > 设置 > 高级 > 缓存)以及浏览器缓存,确保区块加载最新配置
- 如果你的站点使用多语言插件,代码中的国家名称会自动匹配当前语言,无需额外修改
内容的提问来源于stack exchange,提问作者Swapot
相关产品推荐
相关产品推荐

