Skip to content

Instantly share code, notes, and snippets.

@bernattorras
Last active November 22, 2021 01:49
Show Gist options
  • Save bernattorras/9a96fe50a9fa63d9991e5c503782db23 to your computer and use it in GitHub Desktop.
Save bernattorras/9a96fe50a9fa63d9991e5c503782db23 to your computer and use it in GitHub Desktop.
A custom function to log any subscription status changes.
<?php
// Log Subscription status changes
add_filter( 'woocommerce_subscription_status_updated', 'log_sub_status_changes', 10, 3 );
function log_sub_status_changes( $subscription, $new_status, $old_status ) {
$logger = new WC_Logger();
$subscription_id = $subscription->get_id();
$sub_user = $subscription->get_user_id();
switch ( $new_status ) {
// All available statuses are "pending", "active", "on-hold", "pending-cancel", "cancelled", and "expired"
// Add as many 'cases' as wanted for each status change
case 'expired':
$logger->add( 'sub_status_changes', 'Subscription #' . $subscription_id . ' (from user #' . $sub_user . ') expired' );
break;
default:
$logger->add( 'sub_status_changes', 'Subscription #' . $subscription_id . ' (from user #' . $sub_user . ') changed its status from ' . $old_status . ' to ' . $new_status );
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment