WordPress send email on add_post_meta - php

I want to send a notification email to Author when a Custom Meta Field is added to their WordPress Post by add_post_meta or update_post_meta.
So far my code below is working successfully but it executes only when I use Save Post
function order_update_send_email( $post_id ) {
$email_sent = get_post_meta($post_id, 'email_sent', true);
$report = get_post_meta($post_id, 'report', true);
if ( $email_sent == 'Sent' ) {
return;
}
if ( $report ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = 'Your report for: ' . $post_title;
$message = "Your order is completed\n\n";
$message .= "Report for: " . $post_title . "\n\n Link:" . $post_url;
$message .= "\n\n" . $report;
wp_mail( 'test#email.me', $subject, $message );
update_post_meta($post_id, 'email_sent', 'Sent');
}
}
add_action( 'save_post', 'order_update_send_email' );
In above code the Action hook save_post is working fine but I can't get add_post_meta or update_post_meta to work.
Anyone please advice, thanks.

you can use below sample code for this.
add_action( 'added_post_meta', 'wp_afterpostmeta', 10, 4 );
add_action( 'updated_post_meta', 'wp_afterpostmeta', 10, 4 );
function wp_afterpostmeta( $meta_id, $post_id, $meta_key, $meta_value )
{
if ( 'wp_meta_key' == $meta_key ) {
wp_do_something( $post_id, $meta_value );
}
}

Related

Send email to users who finished submitting a product review in WooCommerce

I am using the following code which works fine.
function send_comment_email_notification( $comment_ID, $commentdata ) {
$comment = get_comment( $comment_id );
$postid = $comment->comment_post_ID;
$master_email = 'email#gmail.com';
var_dump($commentdata);
if( isset( $master_email ) && is_email( $master_email ) ) {
$message = 'New comment on ' . get_the_title( $postid ) . '';
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( $master_email, 'New Comment', $message );
}
}
add_action( 'comment_post', 'send_comment_email_notification', 11, 2 );
However, I would like to send an email to the user who just reviewed on a product page so I can offer them a coupon and a thank you for giving a review.
The problem is the $master_email variable, that is "hard coded". I need to capture the email entered by the user after he/she submitted the product review
Any advice?
To get the email entered by the user after he/she submitted the product review, you can use $commentdata['comment_author_email']
So you get:
function action_comment_post( $comment_ID, $comment_approved, $commentdata ) {
// Isset
if ( isset ( $commentdata['comment_author_email'] ) ) {
// Get author email
$author_email = $commentdata['comment_author_email'];
if ( is_email( $author_email ) ) {
// Post ID
$post_id = $commentdata['comment_post_ID'];
// Send e-mail
$to = $author_email;
$subject = 'The subject';
$body = sprintf( __(' Thank you for giving a review on %s', 'woocommerce' ), '' . get_the_title( $post_id ) . '' );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( $to, $subject, $body, $headers );
}
}
}
add_action( 'comment_post', 'action_comment_post', 10, 3 );

WooCommerce how to attach custom invoice to Email

with some help I manage to create a plugin to attach, on the finished order email, an invoice.
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
$allowed_statuses = array( 'customer_completed_order' );
if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
$pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
$pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name;
$attachments[] = $pdf_path;
}
return $attachments;
}
This code is to check on the order meta for "email_fatura" (translated "email_invoice"), and get the value of this field. This value takes the path root /Faturas/GestaoDespesas/ORDER123.pdf and attach the pdf to the email.
However, the problem is when there isn't a field "email_fatura", it stills attachs a file called "GestaoDespesas", that comes from /Faturas/**GestaoDespesas**/
For those who know PHP I assume that it is easy to fix this.
Thank you in advance for any kind of help.
I would first check whether the field is empty or not, if it is then just return:
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
$allowed_statuses = array( 'customer_completed_order' );
$pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
if ( empty($pdf_name) ){
return;
}
if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
$pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
$pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name;
$attachments[] = $pdf_path;
}
return $attachments;
}

Check if its a new custom post or update using save_post hook

