Gravity form | Get the sender email to fire a notification - wordpress

In order to send a post form submission notification with wp_mail(), I would like to get the sender email.
I have created an hidden field into my form where I can get the user logged email.
What is the function I can use to get the user logged email to send the notification ?
The hidden field ID into the form is 4.
I have created a variable :
$subscribers = rgar( $entry, '4' );
But it's not working...
My code to send the notification :
add_action( 'gform_after_submission', 'notification_mail', 10, 2 );
function notification_mail( $entry, $form ) {
$created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
foreach ( $created_posts as $post )
{
$post_id = $post['post_id'];
$subscribers = rgar( $entry, '4' );
$subject = 'Merci';
$message = "<p>Bonjour,</p>
<p>Merci d'avoir rempli notre formulaire</p>";
$headers = array('Content-Type: text/html; charset=UTF-8','From: SMP <noreply#smp.fr>');
$content_type = function() { return 'text/html'; };
add_filter( 'wp_mail_content_type', $content_type );
wp_mail( $subscribers, $subject, $message, $headers );
remove_filter( 'wp_mail_content_type', $content_type );
}
}
Thank you so much !
FX

Ok... I just wrote this function to get the user mail (my user is logged when he complets the form) :
$user = wp_get_current_user();
$subscriber = esc_html( $user->user_email );

Related

How I can get order details and checkout details on my email woocommerce?

