I am trying to figure out how to add user information and specific meta data to a separate table through PHP. I would prefer to do this when a user posted to the wp_users
and wp_usermeta
table. I am using the WooCommerce Auction plugin and the Salient theme.
Also if I made a PHP file that ran a query to pull the data and then insert, where is a good place I can call that function to ensure the table is up to date?
This is the function I've created.
add_action('user_register', 'save_to_donors',10,1);
function save_to_donors( $user_id ) {
$single = true;
$user_email = get_userdata($user_id);
$fn= get_user_meta($user_id, 'first_ name', $single );
$ln= get_user_meta($user_id, 'last_name', $single );
$em= $user_email->user_email;
$ph= get_user_meta($user_id, 'billing_phone', $single );
$ad= get_user_meta($user_id, 'billing_address_1', $single );
$ci= get_user_meta($user_id, 'billing_city', $single );
$st= get_user_meta($user_id, 'billing_state', $single );
$zi= get_user_meta($user_id, 'billing_postcode',$single );
$do= get_user_meta($user_id, '_money_spent', $single );
$sql = "INSERT INTO donors (user_id,first_name,last_name,email," .
"phone,address,city,state,zip,amount_donated)".
"VALUES('$user_id','$fn','$ln','$em','$ph','$ad','$ci','$st','$zi','$do')";
$result = mysqli_query($wpdb, $sql) or die('Write Error!');
}
I am trying to figure out how to add user information and specific meta data to a separate table through PHP. I would prefer to do this when a user posted to the wp_users
and wp_usermeta
table. I am using the WooCommerce Auction plugin and the Salient theme.
Also if I made a PHP file that ran a query to pull the data and then insert, where is a good place I can call that function to ensure the table is up to date?
This is the function I've created.
add_action('user_register', 'save_to_donors',10,1);
function save_to_donors( $user_id ) {
$single = true;
$user_email = get_userdata($user_id);
$fn= get_user_meta($user_id, 'first_ name', $single );
$ln= get_user_meta($user_id, 'last_name', $single );
$em= $user_email->user_email;
$ph= get_user_meta($user_id, 'billing_phone', $single );
$ad= get_user_meta($user_id, 'billing_address_1', $single );
$ci= get_user_meta($user_id, 'billing_city', $single );
$st= get_user_meta($user_id, 'billing_state', $single );
$zi= get_user_meta($user_id, 'billing_postcode',$single );
$do= get_user_meta($user_id, '_money_spent', $single );
$sql = "INSERT INTO donors (user_id,first_name,last_name,email," .
"phone,address,city,state,zip,amount_donated)".
"VALUES('$user_id','$fn','$ln','$em','$ph','$ad','$ci','$st','$zi','$do')";
$result = mysqli_query($wpdb, $sql) or die('Write Error!');
}
Share
Improve this question
edited Jul 21, 2016 at 6:10
Tim Malone
4,8045 gold badges29 silver badges41 bronze badges
asked Jul 20, 2016 at 0:41
dozerdozer
131 silver badge4 bronze badges
3
- 1 This question might be helpful: wordpress.stackexchange/questions/63600/… – czerspalace Commented Jul 20, 2016 at 0:54
- When in the process are you trying to add data to the donors table? Why not just add in the user id and the donation amount? With the user id you can fetch the required data from the user and user meta tables – stoi2m1 Commented Jul 21, 2016 at 1:39
- I am trying to add data after the user registration and then updating the table when the user donates. – dozer Commented Jul 21, 2016 at 15:55
3 Answers
Reset to default 1Looks to me you should be seeking a woocommerce purchase completion hook. This would be when you could add that a donation has been made and at what amount, then you could grab any user information, amounted donated and other info you need and save it to your donor table.
use this:
add_action('woocommerce_order_status_completed', 'save_to_donors',10,1);
instead of this:
add_action('user_register', 'save_to_donors',10,1);
also change $user_id
to $order_id
as the parameter being passed into your function.
You will need to make some changes to your function since now $order_id
will be passed into your function instead of $user_id
. You can use some of the following code to get the $order
object and $user_id
I also found some other code that will get you info from the order vs from the users meta, not sure if they are going to be different.
$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$billing_address = $order->get_billing_address();
$billing_address_html = $order->get_formatted_billing_address(); // for printing or displaying on web page
$shipping_address = $order->get_shipping_address();
$shipping_address_html = $order->get_formatted_shipping_address(); // for printing or displaying on web page
Info on the use of this hook woocommerce_order_status_completed
Info on how to get user id from order id
This action hook allows you to access data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.
Not all user meta data has been stored in the database when this action is triggered. For example, nickname is in the database but first_name and last_name. The password has already been encrypted when this action is triggered.
Typically, this hook is used for saving additional user meta passed by custom registration forms.
This example will save a first_name field passed by a custom registration field.
Also, keep in mind that validation of registration fields should not be performed within this hook! Validate using the registration_errors hook, instead (the user_register hook will not be called if registration_errors validation fails).
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
if ( isset( $_POST['first_name'] ) )
update_user_meta($user_id, 'first_name', $_POST['first_name']);
}
NOTE: This answer looks irrelevant to the OP's question after further analysis of the added/updated function in the question. Im leaving this answer in place and creating a new answer for historical use, since part of the question seems applicable.
You could use something like the following to create user meta when users are added to the user table. Put this in the functions.php file of your theme.
function myplugin_registration_save( $user_id ) {
if ( isset( $_POST['first_name'] ) )
update_user_meta(
$user_id,
'your_key', // name of key
$your_value // value of your key
);
}
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
To retrieve this data for later use use the function:
get_user_meta($user_id, 'your_key', $single);
More info on user_register hook More info on update_user_meta() More info on get_user_meta()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205618a4616579.html
评论列表(0条)