I'm trying to send a mail using wp_mail function and hook it on save_post. How can I check it if it is a new post or it is just updating post? I tried the update parameter of save_post but always gave me the boolean true answer. I only need to send it if its a new post after published. Is it possible?
Here is my code:
add_action( 'save_post', 'dt_email_client_new', 13, 3 );
function dt_email_client_new( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if($post_type != 'dt_appointments'){
return;
}
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// Stop WP from clearing custom fields on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// Prevent quick edit from clearing custom fields
if (defined('DOING_AJAX') && DOING_AJAX)
return;
remove_action( 'save_post', 'dt_email_client_new', 13, 3 );//this will avoid duplicate emails
//how can I check if its new post or update?
$get_first_name = get_post_meta($post_id, 'first_name', true);
$get_last_name = get_post_meta($post_id, 'last_name', true);
$get_date_time = get_post_meta($post_id, 'appointment_date_time', true);
$get_email = get_post_meta($post_id, 'email', true);
$get_doctor_id = get_post_meta($post_id, 'doctor', true);
$get_phone = get_post_meta($post_id, 'phone', true);
$user_info = get_userdata( $get_doctor_id );
$firstname = $user_info->first_name;
$lastname = $user_info->last_name;
$get_sched_date_format = date('F d, Y', strtotime($get_date_time));
$get_sched_time_format = date('H:i A', strtotime($get_date_time));
$to = $get_email;
$headers = array('Content-Type: text/html; charset=UTF-8','From: Test <sample#gmail.com>');
$subject = 'sample subj here...';
$body = '<p><strong>Hi '.$get_first_name.',</strong></p>
<p style="margin: 0px;">Your appointment request has been confirmed. Please proceed to ......</p>
<p></p>';
wp_mail( $to, $subject, $body, $headers );
}
Please try below code:
add_action('publish_post', 'send_admin_email');
function send_admin_email($post_id){
$postdata = get_post($post_id);
$time_differ = round(abs(strtotime($postdata->post_modified) - strtotime($postdata->post_date)) / 60,2);
if ( $time_differ < 0.20 ) {
wp_mail('test#gmail.com', 'Test', 'Test message');
}
}
You can use 3rd argument of call back function for it. Cheers:)
add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( $post_ID, $post, $update ) {
if(!$update )
{
//new post
}
else
{
//update it
}
}

Send email once not every time post updated

I'm using "Front End PM" plugin and i have this snippet below for sending message to the post author when his post is published, it's working fine but still sending messages every time the post is updated! How can i stopped that?
Another point is how to send the same message to all registered users?
add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );
function fep_cus_user_publish_send_messaage( $ID, $post ){
if ( ! function_exists( 'fep_send_message' ) )
return;
$message = [];
$message['message_to_id'] = $post->post_author; // Post author ID.
$name = get_the_author_meta( 'display_name', $post->post_author );
$title = $post->post_title;
$permalink = get_permalink( $ID );
$message['message_title'] = sprintf( 'Published: %s', $title );
$message['message_content'] = sprintf ('Congratulations, %s! Your article ā€œ%sā€ has been published.', $name, $title );
$message['message_content'] .= sprintf( 'View: %s', $permalink );
$message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
$override = array('post_author' => 1);//change with message sender id
// Send message
fep_send_message( $message, $override );
}
Use this Method :
add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );
function fep_cus_user_publish_send_messaage( $ID, $post ){
if ( ! function_exists( 'fep_send_message' ) )
return;
//Check Send
$send_email = get_post_meta( $post->ID, 'fep_send_email', true );
if ( ! empty( $send_email ) ) return;
$message = [];
$message['message_to_id'] = $post->post_author; // Post author ID.
$name = get_the_author_meta( 'display_name', $post->post_author );
$title = $post->post_title;
$permalink = get_permalink( $ID );
$message['message_title'] = sprintf( 'Published: %s', $title );
$message['message_content'] = sprintf ('Congratulations, %s! Your article ā€œ%sā€ has been published.', $name, $title );
$message['message_content'] .= sprintf( 'View: %s', $permalink );
$message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
$override = array('post_author' => 1);//change with message sender id
//Set Post Meta
update_post_meta( $post->ID, 'fep_send_email', '1' );
// Send message
fep_send_message( $message, $override );
}

Getting a value from a function in WordPress

I am a bit new to WordPress and i have a small problem.
I'm checking if my mail was send or not and i want to save this information in a custom table that i made. I need to retrieve this information in a different function where I then save this in my custom table.
This is the function where i check if the mail is send or not.
/*
* Send notification when stock is low
*/
function boilerplate_custom_low_stock_notification( $order ) {
foreach ( $order->get_items() as $item ) {
print_r($item);
if ( $item->is_type( 'line_item' ) && ( $product = $item->get_product() ) && $product->managing_stock() ) {
$new_stock = get_post_meta( $product->get_id(), '_stock', true );
$minimum_stock = get_post_meta( $product->get_id(), '_stock_minimum', true );
$stock = $product->get_stock_quantity();
$name = $product->get_name();
if ( $new_stock <= $minimum_stock ) {
//Send email to admin
$notification = wp_insert_post(
array(
'post_type' => 'shop_history',
'post_title' => 'The stock of '. $name . ' is running low.',
'post_content' => 'The stock is running low. There are only ' . $stock . " pieces left.",
'post_status' => 'publish',
) );
$mailResult = false;
$mailResult = wp_mail( get_option('admin_email'), 'Your stock is running low', 'The stock of ' . $name . ' is running low. You have ' . $stock . ' piece(s) left.' );
if( $mailResult == true){
$mail_send = "Mail was send";
}else{
$mail_send = "Something went wrong en there was no mail send";
}
//error_log( $mail_send );
return $notification;
}
}
}
}
add_action( 'woocommerce_reduce_order_stock', 'boilerplate_custom_low_stock_notification', 10, 3 );
In this function i want to save the information.
/*
* Add values to column
*/
function woocommerce_shop_history_mail_column( $column, $post_id ) {
switch ( $column ) {
case 'mail' :
echo 'Is my mail send or not?';
break;
}
}
add_filter( 'manage_shop_history_posts_custom_column', 'woocommerce_shop_history_mail_column', 10, 2 );

Categories