Last updated on October 18, 2023

WooCommerce subscription renewal issues

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Resolve WooCommerce subscription renewal issues.

If you are experiencing issues with WooCommerce subscription renewals, there are a few common problems that you can troubleshoot. One common issue is when the renewal payment fails or is not processed correctly. To address this, you can use the woocommerce_subscription_payment_failed hook to perform custom actions when a subscription renewal payment fails.

Here’s an example code snippet that demonstrates how to use this hook:

function wpsnippets_handle_subscription_payment_failed( $subscription ) {
    // Get the user associated with the subscription
    $user_id = $subscription->get_user_id();

    // Send a notification to the user about the failed payment
    $user_email = get_userdata( $user_id )->user_email;
    $subject = 'Subscription Renewal Payment Failed';
    $message = 'Your subscription renewal payment has failed. Please update your payment details to avoid any service interruptions.';
    wp_mail( $user_email, $subject, $message );
}
add_action( 'woocommerce_subscription_payment_failed', 'wpsnippets_handle_subscription_payment_failed' );

In this example, we define a custom function wpsnippets_handle_subscription_payment_failed that accepts the $subscription object as a parameter. Inside the function, we retrieve the user ID associated with the subscription and send an email notification to the user informing them about the failed payment.

You can customize the email message, subject, and any additional actions you want to perform when a subscription renewal payment fails.

By using this code snippet, you can handle subscription renewal payment failures in a way that suits your specific needs, such as sending notifications or performing additional actions to resolve the issue.

Examples

Example 1: Check if a WooCommerce subscription is active

This example demonstrates how to check if a WooCommerce subscription is active using the wcs_is_subscription_active() function.

if ( wcs_is_subscription_active( $subscription_id ) ) {
    // Subscription is active
} else {
    // Subscription is not active
}

The wcs_is_subscription_active() function accepts a subscription ID as a parameter and returns a boolean value indicating whether the subscription is active or not.

Example 2: Get the next renewal date of a WooCommerce subscription

This example shows how to retrieve the next renewal date of a WooCommerce subscription using the wcs_get_next_payment_date() function.

$next_renewal_date = wcs_get_next_payment_date( $subscription_id );

if ( $next_renewal_date ) {
    echo 'Next renewal date: ' . $next_renewal_date->format( 'Y-m-d' );
} else {
    echo 'Subscription does not have a next renewal date.';
}

The wcs_get_next_payment_date() function takes a subscription ID as a parameter and returns a DateTime object representing the next renewal date of the subscription. You can format the date using the format() method of the DateTime object.

Example 3: Cancel a WooCommerce subscription renewal

This example demonstrates how to cancel the renewal of a WooCommerce subscription using the wcs_cancel_subscription_renewal() function.

wcs_cancel_subscription_renewal( $subscription_id );

The wcs_cancel_subscription_renewal() function cancels the renewal of the subscription specified by the subscription ID. After canceling the renewal, the subscription will not be automatically renewed in the future.

Last updated on October 18, 2023. Originally posted on December 2, 2023.

Leave a Reply