WooCommerce结账页非欧盟VAT提示修改:单国家值换为多国家数组
WooCommerce结账页非欧盟国家VAT提示多国家适配修改方案
核心修改点
- 将原来的单国家代码变量修改为包含挪威、英国、瑞士三国代码的数组
- 将原逻辑的单值相等判断更换为数组包含匹配,支持多国家触发提示
修改后完整代码
// 输出税费提示行 add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_echo_notice_shipping' ); function bbloomer_echo_notice_shipping() { echo '<tr class="non-eu-tax-notice" style="display:none"> <th>'. __( '提示', 'woocommerce' ) .'</th> <td data-title=" '. __( '提示', 'woocommerce' ) .' ">'. __( '未收取增值税。请注意,您可能需要在所在国申报增值税和关税,可在此处查看更多信息', 'woocommerce' ) .'</td> </tr>'; } // 根据账单国家切换提示的显示状态 add_action( 'woocommerce_checkout_after_order_review', 'bbloomer_show_notice_shipping' ); function bbloomer_show_notice_shipping(){ wc_enqueue_js( " // 设置需要显示提示的国家代码数组 var allowedCountries = ['NO', 'UK', 'CH']; // 获取结账页当前选中的国家代码 selectedCountry = $('select#billing_country').val(); // 切换提示显示状态的函数 function toggle_upsell( selectedCountry ) { if( allowedCountries.includes(selectedCountry) ){ $('.non-eu-tax-notice').show(); } else { $('.non-eu-tax-notice').hide(); } } // 页面加载时执行一次状态判断 toggle_upsell( selectedCountry ); // 国家选择变更时重新判断状态 $('select#billing_country').change(function(){ toggle_upsell( this.value ); }); " ); }
扩展说明
后续如果需要新增其他触发提示的国家,直接往allowedCountries数组里追加对应国家的ISO代码即可,无需修改其他逻辑。
内容的提问来源于stack exchange,提问作者Thyregod
相关产品推荐
相关产品推荐

