T
T
TinkyGO2020-12-02 10:32:40
WordPress
TinkyGO, 2020-12-02 10:32:40

How to add the ability to select a WooCommerce delivery date/time?

Own, subject. Googled some plugins, but it won't start. After setting up the plugins, this option did not appear.

Well, it's okay, I think you can cut it yourself. It's just type=date, I just need to know how to throw the whole thing into checkout and add it to the meta, so that later it will be displayed in the order.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bodrosh, 2020-12-02
@Bodrosh

This is how you can (maybe you need the flatpickr library for the dropdown calendar b dhtvtyb)

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    echo '<div class="form-row notes"><br><label>Дата и время получения заказа</label>';

    woocommerce_form_field( 'client_date', array(
        'type'          => 'text',
        'class'         => array('form-row-first'),
        'label'         => __(''),
        'placeholder'   => _x('Дата', 'placeholder', 'woocommerce'),
    ), $checkout->get_value( 'client_date' ));

    woocommerce_form_field( 'client_time', array(
        'type'          => 'text',
        'class'         => array('form-row-last'),
        'label'         => __(''),
        'placeholder'   => _x('Время', 'placeholder', 'woocommerce'),
    ), $checkout->get_value( 'client_time' ));

    echo '</div>';
    echo '<div class="form-row notes br-notes-1"><label>Время заказа определяется после звонка оператора.</label></div>';

}
/**
 * Обновляем добавленные поля в БД
 */
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 ( ! empty( $_POST['client_date'] ) ) {
        update_post_meta( $order_id, 'client_date', sanitize_text_field( $_POST['client_date'] ) );
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Дата и время').':</strong> ' . get_post_meta( $order->get_id(), 'client_date', true ) . ' ' .get_post_meta( $order->get_id(), 'client_time', true ) . '</p>';
}
/* Отправка добавленных полей на email
*/
add_filter('woocommerce_email_order_meta_fields', 'br_add_email_order_meta_fields', 10, 3);

function br_add_email_order_meta_fields( $fields, $sent_to_admin, $order_obj ) {
 $fields['client_date'] = array(
        'label' => 'Дата и время',
        'value' => get_post_meta( $order_num, 'client_date', true ) . ' ' . get_post_meta( $order_num, 'client_time', true )
    );
}
add_action( 'wp_footer', 'awoohc_add_script_update_shipping_method' );
function awoohc_add_script_update_shipping_method() {
    if ( is_checkout() ) { ?>
         <script>
            jQuery(document).ready(function ($) {

                  if( $("#client_date").length > 0 ) {
                    let optsDate = {
                        "locale": "ru",
                        "dateFormat":  "d.m.Y",
                    };
                    $("#client_date").flatpickr(optsDate);
                }

                if( $("#client_time").length > 0 ) {
                    let optsTime = {
                        locale: "ru",
                        enableTime: true,
                        noCalendar: true,
                        dateFormat: "H:i",
                        time_24hr: true
                    };

                    $("#client_time").flatpickr(optsTime);
                }
            });



        </script>
 <?php    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question