mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://developer.woocommerce.com/2025/05/08/5-real-world-woocommerce-snippets-inspired-by-merchant-support-requests/
19 lines
928 B
Text
19 lines
928 B
Text
add_action( 'woocommerce_subscription_status_updated', 'auto_cancel_pending_cancellations', 10, 3 );
|
|
|
|
function auto_cancel_pending_cancellations( $subscription, $new_status, $old_status ) {
|
|
// Ensure we are dealing with a valid subscription object
|
|
if ( is_a( $subscription, 'WC_Subscription' ) ) {
|
|
// Check if the new status is 'pending-cancel'
|
|
if ( 'pending-cancel' === $new_status ) {
|
|
// Attempt to cancel the subscription
|
|
try {
|
|
$subscription->update_status( 'cancelled' );
|
|
// Optionally log the cancellation
|
|
error_log( 'Subscription ID ' . $subscription->get_id() . ' has been automatically cancelled.' );
|
|
} catch ( Exception $e ) {
|
|
// Log any errors
|
|
error_log( 'Error cancelling subscription ID ' . $subscription->get_id() . ': ' . $e->getMessage() );
|
|
}
|
|
}
|
|
}
|
|
}
|