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

如何在WooCommerce中设置禁运国家并限制结账付款?

Hey there! Let me walk you through exactly how to block shipments to specific countries (like China) in WooCommerce, and ensure customers can't proceed to checkout if they select a restricted country.

方法一:使用WooCommerce内置设置(推荐)

You were right to suspect it's tied to shipping settings—you just might have missed the key section. Here's the step-by-step breakdown:

  • Head to your WordPress dashboard, navigate to WooCommerce > Settings, then click the Shipping tab.
  • Look for Shipping Zones—this is where you control which countries get delivery options.
  • Click into the shipping zone you want to modify (e.g., "Global Shipping" or your existing primary zone), then hit the "Edit" button next to Zone Locations.
  • In the popup window, select Exclude specific countries, then find and check "China" from the dropdown list. Save your changes.
  • Critical check: Make sure no other shipping zone includes China. If another zone accidentally covers it, your restriction won't work, so audit all your zones' location rules.
  • Final touch: Go back to WooCommerce Settings > General tab. Under Ship to countries, select Ship to specific countries, then check all the countries you do support. Now, when a customer selects China at checkout, WooCommerce will immediately show a "cannot ship to this location" message and block access to the payment step.
方法二:自定义代码(针对更复杂需求)

If the built-in settings don't give you enough control, you can add a simple code snippet to enforce the restriction directly during checkout:

add_action( 'woocommerce_checkout_process', 'restrict_checkout_to_allowed_countries' );
function restrict_checkout_to_allowed_countries() {
    // Get the customer's selected shipping country
    $selected_country = WC()->customer->get_shipping_country();
    
    // Define restricted country codes (CN = China)
    $restricted_countries = array( 'CN' );
    
    // Block checkout if restricted country is selected
    if ( in_array( $selected_country, $restricted_countries ) ) {
        wc_add_notice( '抱歉,我们目前不支持向中国发货,请选择其他支持的国家继续结账。', 'error' );
    }
}
  • Add this code to your child theme's functions.php file, or use a custom code snippet plugin (avoid editing the parent theme directly—updates will erase your changes).
  • The two-letter country code (e.g., CN for China) follows ISO 3166 standards—you can add more countries to the $restricted_countries array if needed.
Quick Tip

Don't forget to test the flow! Switch to your store's front end, go through the checkout process, and select China to confirm the restriction works as expected.

内容的提问来源于stack exchange,提问作者Toby Van der Veer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:34:36