// Schedule reminder email after purchase later action function woo_purchase_later_schedule_reminder($email, $product_id, $reminder_interval) { // Calculate the time for sending the reminder $delay_in_seconds = 0; if ($reminder_interval === '2 days') { $delay_in_seconds = 2 * DAY_IN_SECONDS; // 2 days delay } // Schedule a WP-Cron event if the interval is delayed if ($delay_in_seconds > 0) { wp_schedule_single_event(time() + $delay_in_seconds, 'woo_purchase_later_send_reminder', array($email, $product_id)); } else { // Send immediately if no delay woo_purchase_later_send_reminder($email, $product_id); } } // Send the reminder email function woo_purchase_later_send_reminder($email, $product_id) { $product_title = get_the_title($product_id); $to = $email; $subject = get_option('woo_purchase_later_reminder_email_subject', 'Your Purchase Later Reminder'); $message = get_option('woo_purchase_later_reminder_email', 'This is a reminder for the product you wanted to purchase later: ') . $product_title; // Send the reminder email wp_mail($to, $subject, $message); } // Hook for sending reminder emails add_action('woo_purchase_later_send_reminder', 'woo_purchase_later_send_reminder', 10, 2); // Modify the AJAX handler to schedule the reminder email function woo_purchase_later_handle_request() { $email = sanitize_email($_POST['email']); $product_id = intval($_POST['product_id']); $reminder_interval = sanitize_text_field($_POST['reminder_interval']); // Save reminder to the database global $wpdb; $table_name = $wpdb->prefix . 'purchase_later'; $wpdb->insert($table_name, array( 'email' => $email, 'product_id' => $product_id, 'reminder_interval' => $reminder_interval, 'status' => 'pending', 'created_at' => current_time('mysql') )); // Schedule the reminder email woo_purchase_later_schedule_reminder($email, $product_id, $reminder_interval); // Send confirmation email $product_title = get_the_title($product_id); $to = $email; $subject = get_option('woo_purchase_later_confirmation_email_subject', 'Your Purchase Later Reminder'); $message = 'You have scheduled a reminder for the product: ' . $product_title . '. We will remind you as per your selected time.'; wp_mail($to, $subject, $message); }