Your cart is currently empty.
';
woocommerce_form_field('delivery_date', [
'type' => 'date',
'required' => true,
'label' => __('Delivery date', 'woocommerce'),
'input_class' => ['input-text'],
], WC()->session->get('delivery_date'));
echo '
';
}
}
add_action('woocommerce_cart_collaterals', 'bd_delivery_date_field_cart', 20);
function bd_delivery_date_field_checkout( $fields ) {
// If you already have a checkout field, skip this block
$fields['billing']['delivery_date'] = [
'type' => 'date',
'required' => true,
'label' => __('Delivery date', 'woocommerce'),
'priority' => 25,
];
return $fields;
}
add_filter('woocommerce_checkout_fields', 'bd_delivery_date_field_checkout');
/* 2) Save to WC session whenever it’s posted (cart or checkout) */
function bd_capture_delivery_date_to_session() {
if ( isset($_POST['delivery_date']) ) {
WC()->session->set('delivery_date', sanitize_text_field($_POST['delivery_date']));
}
}
add_action('wp_loaded', 'bd_capture_delivery_date_to_session');
/* 3) Server-side validation (blocks Apple Pay/express if missing) */
function bd_require_delivery_date() {
$date = isset($_POST['delivery_date']) ? sanitize_text_field($_POST['delivery_date']) : WC()->session->get('delivery_date');
if ( empty($date) ) {
wc_add_notice(__('Please select a delivery date.'), 'error');
}
}
add_action('woocommerce_checkout_process', 'bd_require_delivery_date');
/* 4) Copy the date to the order no matter the gateway (incl. Apple Pay) */
function bd_add_delivery_date_to_order( $order, $data ) {
$date = isset($_POST['delivery_date']) ? sanitize_text_field($_POST['delivery_date']) : WC()->session->get('delivery_date');
if ( $date ) {
$order->update_meta_data('_delivery_date', $date);
}
}
add_action('woocommerce_checkout_create_order', 'bd_add_delivery_date_to_order', 10, 2);
/* 5) Hide Apple Pay / express buttons until a date is selected */
function bd_hide_prb_until_date_js() {
if ( ! ( is_cart() || is_checkout() ) ) return;
?>