如何在 WooCommerce 中删除特定国家

How to remove specific country in WooCommerce

谁能告诉我如何从 woocommerce 中删除特定国家/地区。 woocommerce 中有一个选项,表示销售地点包含所有国家和特定国家。

但是我想卖给除 1 个国家以外的所有国家,例如美国!那么我怎样才能从国家列表中删除美国。就像我使用 "specific countries" 选项一样,我将不得不添加除美国以外的所有国家/地区,这是一个较长的过程。

有什么代码可以帮助我,我可以将其放入主题功能中,以便在结账时美国国家不会出现在国家列表中吗?

尝试以下代码段

function woo_remove_specific_country( $country ) 
{
   unset($country["US"]);
   return $country; 
}
add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );

参考 http://www.boopathirajan.com/remove-specific-country-woocommerce-country-list/

如果您想保留一系列国家/地区,但您只有键,请执行以下操作:

function woo_remove_specific_country( $countries ) {

    global $woocommerce;

    // default do not limit
    $limited_countries = array_values(array_flip($countries));

    // Country array to keep
    $eu = $woocommerce->countries->get_european_union_countries( );

    // keep countries, sort them out of the full array to keep the names
    $found   = array_filter($countries, function($item) use ($eu) {
        return in_array($item, $eu);
    }, ARRAY_FILTER_USE_KEY); // USE_KEY is essential cause we are filtering the language codes

    // return the found countries
    return $found;
}
add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );