Add Custom Order Notes in WooCommerce Checkout Easily

How to add order notes in woocommerce checkout; Woocommerce add custom order notes field; Add note to checkout woocommerce; Woocommerce checkout order notes plugin; Customize order notes in woocommerce; Add order notes programmatically woocommerce; Woocommerce checkout page add notes; Enable order notes in woocommerce; Woocommerce order notes field customization; Add customer note in woocommerce checkout;

Explanation

To add a custom order notes field to your WooCommerce checkout page, this code does the trick. It introduces a new section where customers can leave additional notes about their order.

  • Adding the Field: The code hooks into the checkout process to display a new text area labeled "Order Notes" right after the existing order notes section. This is where customers can type in any extra information they want to share about their order.
  • Validation: Before the order is processed, the code checks if the customer has filled out the order notes field. If it's empty, an error message prompts them to provide some notes.
  • Saving the Notes: Once the order is placed, any notes entered by the customer are saved with the order details. This ensures that the information is stored and can be accessed later.
  • Viewing in Admin: On the admin side, when viewing an order, the custom notes are displayed under the order details. This makes it easy for store managers to see any special instructions or comments left by the customer.

This setup is perfect for businesses that need extra information from customers during the checkout process, like delivery instructions or gift messages.

Code

<?php
// Hook to add a custom order notes field in WooCommerce checkout
add_action('woocommerce_after_order_notes', 'wp_dudecom_custom_order_notes_field');

function wp_dudecom_custom_order_notes_field($checkout) {
    echo '<div id="wp_dudecom_custom_order_notes_field"><h2>' . __('Additional Order Notes') . '</h2>';

    woocommerce_form_field('wp_dudecom_order_notes', array(
        'type'        => 'textarea',
        'class'       => array('wp-dudecom-order-notes form-row-wide'),
        'label'       => __('Order Notes'),
        'placeholder' => __('Enter any additional notes for your order here.'),
    ), $checkout->get_value('wp_dudecom_order_notes'));

    echo '</div>';
}

// Validate the custom order notes field
add_action('woocommerce_checkout_process', 'wp_dudecom_validate_order_notes_field');

function wp_dudecom_validate_order_notes_field() {
    if (!$_POST['wp_dudecom_order_notes']) {
        wc_add_notice(__('Please enter order notes.'), 'error');
    }
}

// Save the custom order notes field data
add_action('woocommerce_checkout_update_order_meta', 'wp_dudecom_save_order_notes_field');

function wp_dudecom_save_order_notes_field($order_id) {
    if (!empty($_POST['wp_dudecom_order_notes'])) {
        update_post_meta($order_id, '_wp_dudecom_order_notes', sanitize_textarea_field($_POST['wp_dudecom_order_notes']));
    }
}

// Display the custom order notes field data in the admin order edit page
add_action('woocommerce_admin_order_data_after_order_details', 'wp_dudecom_display_order_notes_in_admin');

function wp_dudecom_display_order_notes_in_admin($order) {
    $order_notes = get_post_meta($order->get_id(), '_wp_dudecom_order_notes', true);
    if ($order_notes) {
        echo '<p><strong>' . __('Order Notes') . ':</strong> ' . esc_html($order_notes) . '</p>';
    }
}
?>

Instructions

To implement the custom order notes field in your WooCommerce checkout, follow these steps:

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Access your WordPress dashboard.
  2. Navigate to Appearance > Theme Editor if you are adding the code to functions.php. Alternatively, use a code editor to open your custom plugin file.
  3. Copy the provided code snippet.
  4. Paste the code at the end of the functions.php file or your custom plugin file.
  5. Save the changes.
  6. Test the checkout process on your WooCommerce store to ensure the custom order notes field appears and functions correctly.

By following these steps, you can easily add a custom order notes field to your WooCommerce checkout page, allowing customers to provide additional information with their orders.

If you need further assistance or want to explore more advanced functionalities, consider reaching out to wp-dude.com for expert help.