×
Black Friday Savings
00Days
:
07Hours
:
49Minutes
:
25Seconds
Use Coupon 40%OFF

Includes 1 Year Priority Support

Shop Now

WP SITES

3093 Coded Tutorials & 296 Plugins

Delivery Date For WooCommerce Classic Checkout Page

This code enables you to add a text field for a delivery date to your checkout page. To add the field to the classic checkout here’s 2 parts.

1st, the form field which is a text field.

add_action( 'woocommerce_before_checkout_billing_form', 'wpsites_classic_checkout_date_picker', 999 );
function wpsites_classic_checkout_date_picker() {

    woocommerce_form_field( 'delivery_date', array(
        'type'         => 'text',
        'class'        => array( 'form-row-wide' ),
        'required'     => true,
        'label'        => 'Select Delivery Date',
        'input_class'  => array( 'datepicker' ),
    ), WC()->checkout->get_value( 'delivery_date' ) );

    }
    
}

2nd part is the save function to save the value entered into the field so it can be displayed after checkout is complete in different locations.

add_action( 'woocommerce_checkout_update_order_meta', 'wpsites_save_delivery_date' );
function wpsites_save_delivery_date( $order_id ) {
    if ( isset( $_POST['delivery_date'] ) ) {
        update_post_meta( $order_id, 'delivery_date', sanitize_text_field( $_POST['delivery_date'] ) );
    }
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'wpsites_delivery_date_display' );
function wpsites_delivery_date_display( $order ) {
    $delivery_date = get_post_meta( $order->get_id(), 'delivery_date', true );
    if ( $delivery_date ) {
        echo '<p><strong>Delivery Date:</strong> ' . esc_html( $delivery_date ) . '</p>';
    }
}

= Only for Shipping =

Only display field if product requires shipping :

add_action( 'woocommerce_before_checkout_billing_form', 'wpsites_classic_checkout_date_picker', 999 );
function wpsites_classic_checkout_date_picker() {

    if ( WC()->cart->needs_shipping() ) {
    
    echo '<div id="show-if-shipping"><h3>Delivery Date</h3>';

    woocommerce_form_field( 'delivery_date', array(
        'type'         => 'text',
        'class'        => array( 'form-row-wide' ),
        'required'     => true,
        'label'        => 'Select Delivery Date',
        'input_class'  => array( 'datepicker' ),
    ), WC()->checkout->get_value( 'delivery_date' ) );
    
     echo '</div>';

    }
    
}

= Only for Specific Products =

Add your comma array of products ids.

add_action( 'woocommerce_after_checkout_billing_form', 'wpsites_product_date_picker' );
function wpsites_product_date_picker() {
    // Your product IDs
    $target_product_ids = array( 123 ); // Replace with your actual product IDs

    $found = false;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( in_array( $cart_item['product_id'], $target_product_ids ) ) {
            $found = true;
            break;
        }
    }

        woocommerce_form_field( 'delivery_date', array(
            'type'         => 'text',
            'class'        => array( 'form-row-wide' ),
            'required'     => true,
            'label'        => 'Select Delivery Date',
            'input_class'  => array( 'datepicker' ),
        ), WC()->checkout->get_value( 'delivery_date' ) );

}

= Only for Products in Taxonomy =

Swap out the product category and tag slugs.

add_action( 'woocommerce_after_checkout_billing_form', 'wpsites_taxonomy_date_picker' );
function wpsites_taxomomy_date_picker() {
    $target_categories = array( 'pre-order', 'custom-made' ); // Slugs of categories
    $target_tags       = array( 'deliverable', 'express' );    // Slugs of tags

    $found = false;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id'];

        // Check if product is in one of the target categories
        if ( has_term( $target_categories, 'product_cat', $product_id ) ) {
            $found = true;
            break;
        }

        // Check if product has one of the target tags
        if ( has_term( $target_tags, 'product_tag', $product_id ) ) {
            $found = true;
            break;
        }
    }

        woocommerce_form_field( 'delivery_date', array(
            'type'         => 'text',
            'class'        => array( 'form-row-wide' ),
            'required'     => true,
            'label'        => 'Select Delivery Date',
            'input_class'  => array( 'datepicker' ),
        ), WC()->checkout->get_value( 'delivery_date' ) );
    
}

= Location =

Swap out the default hook in the add_action woocommerce_before_checkout_billing_form to change the location of the delivery date field.

Hook NameDescription
woocommerce_before_checkout_formBefore the checkout form.
woocommerce_checkout_before_customer_detailsBefore billing/shipping section.
woocommerce_checkout_after_customer_detailsAfter billing/shipping section.
woocommerce_checkout_before_order_review_headingBefore the “Your order” heading.
woocommerce_checkout_before_order_reviewBefore the order review table.
woocommerce_checkout_order_reviewInside the order review table.
woocommerce_checkout_after_order_reviewAfter the order review table.
woocommerce_after_checkout_formAfter the entire checkout form.

= Related Plugins =

Was this helpful?

Yes
No
Thanks for your feedback!

Leave a Reply