I am building a plugin that will cycle through all customer reviews and show them on any page.
I know how to get the user's display name from the review/comment, but how to get his billing location also?
I think what I need is a way to get the WooCommerce user data from the review/comment ID - if this is possible??
Providing they've submitted an order and you've got their user ID, you should be able to do something like:
$city = get_user_meta( $user_id, 'shipping_city', true );
or...
$city = get_user_meta( $user_id, 'billing_city', true );
It might be worth having a look at your user meta table to see what's in there.
Edit
Just re-read the question and maybe you don't have the user ID yet. You should be able to get this from the $wpdb->comments table.
Related
I have code that is running in the "woocommerce_created_customer" hook.
I have created custom fields in my checkout and some of them need to be copied to the user profile.
I have different checkout pages using WooFunnels plugin, some ask for the custom fields and some don't but I need to make sure that all the custom fields have meta_data.
So, I have the following code in the hook:
if (empty($_REQUEST["condition_other_info"])){
update_user_meta( $user_id, "condition_other_info", "");
}else{
update_user_meta( $user_id, "condition_other_info", $_REQUEST["condition_other_info"]);
}
if (empty($_REQUEST["date_of_birth"]) ){
$dob_update_return = update_user_meta( $user_id, "date_of_birth", "2000-01-01");
error_log("return of update_user_meta of 'date_of_birth':". $dob_update_return);
}else{
update_user_meta( $user_id, "date_of_birth", $_REQUEST["date_of_birth"]);
}
No, here is the funny part. The first one works fine and saves to the database (along with about 11 other similar "update_user_meta" commands, but the second one (along with 3 others) does not. The 4 that do not work are in the middle of the good 11 ones.
No errors are thrown.
Not only that, but when I check my debug log, $dob_update_return shows me a meta_id value (that is in order with the orders) but when I go to the Database after the user is created, these 4 meta records are gone, while the others are there... Those 4 numbered keys are missing.
Am I the only one that thinks this is quite strange?
I feel like there might be other code that it is deleting these values after I add them, but I can not figure out how to find that code...
This is my first post here although I have learned much from visiting previously.
I need to sync data from the wp_users base to a second table. That part I have accomplished successfuly by placing the following code in my functions file:
function add_to_YjT_bookly_customers($user_id) {
global $wpdb;
$user_info = get_userdata($user_id );
$wpdb->insert( 'YjT_bookly_customers', array('full_name' => $user_info->user_login,'wp_user_id' => $user_info->ID,'email' =>$user_info->user_email,'first_name' => $user_info->first_name), array('%s', '%s', '%s', '%s') );
}
add_action( 'user_register', 'add_to_YjT_bookly_customers');
But I also want to sync data from the usermeta db, specifically the first_name and last_name. You can see from the code above that the target db is Yjt_bookly_customers and that I tried (hoped) that the first_name woudl also sync. And while the user_login, id, and pw sync'ed, the first_name did not. I guess the metadata is not covered by the $user_id variable. So my question is what code would I need to add to also sync the first_name, last_name fields from the usermeta table to successfully sync those as well at registration submission.
Finally, forgive me if my question is not technically perfect. I am new, but learning. Thanks so much for any help.
Pradhan
I wamt to make an auto fill field with gravity form that fill the name of the user and their roles automatically by inserting meta ID, for the name i found the ID 'display_name' but I can;t figure it out what is the roles meta ID, thank you.
It is not so straightforward. user_meta table in wordpress database contains field wp_capabilities, in which is stored an object of capabilities. It is better to use WordPress funcions to extract user roles:
$id = ... // ID of the user
$caps = get_userdata(1)->wp_capabilities; // object of capabilites looking like {administrator: true}
$roles = array_keys($caps); // now we have list of capabilies in array
I have a custom post with 2 fields, the first field being the user, and the second one being a table, and whenever a user is logged in, I must display all of the posts that have the user in the 1st field, this is what I found:
In wp_user the ID of the user I'll be using to test it will have: user_id = 3
In wp_postmeta, I have a row with these values:
-post_id=92
-meta_value=3
And in wp_posts, in the ID = 92, I have the specific post name I was looking for.
I'm pretty confused on how to make this code dynamically, so any user will be able to see their own posts. And BTW, the post_author is the admin, so I can't use that field
Basiclly, I must retrieve the post_id from wp_postmeta by only using the the meta_value which I've seen a couple of guides but can't really understand how to do it. I know I must use wp_get_current_user(). Please someone explain me with detail how to achieve this.
Thanks
In your functions.php you need to place this hook , this hook is fired whenever a post is added or updated you need to use this for saving post id in the post meta table.
function enter_post_id( $post_id ) {
wp_get_current_user() // Get current user id with this
update_post_meta()($post_id, $meta_key, $meta_value, $unique)
// add a meta key say 'user_id' and place the value of user_id as meta value
}
add_action( 'save_post', 'enter_post_id' );
Now You have the match completed , you just need to query through the post meta table and get the post id for the user , and then from wp_posts show only the post's with matched ID.
Hope this helps . Feel free to ask if you have any confusion.
No problem :) Use this function.
function get_post_id(){
global $wpdb;
$result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key='user_id' AND meta_value= $user_id ", OBJECT );
// Where $user_id is the id you get from wp_current_user()
}
Now var dump this $result to see the post_id
loop through this to get values for each post id
I am creating a simple db for School Students and want to achieve it in wordpress using any method that is available now.
Every student details like first name, class etc will be saved in database along with ID as primary key.
In PHP, there used to be ONE file like details.php etc and based on QUery passed using GET or POST method, it will display the details. Can we do the same in Wordpress using a SINGLE Page or Post;
Instead of creating seperate Page / Post for every student, i had created PHP Queries in Page / Post using WP Plugin which will display the details of student based on querying ID.
But i am not sure how to make it generalized so that on entering http://mywpsite.com/studentpageorpost/?id=10 i should get the details of student with ID 10; and on entering http://mywpsite.com/studentpageorpost/?id=11, i should get the details of ID 11;
Can anyone please help on this.
If I understand well the code would work like this:
1 Take the id number from url and stored
1.1 htmlspecialchars() is for converting html tags so you can't be hacked by php injection
$id = htmlspecialchars( $_GET["id"] );
2 Here we have stored the user info in a object with the wordpress function get_userdata();
2.1 If ID not found return false.
$user_data = get_userdata( $id );
Accessing Usermeta Data
$user_data = get_userdata( $id );
echo $user_data->last_name . ", " . $user_info->first_name . "\n";
Results in:
Doe, John
If you want to know how to access more user info use print_r($user_data); and will output all the info that the user has.
Here are some of the useful values in the wp_users and wp_usermeta tables you can access with this function for use in your theme or plugin:
users
ID
user_login
user_pass
user_nicename
user_email
user_url
user_registered
display_name
user_meta
user_firstname
user_lastname
nickname
description
wp_capabilities (array)
admin_color (Theme of your admin page. Default is fresh.)
closedpostboxes_page
primary_blog
rich_editing
source_domain
Edit: I think the above system is the easiest still as MarkBlythe sed, no need for individual account, you can use custom post type plugin and custom fields. You could add the students very fast in a loop and array with this function wp_create_user( $username, $password, $email );