Add delivery date and time for woocommerce



/**
 * Add the field to the checkout
 * */
add_action('woocommerce_after_checkout_billing_form', 'my_custom_checkout_field');

function my_custom_checkout_field($checkout) {

    echo '

' . __('Prefered delivery time', 'woothemes') . '

'; $tomorrow = date('Y-m-d', strtotime(the_date('Y-m-d', '','', FALSE) .' +1 day')); $after_tomorrow = date('Y-m-d', strtotime(the_date('Y-m-d', '','', FALSE) .' +2 day')); $_3day_now = date('Y-m-d', strtotime(the_date('Y-m-d','','', FALSE) .' +3 day')); echo '
'; woocommerce_form_field('prefered_delivery_date', array( 'type' => 'select', 'class' => array('form-row-first form-row-wide'), 'label' => '', 'options' => array( $tomorrow => $tomorrow, $after_tomorrow => $after_tomorrow, $_3day_now => $_3day_now ) ), $checkout->get_value('prefered_delivery_date')); echo ''; woocommerce_form_field('prefered_delivery_time', array( 'type' => 'select', 'class' => array('form-row-last form-row-wide'), 'label' =>'', 'options' => array( '9-12:00' => __('9-12:00', 'woothemes'), '12-15:00' => __('12-15:00', 'woothemes'), '15-18:00' => __('15-18:00', 'woothemes'), ) ), $checkout->get_value('prefered_delivery_time')); echo '
'; _e('

Orders placed after 6pm will not be processed until the next business day.

', 'woothemes'); echo '
'; } /** ?* Process the checkout ?* */ add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); function my_custom_checkout_field_process() { global $woocommerce; if (!$_POST['prefered_delivery_time']) $woocommerce->add_error(__('Please enter something into this new shiny field.', 'woothemes')); } /** ?* Update the order meta with field value ?* */ add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); function my_custom_checkout_field_update_order_meta($order_id) { if ($_POST['prefered_delivery_date']) update_post_meta($order_id, 'Prefered delivery date', esc_attr($_POST['prefered_delivery_date'])); if ($_POST['prefered_delivery_time']) update_post_meta($order_id, 'Prefered delivery time', esc_attr($_POST['prefered_delivery_time'])); } add_filter('manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns'); add_action('manage_shop_order_posts_custom_column', 'custom_shop_order_column', 10, 2); function set_custom_edit_shop_order_columns($columns) { //unset($columns['date']); return $columns + array('delivery_time' => __('Delivery time', 'woothemes')); } function custom_shop_order_column($column, $post_id) { switch ($column) { case 'delivery_time': echo get_post_meta($post_id, 'Prefered delivery date', true)."
"; echo get_post_meta($post_id, 'Prefered delivery time', true); break; } } add_action('woocommerce_admin_order_data_after_billing_address', 'add_delivery_order_data'); function add_delivery_order_data($checkout) { global $post; echo '

' . __('Custom prefered delivery time', 'woothemes') . '

'; echo '

' . get_post_meta($post->ID, 'Prefered delivery date', true) .'
'. get_post_meta($post->ID, 'Prefered delivery time', true) . '

'; }

Comments

1,289 responses to “Add delivery date and time for woocommerce”