Extra fields

Hi,
I would like to add an extra field/column to:
1. My suppliers admin section (it is to store text).
2. Orders admin section (it is to store staff initials against each order)
2. Add a text field within each order to add details about the order, just for admin purposes

Is this possible please, thank you. I will open support access, thank you

  • Adam Czajczyk

    Hi Chez

    I hope you're well today and thank you for your questions!

    1. My suppliers admin section (it is to store text).

    Your "Suppliers" section is coming from the "Suppliers Manager for WooCommerce" plugin. I'm not familiar with this plugin's code and adding the such field would require either addtional code snippet (if the plugin provides relevant hooks) or even changing plugin's code. I'd suggest getting in touch with this plugin's developers and asking them as they would most likely be able to give you a straight answer "out of the box" and, if it's doable, suggest solution.

    2. Orders admin section (it is to store staff initials against each order)
    2. Add a text field within each order to add details about the order, just for admin purposes

    There's already a tool for it available as an official WooCommerce extension. You'll find it here:

    https://woocommerce.com/products/admin-custom-order-fields/

    Alternatively, if it's just a simple field, you could add this code to your child-theme functions.php file (or add it as mu plugin):

    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_admin_order_field', 10, 1 );
    
    function my_custom_admin_order_field($order){
        echo '<p><strong>'.__('Field Name','theme_domain').':</strong> ' . get_post_meta( $order->get_id(), 'my_field', true ) . '</p>';
    }
    
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
    function custom_shop_order_column($columns)
    {
        $reordered_columns = array();
    
        // Inserting columns to a specific location
        foreach( $columns as $key => $column){
            $reordered_columns[$key] = $column;
            if( $key ==  'order_status' ){
                // Inserting after "Status" column
                $reordered_columns['my-column1'] = __( 'My Field','theme_domain');
            }
        }
        return $reordered_columns;
    }
    
    // Adding custom fields meta data for each new column (example)
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
    function custom_orders_list_column_content( $column, $post_id )
    {
        switch ( $column )
        {
            case 'my-column1' :
                // Get custom post meta data
                $my_var_one = get_post_meta( $post_id, 'my_field', true );
                if(!empty($my_var_one))
                    echo $my_var_one;
    
                // Testing (to be removed) - Empty value case
                else
                    echo '<small>(<em>no value</em>)</small>';
    
                break;
        }
    }

    [reference: https://stackoverflow.com/a/36453587]

    If you go with the "code way", please note:

    1) You will have to set your "column name" in this line

    $reordered_columns['my-column1'] = __( 'My Field','theme_domain');

    Just replace "My Field" with your column line there

    2) you might want to set your custom field name; to do this, replace "my_field" with your custom field name in these lines:

    echo '<p><strong>'.__('Field Name','theme_domain').':</strong> ' . get_post_meta( $order->get_id(), 'my_field', true ) . '</p>';

    and

    $my_var_one = get_post_meta( $post_id, 'my_field', true );

    3) You'll probably want to set a field label to be displayed in Order page, so replace "Field name" here with your own text in this line:

    echo '<p><strong>'.__('Field Name','theme_domain').':</strong> ' . get_post_meta( $order->get_id(), 'my_field', true ) . '</p>';

    3) Finally, the field is not ediable directly so to add/update it use regular WP "Custom Fields" meta box (if you don't see it, enter any existing order and enable "Custom Fields" in the "Screen Options" tab).

    Best regards,
    Adam

  • Adam Czajczyk

    Hi Chez

    it shows up on the order list but says no value, not sure how to add value

    It's fetching value of a custom field so if the value is not set, it will show "no data". If you enter the order, you should also see the label for that field but with no data there - and not editable.

    Therefore you actually need to add data if you want to use it:

    - enter the "edit" screen for an order that you want to add the value to it
    - look if you see "Custom Fields" block there; if not, expand "Screen Options" and enable it
    - expand the drop-down list in "Add New Custom Field" and look for your field on the list then select it
    - if that field is not on the list, click "Enter new" right below that selection and put the name of your custom field there (in my code that field was called "my_field")
    - put some value (what you want to be displayed on the list) in a "value" field
    - update order.

    This would add value to the custom field and that value will then be automatically fetched and displayed on order list.

    Kind regards,
    Adam

  • Chez

    Hi this is what I put in function

    /*extra fields*/

    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_admin_order_field', 10, 1 );

    function my_custom_admin_order_field($order){
    echo '<p>'.__('Initials','theme_domain').': ' . get_post_meta( $order->get_id(), 'initials', true ) . '</p>';
    }

    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
    function custom_shop_order_column($columns)
    {
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
    $reordered_columns[$key] = $column;
    if( $key == 'order_status' ){
    // Inserting after "Status" column
    $reordered_columns['my-column1'] = __( 'Initials','theme_domain');
    }
    }
    return $reordered_columns;
    }

    // Adding custom fields meta data for each new column (example)
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
    function custom_orders_list_column_content( $column, $post_id )
    {
    switch ( $column )
    {
    case 'my-column1' :
    // Get custom post meta data
    $my_var_one = get_post_meta( $post_id, 'initials', true );
    if(!empty($my_var_one))
    echo $my_var_one;

    // Testing (to be removed) - Empty value case
    else
    echo '<small>(no value)</small>';

    break;
    }
    }

    ________________________________________________________________________________________

    Cant see anything in screen options from within the order, I have probably done something wrong

  • Adam Czajczyk

    Hi Chez

    I don't think you're doing anything wrong. I suppose it's rather me not explaining the process clearly enough :slight_smile:

    That being said, I took the code from your post above, put it on my own test site and recorded a short video for you. Take a look please and I think with it my previous explanation should make a bit more sense now. Don't turn the volume up, there is no sound :slight_smile:

    https://monosnap.com/file/ZOiWwbrvGZwH0lLXrr64PzPAs2WGBa

    I hope this helps but in case you had any follow-up questions, please ask.

    Best regards,
    Adam

  • Adam Czajczyk

    Hi Chez

    Thanks for response!

    I didn't notice that before but you got Advanced Custom Fields plugin installed on site. It's hiding default WP custom fields.

    Try adding following additional code to your theme's "functions.php" file:

    add_filter('acf/settings/remove_wp_meta_box', '__return_false');

    That should "unlock" them so you should be able to follow the "video guide" with it.

    Best regards,
    Adam

  • Adam Czajczyk

    HI Chez

    So basically you need a dedicated meta-box there I guess. Try adding this code in addition to what you already added. It would add a nice standard dedicated "box"/field there.

    ################  META BOX ######################
    
    // add metabox
    function woo_initials_meta_box() {
    
        add_meta_box(
            'woo_initials',
            __( 'Your initials', '' ),
            'woo_initials_meta_box_callback',
            'shop_order'
        );
    }
    
    add_action( 'add_meta_boxes', 'woo_initials_meta_box' );
    
    // get value into it
    function woo_initials_meta_box_callback( $post ) {
    
        // Add a nonce field so we can check for it later.
        wp_nonce_field( 'woo_initials_nonce', 'woo_initials_nonce' );
    
        $value = get_post_meta( $post->ID, 'initials', true );
    
        echo '<input type="text" style="width:100%" id="woo_initials" name="woo_initials" value="' . esc_attr( $value ) . '" />';
    }
    
    // save/update
    function save_woo_initials_meta_box_data( $post_id ) {
    
        // Check if our nonce is set.
        if ( ! isset( $_POST['woo_initials_nonce'] ) ) {
            return;
        }
    
        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $_POST['woo_initials_nonce'], 'woo_initials_nonce' ) ) {
            return;
        }
    
        // 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'] ) && 'page' == $_POST['post_type'] ) {
    
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return;
            }
    
        }
        else {
    
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return;
            }
        }
    
        /* OK, it's safe for us to save the data now. */
    
        // Make sure that it is set.
        if ( ! isset( $_POST['woo_initials'] ) ) {
            return;
        }
    
        // Sanitize user input.
        $my_data = sanitize_text_field( $_POST['woo_initials'] );
    
        // Update the meta field in the database.
        update_post_meta( $post_id, 'initials', $my_data );
    }
    
    add_action( 'save_post', 'save_woo_initials_meta_box_data' );

    This should work (I tested it on my end) but please note that we're going already a bit too far into the custom development area so I'm afraid that's pretty much as far as I can go with such customization. I hope that's fine.

    Best regards,
    Adam

  • Adam Czajczyk

    Hi Chez

    I'm glad to hear that it worked!

    Could you confirm have I reached my hours of your time for the month or do I ask for too much coding etc

    No, there's no such thing as "limit" on our assistance. It's just that there's we do not provide custom development. Take a look here, please:

    https://premium.wpmudev.org/docs/getting-started/getting-support/

    I am (and so are my colleagues) more than happy to assist you with anything WordPress related, including some basic/simple/essential "tweaks" that might require some code but custom solutions are out of the scope. Sometimes, we might go a bit further than usually (like in this case) mostly because the line between support/assistance and custom coding/development is rather thin.

    Bottom line is: I am glad I can/could help you and I'll be happy to continue doing that with other issues/questions. In this case, in my assessment, we just crossed that "line" between support assistance and a fully custom development area. I hope that makes some sense :slight_smile:

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.