Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created August 1, 2024 12:16
Show Gist options
  • Select an option

  • Save goranefbl/6348a0190528bf4628aceace31093dc0 to your computer and use it in GitHub Desktop.

Select an option

Save goranefbl/6348a0190528bf4628aceace31093dc0 to your computer and use it in GitHub Desktop.
<?php
/**
* Hook into checkout
*
* @since 2.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
class WPGens_RAF_Reminder_Email
{
/**
* Constructor.
*
*/
public function __construct()
{
add_action('woocommerce_order_status_' . apply_filters('wpgens_raf_order_status', 'completed'), array($this, 'schedule_order_completed_email'), 10, 1 );
}
public function schedule_order_completed_email($order_id) {
$gens_raf_send_reminder_email = get_option('gens_raf_send_reminder_email');
$gens_raf_reminder_email_delay = get_option('gens_raf_reminder_email_delay', '5');
if($gens_raf_send_reminder_email) {
wp_schedule_single_event(time() + (int)$gens_raf_reminder_email_delay * MINUTE_IN_SECONDS, 'send_referral_notification_email_event', array($order_id));
}
}
public function send_referral_notification_email($order_id)
{
if (!$order_id) {
return;
}
$order = wc_get_order( $order_id );
if (!$order) {
return false;
}
$user_id = $order->get_user_id();
if ($user_id) {
if(get_user_meta($user_id, 'gens_referral_notification_sent', true)) {
return false;
}
add_user_meta($user_id, 'gens_referral_notification_sent', 1);
// Get the gens_referral_id user meta for registered users
$referral = new WPGens_RAF_User($user_id);
$referral_id = $referral->get_referral_id();
} else {
if(!get_option( "gens_raf_allow_guests")) {
return false;
}
// Get the referral id for guests
$email = $order->get_billing_email();
// For guests, we will check if user has only one order, as notification is sent only once.
$customer_orders = wc_get_orders(array(
'numberposts' => 2,
'billing_email' => $email,
'status' => array('wc-processing', 'wc-completed'),
));
if (count($customer_orders) != 1) {
return false;
}
$referral_id = $email;
}
// Get the gens_referral_id user meta
$email = new WPGens_RAF_Email($order->get_billing_email(), $referral_id, $order_id, "reminder");
$email->send_email();
}
}
$wpgens_raf_reminder_email = new WPGens_RAF_Reminder_Email();
add_action('send_referral_notification_email_event', array($wpgens_raf_reminder_email, 'send_referral_notification_email'), 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment