Send billing company with admin notification email - wordpress

I am trying to attach the billing company to the admin email. I am using this script
add_action( 'user_register', array( $this, 'user_register' ) );
function user_register( $user_id ) {
// using this function to send the email
$this->send_notification( 'admin-user', $user_id );
}
public function send_notification( $setting, $id ) {
$user_company = get_user_meta($id, 'billing_company');
wp_mail( $email, $subj, $msg.$user_company[0], $headers );
}
the issue is that get_user_meta returns empty because according to wordpress doc when you use 'user_register' action Not all user meta data has been stored. So basically when user register the usermeta table is still empty because I tried to put an existing user id and it worked fine.
https://codex.wordpress.org/Plugin_API/Action_Reference/user_register.
can anyone suggest a way to send the company name in the admin notification email?

If you are using custom registration form, you can send the email before updating the database.
add_action( 'user_register', array( $this, 'user_register' ) );
function user_register( $user_id ) {
$billing_company = $_POST['billing_company'];
wp_mail( $email, $subj, $billing_company, $headers );
}
Alternatively, you can delay the execution of the function with sleep:
add_action( 'user_register', array( $this, 'user_register' ) );
function user_register( $user_id ) {
sleep(5);
$this->send_notification( 'admin-user', $user_id );
}

Related

How to link WooCommerce guest orders to customer account after registration

Our scenario is:
The guest user enters the site and places one or more orders without the need to register. And after a while, he decides to register on the site.
Now how to link guest orders to customer account after registeration?
I use the following code, but this code only works for users who have already registered but did not log in at the time of purchase. Any advice?
//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
function action_woocommerce_new_order( $order_id ) {
$order = new WC_Order($order_id);
$user = $order->get_user();
if( !$user ){
//guest order
$userdata = get_user_by( 'email', $order->get_billing_email() );
if(isset( $userdata->ID )){
//registered
update_post_meta($order_id, '_customer_user', $userdata->ID );
}else{
//Guest
}
}
}
You can use the woocommerce_created_customer action hook and the wc_update_new_customer_past_orders() function
So you get:
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
// Link past orders to this newly created customer
wc_update_new_customer_past_orders( $customer_id );
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );

Cancel all user orders on subscription cancelled/expired

I want to develop a setup/code in which all the orders made by the user gets cancelled when the his subscription gets cancelled or expired. I am using Wordpress, Woocommerce and Woocommerce Subscriptions plugin.
I am trying to do by using something like this:
function my_cancelled_subscription( $user_id, $subscription_key ) {
//what code should I use here????
}
add_action( 'woocommerce_subscription_status_cancelled', 'my_cancelled_subscription', 10, 2 );
Please help mee.....
I figured it out..........
function my_cancelled_subscription( $subscription ) {
$order_id = $subscription->get_parent_id();
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
$order->update_status( 'cancelled' );
$args = array(
'customer_id' => $user_id,
'status' => 'wc-completed',
);
$arrorders = wc_get_orders( $args );
foreach ($arrorders as $ord) {
$ord->update_status( 'cancelled' );
}
}
add_action( 'woocommerce_subscription_status_cancelled', 'my_cancelled_subscription' );

Create WP user from custom post type with action hook

I have a custom post type that I would like to use to create a WP user with an action hook. Once a user has completed the form the hook should enter the basic information to begin registering a wp user i.e. username, password email.
This is the function I have so far but I don't know why it is not working.
function agency_add_tenant( $post_id) {
$username = get_post_meta ( $post_id, 'username', true );
$password = get_post_meta ( $post_id, 'password', true );
$email_address = get_post_meta ( $post_id, 'email', true );
$user_id = function wp_create_user( $username, $password, $email_address );
// Email the user
wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
}

can i call function in my wordpress plugin when sending publish post in every plugin or user?

i want to call my function when user or admin or another plugin sending publish post.
any hook exist for this ?
i try some hook like publish_post.
but this work just if i put my code in that plugin which sending post.
i want my plugin Independent and when sending post in any plugin or admin or every where .... , my function in my plugin do something.
this is for SEO reason and its hard to say why i want do this.
some code i write
function post_published_notification($post) {
$ID= $post->ID;
$my_post = array(
'ID' => $ID,
'post_title' => "anything"
);
wp_update_post( $my_post );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
There's are hook called save_post - do_action( 'save_post', $post_ID, $post, $update );. You could check if $post->post_status is set to published and maybe keep your own record of whether it's had the published status before.
Example on how to use it:
function post_published_notification( $post_ID, $post, $update ) {
if ( $post->post_status == 'publish' && ! get_post_meta( $post_ID, 'published_notification_set', true ) ) {
update_post_meta( $post_ID, 'published_notification_set', 1 );
}
}
add_action( 'save_post', 'post_published_notification', 10, 3 );

Filter the Buddypress bp_core_signup_user

I want to send out a e-mail to the site admin that a new Buddypress user has signed up. That is what the first function is for. It works great. The second function is to append a few xprofile custom fields into the e-mail. Sadly the filter in the 2e function is not working. How can I make it work? Or how can I combine them so that they work?
function my_pending( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
// Send the email notification.
wp_mail( 'some#some.com', $user_login . ' bla bla', 'bla bla' );
}
add_action( 'bp_core_signup_user', 'my_pending', 10, 5 );
And the second function:
function custom_activation_email_body( $message, $user_id, $key ) {
$field1 = xprofile_get_field_data( '11', $user_id );
$field2 = xprofile_get_field_data( '12', $user_id );
$field3 = xprofile_get_field_data( '4', $user_id );
$message .= sprintf( __( "Username: %s Email: %s Membership Type: %s", 'lang' ), $field1, $field2, $field3 );
return $message;
}
add_filter('bp_core_signup_user', 'custom_activation_email_body', 10, 3);
If you use a field id, don't use it as a string, so
xprofile_get_field_data( '11', $user_id );
should be
xprofile_get_field_data( 11, $user_id );
And I think your filter is applied to the email sent to the new user.
So move your xprofile calls into the add_action function.
Review wp_mail to see how to setup the fields.

Resources