teckut.com

How to Add Metadata to Orders in WordPress

Adding custom metadata to orders in WordPress/WooCommerce can enhance functionality and allow you to store additional information specific to your business needs. In this blog, we’ll walk through how to add metadata to orders using custom code, step by step.

Prerequisites

  • A WordPress site with WooCommerce installed and activated.
  • Basic understanding of PHP and WordPress development.

What is Metadata in WooCommerce Orders?

Metadata in WooCommerce refers to additional information stored alongside orders. This data can include custom fields, notes, or other information required for order processing. The metadata is stored in the wp_postmeta table in WordPress.

Steps to Add Metadata to Orders

1. Add Custom Fields to the Checkout Page

First, we need to add a custom field to the checkout page where users can input the data.

add_action( 'woocommerce_after_order_notes', 'add_custom_checkout_field' );

function add_custom_checkout_field( $checkout ) {
    echo '<div id="custom_checkout_field"><h3>' . __('Additional Information') . '</h3>';

    woocommerce_form_field( 'custom_field', array(
        'type'        => 'text',
        'class'       => array('custom-field-class form-row-wide'),
        'label'       => __('Custom Field'),
        'placeholder' => __('Enter custom data here'),
        'required'    => true,
    ), $checkout->get_value( 'custom_field' ) );

    echo '</div>';
}

This code adds a new input field labeled “Custom Field” under the order notes section on the checkout page.

2. Validate the Custom Field Input

We need to validate the input to ensure it is filled out correctly before proceeding.

add_action( 'woocommerce_checkout_process', 'validate_custom_checkout_field' );

function validate_custom_checkout_field() {
    if ( ! $_POST['custom_field'] ) {
        wc_add_notice( __( 'Please fill in the custom field.' ), 'error' );
    }
}

This function checks if the custom field is empty and displays an error message if it is.

3. Save Custom Field Data to Order Metadata

Once the field is validated, the data can be saved as metadata for the order.

add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_field' );

function save_custom_checkout_field( $order_id ) {
    if ( isset( $_POST['custom_field'] ) ) {
        update_post_meta( $order_id, '_custom_field', sanitize_text_field( $_POST['custom_field'] ) );
    }
}

The update_post_meta function saves the custom field data to the order metadata with the key _custom_field.

4. Display Custom Metadata in the Admin Order Page

To make the metadata visible to admins, you can add it to the order edit page.

add_action( 'woocommerce_admin_order_data_after_order_details', 'display_custom_field_in_admin' );

function display_custom_field_in_admin( $order ) {
    $custom_field = get_post_meta( $order->get_id(), '_custom_field', true );
    if ( $custom_field ) {
        echo '<p><strong>' . __('Custom Field') . ':</strong> ' . esc_html( $custom_field ) . '</p>';
    }
}

This snippet retrieves the metadata from the database and displays it in the order details section in the WooCommerce admin.

5. Retrieve Metadata in Code

If you need to access this metadata in custom scripts or plugins, you can use the following code:

$order_id = 123; // Replace with your order ID
$custom_field = get_post_meta( $order_id, '_custom_field', true );

if ( $custom_field ) {
    echo 'Custom Field Value: ' . esc_html( $custom_field );
}

Final Thoughts

Adding custom metadata to WooCommerce orders can be incredibly useful for businesses that need to capture or display additional information. By following the steps above, you can implement this feature seamlessly.

Feel free to modify the code snippets to suit your specific requirements. If you have any questions, drop them in the comments below!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top