You should better try with the “Chosen shipping method” from WooCommerce session like:
function disable_checkout_button_no_shipping() {
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
// remove button if there is no chosen shipping method
if( empty( $chosen_shipping_methods ) ) {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );
Or in a different way using woocommerce_check_cart_items
action hook:
add_action( 'woocommerce_check_cart_items', 'required_chosen_shipping_methods' );
function required_chosen_shipping_methods() {
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
if( is_array( $chosen_shipping_methods ) && count( $chosen_shipping_methods ) > 0 ) {
// Display an error message
wc_add_notice( __("A shipping method is required in order to proceed to checkout."), 'error' );
}
}
It should better work.