I have to collect order details and checkout details on my email account on clicking the place an order button in woocommerce and redirect them to home page without paying any order fee. I'm using the elementor with WC.
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}
`
You can set a default payment gateway in woocommerce without payment(ex:- COD).
you can hook into thank-you page using hook woocommerce_thankyou. then get the order object and relevent inforamtion and send this information using wp_mail() to your email then redirect the user to homepage.
for order information check out this blog.
https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Use below code and if you need a customized email template.
UPDATE 1.
add_action('woocommerce_thankyou', 'zillion_order_mail_and_redirect', 10, 1);
function zillion_order_mail_and_redirect($order_id)
{
$order = wc_get_order($order_id);
$url = home_url();
$to = 'sendto#example.com';
$subject = 'Order Info';
$body = '';
$headers = array('Content-Type: text/html; charset=UTF-8');
$body .= $order->get_formatted_order_total();
wp_mail($to, $subject, $body, $headers);
wp_safe_redirect($url);
exit;
}
wp_mail() reference: https://developer.wordpress.org/reference/functions/wp_mail/
UPDATE 2:-
You can add your email using woocommerce_email_headers hook to get a copy of the email is being sent to the customer.
add_action('woocommerce_thankyou', 'zillion_order_mail_and_redirect', 10, 1);
function zillion_order_mail_and_redirect($order_id)
{
$url = home_url();
wp_safe_redirect($url);
exit;
}
function zillion_hook_headers( $headers, $id, $order ) {
// The order ID | Compatibility with WC version +3
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$your_email = '<example#gmail.com>';
$headers .= "To: Order Num $order_id $your_email";
return $headers;
}
add_filter( 'woocommerce_email_headers', 'zillion_hook_headers', 10, 3);

Send Email notifications when a user completes a successful order using a certain code l Woocommerce [duplicate]

How to send order notification to a business partner when a specific coupon is used?
I found a solution for the instance when the coupon is applied here :
Send an email notification when a specific coupon code is applied in WooCommerce
However, I need to find a solution for when after order is submitted, because the order is not always submitted after coupon is applied.
Each coupon will have its own email address.
First we add a setting field to admin Coupon pages, to set an email recipient for for a coupon:
// Add a custom field to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
function add_coupon_text_field() {
woocommerce_wp_text_input( array(
'id' => 'email_recipient',
'label' => __( 'Email recipient', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Send an email notification to a defined recipient' ),
'desc_tip' => true, // Or false
) );
}
// Save the custom field value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
function save_coupon_text_field( $post_id, $coupon ) {
if( isset( $_POST['email_recipient'] ) ) {
$coupon->update_meta_data( 'email_recipient', sanitize_text_field( $_POST['email_recipient'] ) );
$coupon->save();
}
}
Then email is sent for each applied coupon to a submitted order, if the email recipient has been set for the applied coupon.
Caution! choose only one of the following functions:
For woocommerce versions Up to 4.3 (new hook)
// For Woocommerce version 4.3+
add_action( 'woocommerce_checkout_order_created', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order ){
$used_coupons = $order->get_used_coupons();
if( ! empty($used_coupons) ){
foreach ( $used_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code ); // WC_Coupon Object
$recipient = $coupon->get_meta('email_recipient'); // get recipient
if( ! empty($recipient) ) {
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
}
}
Or for all WooCommerce versions (since version 3.0)
// For all Woocommerce versions (since 3.0)
add_action( 'woocommerce_checkout_update_order_meta', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order_id ){
$order = wc_get_order( $order_id );
$used_coupons = $order->get_used_coupons();
if( ! empty($used_coupons) ){
foreach ( $used_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code ); // WC_Coupon Object
$recipient = $coupon->get_meta('email_recipient'); // get recipient
if( ! empty($recipient) ) {
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

WordPress additonal check while login

When a user register in my site I have set the user_activation_key column value from wp_users table like that:
$code = sha1( $user_id . time() );
global $wpdb;
$wpdb->update(
$wpdb->prefix.'users', //table name
array( 'user_activation_key' => $code ),
array( 'ID' => $user_id ),
array( '%s' ),
array( '%d' )
);
It's because I want to make activation system by sending an email with clickable link:
$activation_link = add_query_arg(
array(
'key' => $code,
'user' => $user_id
), get_permalink( 44 )
);
$message = "<div style='padding : 20px; border : 1px solid #ddd; color : #000;'>Hello $surname, <br/><br/>Please confirm your email addresss . Click this link to confirm : <a href='$activation_link'>Confirm Now</a><br/><br/></div>";
$to = $email;
$subject = 'Confirm your registration process"';
$body = $message;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Now, the column user_activation_key has the hash code and user_status column value is 0
Now the actual question:
When user go to www.site.com/wp-admin that means login page I want to show an error message if the user_status column value is 0.
I don't' have any idea which hook or how can I cehck this while user login?
Use the admin_notices hook to display messages in the backend, the code below is similar to what your after:
add_action('admin_notices', 'account_activation_check');
function account_activation_check() {
global $wpdb;
// setup vars //
$currentID = get_current_user_id();
$user = get_user_by( 'ID', $currentID );
$userStatus = $user->user_status;
// check if user status is 0 //
if($userStatus == 0) {
echo '<div class="error"><p>Your email has not been verified!</p></div>';
}
}
I have solved the issues by using the wp_authenticate_user hook. Here is the code:
add_filter( 'wp_authenticate_user', 'shibbir_authenticate_user', 10, 2 );
function shibbir_authenticate_user( $user ) {
if ( $user->data->user_status == 0 ) {
return new WP_Error( 'error', __( 'Your account is not activate, Please contact site admininstrator.' , 'shibbir' ) );
}
return $user;
}

Send email notification when new post from post type is created

I'm having a time trying to get this to work and I feel like I have tried everything from transition_post_status, to wp_insert_post and even draft_to_pending for my actions.
I'm trying to send an email to a user and the post author. The user is set via ACF user object. The problem I think, is that possibly the meta is not available or something because $author and $user are not objects so it's not finding them by the ID and grabbing their data.
Here is my function:
add_action( 'transition_post_status', 'send_volunteer_request_email', 10, 3 );
function send_volunteer_request_email( $new_status, $old_status, $post ){
if ( 'volunteers' !== get_post_type( $post ) )
return;
if( wp_is_post_autosave( $post ) ) return;
$user = get_field('user', $post);
$user_data = get_userdata( $user );
$the_event = get_field('event', $post);
$event = get_post($the_event);
$eventLink = get_permalink($event);
$author = get_userdata( $event->post_author );
if(get_post_status($post) == 'pending'){
//Notify event author
$headers = array('Content-Type: text/html; charset=UTF-8');
$subject = 'You have a new volunteer';
$body = '<p>A member has volunteered to meet your need. Click here to view and accept or decline!</p>';
$body .= '<p>Thank you for being a valuable member<br>https://www.goole.com<br>910-555-1234</p>';
wp_mail( $author->user_email, $subject, $body, $headers );
}
}
Here is an example of one of the errors:
PHP Notice: Trying to get property 'user_email' of non-object
And finally, you can see where these are set
Try changing your code as below:
$user = get_field('user', $post->ID);
$user_data = get_userdata( $user );
$the_event = get_field('event', $post->ID);
$event = get_post($the_event);
The reason that you are getting the error is because you were passing post object to get_field function instead of post id.

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