mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-03 12:12:25 +08:00
https://stackoverflow.com/questions/68111786/show-certain-countries-on-woocommerce-checkout-for-particular-products-in-cart/68111901
31 lines
961 B
Text
31 lines
961 B
Text
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 );
|