WP SITES

3085 Coded Tutorials & 288 Plugins

Add Lead Time for Completion Date to WooCommerce

This code adds a custom field to the single product edit screen enabling the storeowner to add a completion date for each product.

The estimated completion date shows on the single product page.

Add the code to the end of your child themes functions file or custom code snippets plugin.

add_action( 'woocommerce_product_options_general_product_data', 'add_lead_time_field' );
function add_lead_time_field() {

    woocommerce_wp_text_input( array(
        'id' => '_lead_time_days',
        'label' => 'Lead Time (Days)',
        'placeholder' => 'Number of days to complete',
        'type' => 'number',
        'custom_attributes' => array(
            'min' => '1',
            'step' => '1',
        ),
    ));
}

add_action( 'woocommerce_admin_process_product_object', 'save_lead_time_field' );
function save_lead_time_field( $product ) {

    if ( isset( $_POST['_lead_time_days'] ) ) {
    
        $product->update_meta_data( '_lead_time_days', absint( $_POST['_lead_time_days'] ) );
    }
}

add_action( 'woocommerce_single_product_summary', 'display_estimated_completion_date', 25 );
function display_estimated_completion_date() {

    global $product;
    
    $lead_time_days = $product->get_meta( '_lead_time_days' );

    if ( $lead_time_days ) {
      
        $completion_date = date( 'Y-m-d', strtotime( '+' . absint( $lead_time_days ) . ' days' ) );
        
        echo '<p class="product-completion-date">Estimated Completion Date: ' . esc_html( $completion_date ) . '</p>';
    }
}

Need More Coding Help?

Book a Consultation

Leave a Reply

New Plugins