Show/hide certain countries on checkout for particular products in cart WooCommerce on child sites.
Snippet Type
Execute on Child Sites
Snippet
function filter_woocommerce_countries_allowed_countries( $countries ) {
// Cart or checkout page
if ( is_cart() || is_checkout() ) {
// The targeted product ids
$targeted_ids = array( 30, 815 );
// Flag
$found = false;
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
}
// True
if ( $found ) {
// Remove
unset( $countries[ 'NL' ] );
unset( $countries[ 'FR' ] );
}
}
// Return
return $countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'filter_woocommerce_countries_allowed_countries', 10, 1 );