Note: This code is made to work in Woocommerce 3+ (but not in very old version 2.3)
Your question is not so clear… so you have mainly 2 options (and checking add to cart at the end for both options, when customer is registered, when shipping country is detected or has been set in cart or checkout):
OPTION 1 - Instead of removing shipping methods for a product that is not shippable for all other countries than Italy, you should better remove the related cart item displaying a custom notice… You will have to define the product Ids in the function that are only shippable in Italy:
add_action( 'woocommerce_before_calculate_totals', 'checking_and_removing_items', 10, 1 );
function checking_and_removing_items( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$custome_shipping_country = WC()->customer->get_shipping_country();
if( empty($custome_shipping_country) ){
$package = WC()->shipping->get_packages()[0];
if( ! isset($package['destination']['country']) ) return;
$custome_shipping_country = $package['destination']['country'];
}
// Only for NON Italians customers
if( $custome_shipping_country == 'IT' ) return;
// ==> HERE set your product IDs (ITALY ONLY)
$products_ids = array(37, 57);
// Iterate through each cart item
$found = false;
foreach( $cart->get_cart() as $cart_item_key => $cart_item )
if( in_array( $cart_item['data']->get_id(), $products_ids ) ){
$found = true;
$cart->remove_cart_item( $cart_item_key ); // remove item
}
if( $found ){
// Custom notice
wc_clear_notices();
wc_add_notice('Some products are not shippable to your country and have been removed', 'notice');
}
}
Code goes in function.php file of your active child theme (active theme).
Add to cart validation feature is at the end…
OPTION 2 - Removing shipping methods for a product that is not shippable for all other countries than Italy and displaying a custom error notice… You will have to define the product Ids in the function that are only shippable in Italy:
add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
return $rates;
// Only for NON Italians customers
if( $package['destination']['country'] == 'IT' ) return $rates;
// ==> HERE set your product IDs (ITALY ONLY)
$products_ids = array(37, 57);
// Loop through cart items and checking
$found = false;
foreach( $package['contents'] as $item )
if( in_array( $item['data']->get_id(), $products_ids ) ){
$found = true;
break;
}
if( ! $found ) return $rates; // If nothing is found: We EXIT
foreach( $rates as $rate_id => $rate )
unset($rates[$rate_id]); // Removing all shipping methods
// Custom notice
wc_clear_notices();
wc_add_notice('Some products are only shippable for Italy', 'error');
return $rates;
}
Code goes in function.php file of your active child theme (active theme).
Add to cart validation feature with a custom notice (for both options).
You will have to define the product Ids in the function that are only shippable in Italy.
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_products_for_non_italian', 20, 3 );
function avoid_products_for_non_italian( $passed, $product_id, $quantity ) {
$custome_shipping_country = WC()->customer->get_shipping_country();
if( empty($custome_shipping_country) ){
$package = WC()->shipping->get_packages()[0];
if( ! isset($package['destination']['country']) ) return $passed;
$custome_shipping_country = $package['destination']['country'];
}
// Only for NON Italians customers
if( $custome_shipping_country == 'IT' ) return $passed;
// ==> HERE set your product IDs (ITALY ONLY)
$products_ids = array(37, 57);
// The condition
if( in_array( $product_id, $products_ids ) ){
$passed = false;
wc_add_notice( 'This product is only shippable for Italy.', 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (active theme).
All code is tested and works for Woocommerce version 3+ (may be 2.6.x too)