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

WooCommerce:按条件显示价格后缀(仅标准税对应国家显示+tax)

Solution: Show '+tax' Price Suffix Only for Specified Tax Countries in WooCommerce

I get it, the Price Based on Country plugin not working can be frustrating. Let’s go with a custom code approach that gives you full control—this will check the user’s location against your standard tax countries and only append the '+tax' suffix when they match.

Step 1: Add the Custom Code

Here’s a snippet that hooks into WooCommerce’s price formatting filter. It pulls your configured tax countries directly from WooCommerce settings, checks the user’s current country, and adds the suffix only when needed:

add_filter('woocommerce_get_price_suffix', 'add_tax_suffix_for_specified_countries', 10, 4);
function add_tax_suffix_for_specified_countries($suffix, $product, $price, $qty) {
    // Get the countries where tax is applied (from WooCommerce standard tax settings)
    $tax_countries = get_option('woocommerce_tax_country');
    if (empty($tax_countries)) {
        return $suffix; // No tax countries set, return original suffix
    }
    
    // Convert tax countries to an array (in case multiple are set)
    $tax_countries_array = is_array($tax_countries) ? $tax_countries : array($tax_countries);
    
    // Get user's current country
    $user_country = WC()->customer->get_shipping_country();
    // Fallback to billing country if shipping isn't set
    if (empty($user_country)) {
        $user_country = WC()->customer->get_billing_country();
    }
    
    // Check if user's country is in the tax countries list
    if (!empty($user_country) && in_array($user_country, $tax_countries_array)) {
        $suffix .= ' +tax'; // Append the suffix
    }
    
    return $suffix;
}

Step 2: Where to Add This Code

You have two safe options:

  • Child Theme Functions.php: Go to Appearance → Theme File Editor, select your child theme, open functions.php, and paste the code at the bottom.
  • Code Snippets Plugin: Install and activate the "Code Snippets" plugin, create a new snippet, paste the code, and activate it. This is safer if you don’t want to edit theme files directly.

Step 3: Verify Your Settings

Make sure these are set up correctly in WooCommerce:

  • Enable geolocation: Go to WooCommerce → Settings → General, under "Default customer location" select "Geolocate" or "Geolocate (with page caching support)" so WooCommerce can detect the user’s country.
  • Confirm your tax countries: Check WooCommerce → Settings → Tax → Standard Rates to ensure the countries you want are listed here (the code pulls from the 'woocommerce_tax_country' option which is set based on these rates).

Troubleshooting Tips

  • If the suffix isn’t showing: Clear your site cache and browser cache, then test again. Geolocation might take a moment to detect your country.
  • If it’s showing for all users: Double-check that your tax countries are correctly set, and that geolocation is working (you can test by using a VPN to switch to a non-tax country).

This approach avoids plugin dependency and gives you direct control over when the suffix appears.

内容的提问来源于stack exchange,提问作者Sonu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:14:58