如何在WooCommerce中扩展get_european_union_countries()函数新增英国、美国
WooCommerce 国家匹配规则扩展方案
你不需要修改WooCommerce核心的get_european_union_countries()函数,核心代码修改后会在WooCommerce版本升级时被覆盖,直接在现有代码逻辑中合并额外需要匹配的国家即可,修改后的参考代码如下:
add_filter( 'wc_aelia_tdbc_keep_prices_fixed', function($keep_prices_fixed): bool { $countries = new WC_Countries(); $eu_countries = $countries->get_european_union_countries(); // 定义额外需要纳入匹配的国家,使用ISO 3166-1 alpha-2国家编码 $additional_countries = ['GB', 'US']; // 合并欧盟国家和额外国家得到最终匹配列表 $target_countries = array_merge($eu_countries, $additional_countries); if ( WC()->customer ) { $keep_prices_fixed = in_array( WC()->customer->get_billing_country(), $target_countries ); } return $keep_prices_fixed; });
说明
- 代码中
GB对应英国、US对应美国,采用的是WooCommerce通用的ISO 3166-1 alpha-2国家编码标准,后续需要新增其他国家时直接往$additional_countries数组中添加对应国家编码即可 - 该方案完全不触碰WooCommerce核心代码,版本升级不会丢失修改,兼容性更强
内容的提问来源于stack exchange,提问作者Jerry
相关产品推荐
相关产品推荐

