solidsn2004
solidsn2004

Reputation: 315

How to add custom field and display it in admin-new-order.php file in woocommerce?

I am working in Wordpress with WooCommerce and my client wanted to display a new field under "My Account" page of his customers called "Certification Type".

I have uploaded through a plugin a CSV file with 4000 of his customers and within this plugin there were custom fields (including the "Certification Type") that were added to the database.

My client wanted to display this field within the email he receives every time some of his clients orders something. So basically I have created a child theme and added the admin-new-order.php file.

Inside this file I have added the following code:

<?php
    global $current_user;
    get_currentuserinfo();
    echo 'Certification Type: ' . $current_user->Certification . "\n";
?>

When I tested the code using my admin login, I receive the email and the code works fine and shows me the value that I have in the Certification Type field. When my client tried it he receives the Certification Type: without showing what is in the field of the client who made the order.

Can someone please help me resolve this issue? I am not really good with php so sorry in advance if I am doing something blatantly wrong.

Update: I have recently figured out that my client's server is running Windows NT. Does this have to do anything with the issue? I have also installed a plugin to clear the cache but I am still getting the same result. Can someone please point me to the right direction here? What am I missing?

Upvotes: 2

Views: 1358

Answers (2)

Omer Farooq
Omer Farooq

Reputation: 4084

Well i think a new user field should have been added to wp_usermeta table instead of the wp_users table. The way you are looking with get_currentuserinfo(); this means that it is added to the users table. But if it was added to the usermeta table then you should have done this.

<?php echo get_user_meta($user_id, 'Certification', true);  ?>

So first of all you have to confirm which table stores the certification field.

1) is it in the wp_users table or in wp_usermeta table. For this you will have to log into your phpmyadmin or whatever database software you are using, then goto both of these tables and search for a random user for example search for user_id = 57 and see whether the Certification field is inside the users table or is it inside the usermeta table.

Or use a sql query like this to see all the user meta results for a random user id (make sure your db prefix is wp_ or replace it with your db table name)

global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_usermeta WHERE user_id = 57', OBJECT );
var_dump($results); die();

This is just for debuging and will dump all the usermeta of the user_id 57 in your browser. So you can then use chrome search to search for "Certificate". If you do find it here, then it means that the data is stored in usermeta and you just simply need to run this code to get the certificate. Also there is a difference between "Certificate" and "certificate", so make sure you are asking for the one in the database.

EDIT: Here is the complete code to use

<?php
    global $current_user;
    get_currentuserinfo();
    echo 'Certification Type: ' . get_user_meta($current_user->ID, 'Certification', true) . "\n";
?>

Upvotes: 3

Andre Dixon
Andre Dixon

Reputation: 360

Being that this is not one of the default fields, I personally would write an SQL script which would insert the data for this field for each customer. I am assuming that the date for this custom field is stored in user_meta table so that would be the first play I investigate.

Upvotes: 1

Related Questions