Home / Admin / WooCommerce Local Pickup Date Field | WooCommerce Pickup Date
Duplicate Snippet

Embed Snippet on Your Site

WooCommerce Local Pickup Date Field | WooCommerce Pickup Date

Adds a date picker to the checkout page when "Local Pickup" is selected. The selected pickup date is saved to the order, shown in admin, order emails, and on the Thank You page. Field is hidden for other shipping methods and is required only when local pickup is selected.

Code Preview
php
<?php
// 1. Show pickup date after shipping & before tax/totals
add_action('woocommerce_review_order_after_shipping', 'custom_pickup_date_field');
function custom_pickup_date_field() {
    $tomorrow = date('Y-m-d', strtotime('+1 day'));
    ?>
    <tr class="pickup-date-row" style="display:none;">
        <th><?php _e('Pickup Date', 'wpexpert28'); ?> <span class="required">*</span></th>
        <td>
            <input type="date"
                   name="pickup_date"
                   id="pickup_date"
                   class="input-text"
                   min="<?php echo esc_attr($tomorrow); ?>"
                   value="<?php echo esc_attr($tomorrow); ?>" />
        </td>
    </tr>
    <script>
        function togglePickupDateRow() {
            const selected = document.querySelector('input[name^="shipping_method"]:checked');
            const isPickup = selected && selected.value.includes("local_pickup");
            const row = document.querySelector('.pickup-date-row');
            const input = document.querySelector('#pickup_date');
            if (row && input) {
                row.style.display = isPickup ? 'table-row' : 'none';
                input.required = isPickup;
                // Set default value to tomorrow if not set
                if (isPickup && !input.value) {
                    const today = new Date();
                    today.setDate(today.getDate() + 1);
                    const yyyy = today.getFullYear();
                    const mm = String(today.getMonth() + 1).padStart(2, '0');
                    const dd = String(today.getDate()).padStart(2, '0');
                    input.value = `${yyyy}-${mm}-${dd}`;
                }
                if (!isPickup) {
                    input.value = '';
                }
            }
        }
        jQuery(function($) {
            togglePickupDateRow();
            $(document.body).on('updated_checkout updated_shipping_method change', togglePickupDateRow);
        });
    </script>
    <?php
}
// 2. Validate if Local Pickup is selected
add_action('woocommerce_checkout_process', 'validate_pickup_date_local_pickup');
function validate_pickup_date_local_pickup() {
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    if (is_array($chosen_methods) && strpos($chosen_methods[0], 'local_pickup') !== false) {
        if (empty($_POST['pickup_date'])) {
            wc_add_notice(__('Please select a pickup date for Local Pickup.'), 'error');
        }
    }
}
// 3. Save pickup date to order meta
add_action('woocommerce_checkout_create_order', 'save_pickup_date_order_meta_final', 10, 2);
function save_pickup_date_order_meta_final($order, $data) {
    if (!empty($_POST['pickup_date'])) {
        $order->update_meta_data('_pickup_date', sanitize_text_field($_POST['pickup_date']));
    }
}
// 4. Show pickup date in admin order screen
add_action('woocommerce_admin_order_data_after_shipping_address', 'admin_order_pickup_date_display', 10, 1);
function admin_order_pickup_date_display($order) {
    $pickup_date = $order->get_meta('_pickup_date');
    $chosen_methods = $order->get_shipping_methods();
    foreach ($chosen_methods as $method) {
        if (strpos($method->get_method_id(), 'local_pickup') !== false && $pickup_date) {
            echo '<p><strong>' . __('Pickup Date:', 'wpexpert28') . '</strong> ' . esc_html($pickup_date) . '</p>';
        }
    }
}
// 5. Show pickup date in order emails
add_filter('woocommerce_email_order_meta_fields', 'pickup_date_in_email_meta', 10, 3);
function pickup_date_in_email_meta($fields, $sent_to_admin, $order) {
    $pickup_date = $order->get_meta('_pickup_date');
    $chosen_methods = $order->get_shipping_methods();
    foreach ($chosen_methods as $method) {
        if (strpos($method->get_method_id(), 'local_pickup') !== false && $pickup_date) {
            $formatted_date = date('d-m-Y', strtotime($pickup_date));
            $fields['pickup_date'] = array(
                'label' => __('Pickup Date', 'wpexpert28'),
                'value' => esc_html($formatted_date),
            );
            break;
        }
    }
    return $fields;
}
//show on thank you page
add_filter('woocommerce_get_order_item_totals', 'insert_pickup_date_after_shipping_row', 20, 3);
function insert_pickup_date_after_shipping_row($totals, $order, $tax_display) {
    $pickup_date = $order->get_meta('_pickup_date');
    $chosen_methods = $order->get_shipping_methods();
    $is_local_pickup = false;
    foreach ($chosen_methods as $method) {
        if (strpos($method->get_method_id(), 'local_pickup') !== false) {
            $is_local_pickup = true;
            break;
        }
    }
    if ($pickup_date && $is_local_pickup) {
        $formatted_date = date('d-m-Y', strtotime($pickup_date));
        $new_totals = [];
        foreach ($totals as $key => $total) {
            $new_totals[$key] = $total;
            if ($key === 'shipping') {
                $new_totals['pickup_date'] = array(
                    'label' => __('Pickup Date', 'wpexpert28'),
                    'value' => esc_html($formatted_date),
                );
            }
        }
        return $new_totals;
    }
    return $totals;
}

Comments

Add a Comment