skip to Main Content

I’ve been googling and searching here for a simple way to add an empty field box to the Edit Orders page. We will use this to put in a reference for the shipment from our courier.

We would add it to the order notes, but we want it to be searchable, and also want to add it as a column in the Orders Admin page (I have Admin Columns plugin that I think can do that bit, I just need to add this field to start).

Hope someone can help, thanks!

EDIT:

I found this question which seems to be similar, but more complicated, than what I am looking for and I cant figure out how to simplify it. Add a custom meta box on order edit pages and display it on the customer order pages

I don’t need anything that will display on the front end to customers like this. Just a simple empty box that displays on every order-edit page (perhaps below the order notes) that can be searched. I will then display it in a column on the order admin page too.

2

Answers


  1. Chosen as BEST ANSWER

    Managed to get an answer, this is working great!

    //from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
    
    // For displaying in columns.
    
    add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
    function set_custom_edit_shop_order_columns($columns) {
        $columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
        return $columns;
    }
    
    // Add the data to the custom columns for the order post type:
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
    function custom_shop_order_column( $column, $post_id ) {
        switch ( $column ) {
    
            case 'custom_column' :
                echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
                break;
    
        }
    }
    
    // For display and saving in order details page.
    add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
    function add_shop_order_meta_box() {
    
        add_meta_box(
            'custom_column',
            __( 'Custom Column', 'your_text_domain' ),
            'shop_order_display_callback',
            'shop_order'
        );
    
    }
    
    // For displaying.
    function shop_order_display_callback( $post ) {
    
        $value = get_post_meta( $post->ID, 'custom_column', true );
    
        echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
    }
    
    // For saving.
    function save_shop_order_meta_box_data( $post_id ) {
    
        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
    
        // Check the user's permissions.
        if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
                return;
            }
        }
    
        // Make sure that it is set.
        if ( ! isset( $_POST['custom_column'] ) ) {
            return;
        }
    
        // Sanitize user input.
        $my_data = sanitize_text_field( $_POST['custom_column'] );
    
        // Update the meta field in the database.
        update_post_meta( $post_id, 'custom_column', $my_data );
    }
    
    add_action( 'save_post', 'save_shop_order_meta_box_data' );
    
    

  2. enter image description here

    Use the default WooCommerce custom field in the order edit page bottom section.

    enable custom field to get this option

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search