Automatically Cancel WooCommerce Subscriptions with Pending Cancel Status

Managing subscriptions in WooCommerce is an essential task for many online store owners. If you’re running a subscription-based WooCommerce store, you’ve probably encountered the “Pending Cancel” status. This status occurs when a customer initiates a cancellation, leaving the subscription waiting for confirmation or completion.

But what if you could automate this process, saving time and reducing manual work? In this post, I’ll share a WooCommerce code snippet that automatically transitions subscriptions from “Pending Cancel” to “Cancelled.” Let’s dive in!

Why Automate Subscription Cancellations?

The Pending Cancel status is useful when you want to delay cancellations for administrative purposes or until the subscription period ends. However, if your business model doesn’t require this delay, manually managing cancellations can be tedious.

Benefits of Automation:

  • Save Time: Automate repetitive tasks.
  • Improve Accuracy: Avoid errors from manual status changes.
  • Streamline Operations: Keep your subscription management efficient.

Automating Pending Cancel Subscriptions

Here’s the code snippet you can add to your WooCommerce-powered WordPress site:

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() );
            }
        }
    }
}

Shred on gist too: https://gist.github.com/shameemreza/ed6ff1c664c497acbcc37e7892a03974

How It Works:

  1. Hook into Status Updates: The woocommerce_subscription_status_updated hook listens for any subscription status changes.
  2. Check for Pending Cancel: The function checks if the new status is pending-cancel.
  3. Cancel the Subscription: If the condition is met, it automatically changes the status to cancelled.

More information on the action can be found here: https://woocommerce.com/document/subscriptions/develop/action-reference/#subscription-status-change-actions

/

Step-by-Step Guide to Add the Code

Follow these steps to add the snippet to your WordPress site:

  1. Backup Your Site: Before making changes, always create a backup.
  2. Add the Code:
    • Go to your WordPress dashboard.
    • Navigate to Appearance > Theme File Editor or use a plugin like Code Snippets.
    • Add the snippet to your child theme’s functions.php file or in the Code Snippets plugin.
  3. Test the Code:
    • Create a subscription product (Simple or Variable).
    • As a customer, purchase a subscription and initiate a cancellation.
    • Confirm that the status automatically updates from Pending Cancel to Cancelled.

Use Case 1: Seamless Customer Experience

Imagine a subscription box business offering monthly deliveries. If a customer decides to cancel their subscription, automating the process ensures no manual intervention is required, providing a smooth customer journey.

Use Case 2: Simplified Management for Store Admins

For store owners managing hundreds of subscriptions, automation eliminates the hassle of manually updating statuses, freeing up time to focus on growth.

Conclussion

WooCommerce is an incredibly powerful open source WordPress plugin, and with customizations like this, you can tailor it to your business needs. Automating subscription cancellations is just one way to simplify your workflow and enhance operational efficiency.

Whether you’re managing a small subscription service or a large-scale operation, this code snippet can help you save time and reduce errors. Implement it on your site today, and let automation do the heavy lifting for you.

Have you tried this code snippet on your WooCommerce store? Let me know in the comments how it worked for you. If you found this post helpful, feel free to share it with other WooCommerce enthusiasts!

Happy Automating! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *