Created
May 4, 2023 09:12
-
-
Save plugin-republic/c93568fd22f06b422c36dc83068cf8d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get all bookings with an end date in a specified number of days time | |
*/ | |
function prefix_get_bookings_ending() { | |
// How many days in advance should we send the email? | |
$days = 3; | |
$date = date( 'Y-m-d', strtotime( '+' . absint( $days ) . 'days' ) ); | |
error_log( $date ); | |
$args = array( | |
'post_type' => 'bfwc_booking', | |
'posts_per_page' => -1, | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => 'bfwc_end_date', | |
'value' => $date, | |
'compare' => '=' | |
), | |
array( | |
'key' => 'bfwc_status', | |
'value' => 'cancelled', | |
'compare' => '!=' | |
) | |
), | |
'fields' => 'ids' | |
); | |
$bookings = new WP_Query( $args ); | |
// A list of bookings | |
$booking_ids = $bookings->posts; | |
if( $booking_ids ) { | |
foreach( $booking_ids as $booking_id ) { | |
prefix_send_reminder_email( $booking_id, $date ); | |
} | |
} | |
wp_reset_postdata(); | |
} | |
add_action( 'woocommerce_scheduled_sales', 'prefix_get_bookings_ending' ); | |
/** | |
* This function sends the email | |
*/ | |
function prefix_send_reminder_email( $booking_id, $end_date ) { | |
$booking_product_id = get_post_meta( $booking_id, 'bfwc_product_id', true ); | |
$to = get_post_meta( $booking_id, 'bfwc_user_email', true ); | |
$subject = __( 'Your booking', 'bfwc' ); | |
ob_start(); | |
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $subject ) ); | |
$content = ob_get_clean(); | |
// Format the date | |
if( function_exists( 'bfwc_format_date') ) { | |
$end_date = bfwc_format_date( $end_date ); | |
} else { | |
$date_format = get_option( 'date_format' ); // no longer in use since 1.4.4 | |
$end_date = date( $date_format, strtotime( $end_date ) ); | |
} | |
$message = sprintf( | |
'<p>%s<p>', | |
sprintf( | |
__( 'Your booking is coming to an end on %s', 'bfwc' ), | |
$end_date | |
) | |
); | |
$message = apply_filters( 'bfwc_reminder_email_content', $message, $booking_id ); | |
$content .= $message; | |
ob_start(); | |
wc_get_template( 'emails/email-footer.php' ); | |
$content .= ob_get_clean(); | |
wc_mail( $to, $subject, $content ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment