@ryan.murray
To add WooCommerce custom order data, you can use the following steps:
1 2 3 4 5 6 7 8 9 10 11 |
add_action( 'woocommerce_process_shop_order_meta', 'add_custom_order_data' ); function add_custom_order_data( $order_id ) { // Get the order object $order = new WC_Order( $order_id ); // Add custom order data $custom_field_value = get_post_meta( $order_id, '_custom_field', true ); if ( ! empty( $custom_field_value ) ) { $order->add_order_note( 'Custom field: ' . $custom_field_value ); } } |
1 2 3 4 5 |
add_action( 'woocommerce_checkout_create_order', 'save_custom_order_data' ); function save_custom_order_data( $order ) { $custom_field_value = sanitize_text_field( $_POST['custom_field'] ); $order->update_meta_data( '_custom_field', $custom_field_value ); } |
1 2 3 4 5 6 7 |
add_action( 'woocommerce_order_details_after_order_table', 'display_custom_order_data' ); function display_custom_order_data( $order ) { $custom_field_value = get_post_meta( $order->get_id(), '_custom_field', true ); if ( ! empty( $custom_field_value ) ) { echo '<p><strong>Custom Field:</strong> ' . $custom_field_value . '</p>'; } } |
By following these steps, you can easily add custom order data to your WooCommerce orders.