Delivery Day Selector on the Block Checkout
Adds a required "Delivery day" dropdown to the Checkout block listing the next working days, stored on the order with WooCommerce's additional fields API.
Opens WPIDE on
Code
$wpide_delivery_days = array(
'lead_days' => 1, // First day offered = today + this many days.
'count' => 6, // How many days to offer.
'weekends' => false,
);
add_action( 'woocommerce_init', function () use ( $wpide_delivery_days ) {
if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) {
return;
}
$options = array();
$day = new DateTimeImmutable( 'today', wp_timezone() );
$day = $day->modify( '+' . (int) $wpide_delivery_days['lead_days'] . ' days' );
while ( count( $options ) < $wpide_delivery_days['count'] ) {
if ( $wpide_delivery_days['weekends'] || (int) $day->format( 'N' ) < 6 ) {
$options[] = array( 'value' => $day->format( 'Y-m-d' ), 'label' => wp_date( 'l j F', $day->getTimestamp() ) );
}
$day = $day->modify( '+1 day' );
}
woocommerce_register_additional_checkout_field( array(
'id' => 'wpide/delivery-day',
'label' => 'Delivery day',
'location' => 'order',
'type' => 'select',
'required' => true,
'options' => $options,
) );
} );
Notes
Needs WooCommerce 8.9 or newer with the Checkout block, and the chosen day appears on the order screen and in emails automatically.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.