Hook in woocommerce on subscription expiration

How can I create an Hook in woocommerce (wordpress) to be able to trigger a function when a subscription expires?

Something like this

Read More
add_action('woocommerce_subscription_expired', 'my_function', 10, 1);

function my_function($order_id) {
    echo "yeahhhh";
}

UPDATE

I found in the developer doc the following

Action: ‘subscription_expired’

Parameters: $user_id Integer The ID of the user for whom the
subscription expired. $subscription_key String The key for the
subscription that just expired on the user’s account.

Description: Triggered when a subscription reaches the end of its
term, if a length was set on the subscription when it was purchased.
This event may be triggered by either WooCommerce Subscriptions, which
schedules a cron-job to expire each subscription, or by the payment
gateway extension which can call the
WC_Subscriptions_Manager::expire_subscription() function directly.

Where should I place this to have it working

Thanks

Related posts

2 comments

  1. Looks like you are almost there. You just need to use Subscription’s action hook and pass the correct parameters. This seems like a start:

    add_action( 'subscription_expired', 'my_function', 10, 2 );
    
    function my_function( $user_id, $subscription_key ) {
        $sub= wcs_get_subscription_from_key( $subscription_key );
        // do something
    }
    
  2. The wcs_get_subscription_from_key is now deprecated, since version 2.0.

    You can now use:

    add_action( 'woocommerce_subscription_status_expired', 'my_on_subscription_expired', 10 );
    function my_on_subscription_expired( $subscription ) {
        // do something
    }
    

    More hooks (and this one) at Subscriptions Action Reference

Comments are closed.