Remove time select in WooCommerce Local Pickup Time Select when basket contains only virtual products or shipping is selected
Place in theme functions.php or similar
function jt_cart_has_physical_items() {
$has_physical = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( !$cart_item['data']->is_virtual( ) ) {
$has_physical = true;
break; // no point continuing once we've found one physical item
}
}
return $has_physical;
}
function jt_customer_picked_shipping() {
$has_shipping = false;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( stripos( $chosen_methods[0], 'shipping' ) ) {
$has_shipping = true;
}
return $has_shipping;
}
add_action( 'woocommerce_before_checkout_form', function() {
if ( jt_cart_has_physical_items() === false || jt_customer_picked_shipping() === true ) {
// find and unhook the time_select dropdown
$time_select_hooked_location = apply_filters( 'local_pickup_time_select_location', 'woocommerce_after_order_notes' );
remove_action( $time_select_hooked_location, [ Local_Pickup_Time::get_instance(), 'time_select' ] );
}
} );
add_action( 'woocommerce_checkout_process', function() {
if ( jt_cart_has_physical_items() === false || jt_customer_picked_shipping() === true ) {
// remove the processing of the field
remove_action( 'woocommerce_checkout_process', [ Local_Pickup_Time::get_instance(), 'field_process' ] );
remove_action( 'woocommerce_checkout_update_order_meta', [ Local_Pickup_Time::get_instance(), 'update_order_meta' ] );
}
}, 1 );