Created
April 29, 2024 21:22
-
-
Save RiaanKnoetze/ad1d009cbaf2bbf05a33d3647fb85034 to your computer and use it in GitHub Desktop.
Sends custom email notifications for WooCommerce subscription status changes from on-hold to active and pending-cancel to active.
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 | |
// Hook for reactivation from 'on-hold' to 'active' | |
add_action('woocommerce_subscription_status_on-hold_to_active', 'send_custom_email_on_reactivation', 10, 1); | |
function send_custom_email_on_reactivation($subscription) { | |
$user_email = $subscription->get_billing_email(); | |
// Optional: Check if this is an admin reactivation | |
if (is_admin() && current_user_can('manage_woocommerce')) { | |
$action = 'admin reactivated'; | |
} else { | |
$action = 'reactivated'; | |
} | |
send_custom_email($user_email, $action); | |
} | |
function send_custom_email($to, $action) { | |
$subject = 'Subscription ' . $action; | |
$message = 'Your subscription has been ' . $action . '.'; | |
$headers = array('Content-Type: text/html; charset=UTF-8'); | |
wp_mail($to, $subject, $message, $headers); | |
} | |
add_action('woocommerce_subscription_status_updated', 'check_specific_status_transitions', 10, 3); | |
function check_specific_status_transitions($subscription, $new_status, $old_status) { | |
if ($old_status === 'pending-cancel' && $new_status === 'active') { | |
send_custom_email_on_reactivation($subscription); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment