Send email once not every time post updated - php

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 );
}

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 );

Add post category to latest post block in WordPress

I am editing the latest-blog.php file for WordPress and I have two goals.
I would like the post category to be displayed and have a class of category assigned to it.
I would like to assign a class to the thumbnail, the title and the excerpt so I can style them.
The code below is where I believe the changes need to made
foreach ( $recent_posts as $post ) {
$post_id = $post['ID'];
$title = get_the_title( $post_id );
if ( ! $title ) {
$title = __( '(Untitled)' );
}
$post_url = get_permalink( $post_id );
$text = get_post( $post_id );
$text = $text->post_content;
$text = wp_trim_words( $text, 25, '... <a class="subtitle-red" style="display:block;" href="'.$post_url.'">read full article</a>' );
$excerpt = $text;
$list_items_markup .= sprintf(
'<li>
%1$s%3$s<p>%4$s</p>',
get_the_post_thumbnail( $post_id ),
esc_url( get_permalink( $post_id ) ),
esc_html( $title ),
$excerpt
);
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$list_items_markup .= sprintf(
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
esc_attr( get_the_date( 'c', $post_id ) ),
esc_html( get_the_date( '', $post_id ) )
);
}
$list_items_markup .= "</li>\n";
}
As this is a core WP file editing it really isn't the best idea so an extra question... could this be done via functions.php instead?
Thanks
TJ

Customize email subject in WooCommerce with vendor store-name (Dokan)

We found this question https://wordpress.stackexchange.com/questions/320924/woocommerce-order-processing-email-subject-not-changing and it's working fine. We now want to extend this by display the vendor on a order in the order completed mail.
But we are not able to output the vendor store name.
Is there a obvious error in our code?
add_filter( 'woocommerce_email_subject_customer_completed_order',
'change_completed_email_subject', 1, 2 );
function change_completed_email_subject( $subject, $order ) {
global $woocommerce;
// Order ID
$order->get_items();
// Author id
$author_id = $product->post->post_author;
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();
// Blogname
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
// Output subject
$subject = sprintf( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', $order->billing_first_name, $blogname, $order->get_order_number(), $shop_name );
return $subject;
}
Update:
I already tried to get the name via $shop_name = dokan()->vendor->get( $author_id )->get_shop_name(); but no success.
The use of global $woocommerce is not necessary
You use $order->get_items();, but don't do anything with it
$product is not defined
Use $order->get_billing_first_name() VS $order->billing_first_name
So you get:
function filter_woocommerce_email_subject_customer_completed_order( $subject, $order ) {
// Empty array
$shop_names = array();
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get product object
$product = $item->get_product();
// Author id
$author_id = $product->post->post_author;
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();
// OR JUST USE THIS FOR SHOPNAME
// Shop name
// $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
// NOT in array
if ( ! in_array( $shop_name, $shop_names ) ) {
// Push to array
$shop_names[] = $shop_name;
}
}
// Blogname
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
// Set subject
$subject = sprintf( __( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', 'woocommerce' ), $order->get_billing_first_name(), $blogname, $order->get_order_number(), implode( ', ', $shop_names ) );
// Return
return $subject;
}
add_filter( 'woocommerce_email_subject_customer_completed_order', 'filter_woocommerce_email_subject_customer_completed_order', 10, 2 );

Woocommerce change order recieved page title

I am trying to change/add in the title of the "Order Recieved" Woocommerce page.
The below snippet works - I am able to change the pre-existing TEXT with the following code:
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = $str . ' We have emailed the purchase receipt to you.';
return $new_str;
}
The below snippet does not work. - I am unable to change/add the TITLE and also pass in the username to personalise it. Here is the code and also an image of the output I am trying to achieve....The "You are awesome FIRSTNAME" added in.
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
This should be possible as there are examples on how to do it, such as here...I just can't figure out why the main title wont even appear?
As a workaround I inspected the CSS and changed the text below the header to be a larger size and the font family required.
Then through the below PHP I created the custom text with the customer name in the header.
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = sprintf( "You are awesome, %s :) - We've recieved your order.", esc_html( $order->get_billing_first_name() ) );
return $new_str;
}
2022 update:
function ps_title_order_received( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
unset( $order );
}
}
if ( isset ( $order ) ) {
$title = sprintf( "Thank you, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
add_filter( 'the_title', 'ps_title_order_received', 10, 2 );

WordPress send email on add_post_meta

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 );
}
}

Categories