[tc_cart]

// Add a custom note field to the checkout
add_action('woocommerce_after_order_notes', 'custom_checkout_note_field');

function custom_checkout_note_field($checkout) {
    echo '<div id="custom_checkout_note_field"><h2>' . __('Custom Notes') . '</h2>';
    
    woocommerce_form_field('custom_notes', array(
        'type'          => 'textarea',
        'class'         => array('form-row-wide'),
        'label'         => __('Add any special instructions or notes here'),
        'placeholder'   => __('e.g. Leave at the gate or call on arrival'),
    ), $checkout->get_value('custom_notes'));

    echo '</div>';
}

// Save the custom note field
add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_note_field');

function save_custom_checkout_note_field($order_id) {
    if (!empty($_POST['custom_notes'])) {
        update_post_meta($order_id, '_custom_notes', sanitize_textarea_field($_POST['custom_notes']));
    }
}

// Display it in the admin order page
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_note_admin_order', 10, 1);

function display_custom_note_admin_order($order){
    $note = get_post_meta($order->get_id(), '_custom_notes', true);
    if ($note) {
        echo '<p><strong>Customer Note:</strong><br>' . esc_html($note) . '</p>';
    }
}