问题
Can someone tell how can I remove the specific country from woocommerce. There is option in woocommerce that said selling locations with All countries and specific.
However I want to sell to all countries except the 1 country that is US for example! then how can I remove US from the countries list. As if I use "specific countries" option then I will have to add all the countries except the US which is longer process.
Is there any code you can help me with that I can put into functions of theme so that US country will not appear in the list of countries during checkout?
回答1:
Try this following snippet
function woo_remove_specific_country( $country )
{
unset($country["US"]);
return $country;
}
add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );
Reference http://www.boopathirajan.com/remove-specific-country-woocommerce-country-list/
回答2:
If you want to keep an array of countries, but you only have the keys, do something like this:
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 );
来源:https://stackoverflow.com/questions/37568595/how-to-remove-specific-country-in-woocommerce