Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created January 17, 2023 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/6ebc854f015ca42746c5815089161168 to your computer and use it in GitHub Desktop.
Save plugin-republic/6ebc854f015ca42746c5815089161168 to your computer and use it in GitHub Desktop.
<?php
/**
* Get all bookings scheduled for tomorrow
*/
function oliver_get_bookings_for_tomorrow() {
// Get tomorrow's date
$tomorrow = date( 'Y-m-j', strtotime('tomorrow') );
$args = array(
'post_type' => 'bfwc_booking',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'bfwc_start_date',
'value' => $tomorrow,
'compare' => '='
),
array(
'key' => 'bfwc_status',
'value' => 'cancelled',
'compare' => '!='
)
),
'fields' => 'ids'
);
$bookings = new WP_Query( $args );
// A list of bookings for tomorrow
$booking_ids = $bookings->posts;
if( $booking_ids ) {
foreach( $booking_ids as $booking_id ) {
oliver_send_reminder_email( $booking_id );
}
}
wp_reset_postdata();
}
add_action( 'woocommerce_scheduled_sales', 'oliver_get_bookings_for_tomorrow' );
/**
* This function sends the email
* Check the booking ID for the booking product
* Then send an email accordingly
*/
function oliver_send_reminder_email( $booking_id ) {
$booking_product_id = get_post_meta( $booking_id, 'bfwc_product_id', true );
// Make a list of Booking Product IDs and Keycodes
// Use this pattern: $booking_product_id => $keycode
$keys = array(
'914' => '1234', // E.g. 904 is your booking product ID, 1234 is the keycode for this product
'923' => '5678'
);
// Find the keycode
$keycode = isset( $keys[$booking_id] ) ? $keys[$booking_id] : false;
if( $keycode ) {
$to = get_post_meta( $booking_id, 'bfwc_user_email', true );
$subject = __( 'Your key', 'bfwc' );
ob_start();
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $subject ) );
$content = ob_get_clean();
$message = sprintf(
'<p>%s<p>',
sprintf(
__( 'Your keycode is %s', 'wcmo' ),
$keycode
)
);
$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