• Resolved Tecca

    (@tecca)


    Hey there,

    I’m trying to update_user_meta when a subscription has expired and was hoping you could point me in the right direction. Here’s what I have:

    add_action('wps_sfw_expire_subscription_scheduler', 'spc_suspend_expired_spc_account', 10, 1);
    function spc_suspend_expired_spc_account( $subscription_id ) {
    // Exit early if no subscription ID is provided.
    if ( ! $subscription_id ) {
    return;
    }

    // Retrieve the customer ID from the subscription's post meta.
    $user_id = get_post_meta( $subscription_id, 'wps_subscription_id', true );

    // Exit early if no valid customer ID is found.
    if ( ! $user_id ) {
    // Log an error for debugging purposes
    error_log( 'SPC: No customer ID found for subscription ID ' . $subscription_id );
    return;
    }

    // Retrieve the subscription status from the post meta.
    $status = get_post_meta( $subscription_id, 'wps_subscription_status', true );

    // Verify that the status is 'expired'.
    // This check is a safeguard, as the hook implies the status is already expired.
    if ( 'expired' === $status ) {
    // Update user meta to mark the special account as expired.
    // The value 'expired' can be used to control access or display messaging.
    update_user_meta( $user_id, 'spc_account_status', 'expired' );
    }
    }

    An API will check if the account meta is set to inactive, so I really just need to get the user meta updated for expired subscriptions.

    I found the wps_sfw_expire_subscription_scheduler hook on your forum, but I’m actually unsure of what this does, and when it does it (when exactly is this triggered?)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Tecca

    (@tecca)

    I thought about this in the wrong way, and it ended up being way more complicated because of the multisite setup I’m using. Not sure who this will be helpful for, but here’s what I ended up with down below.

    Use case: a subscription expires, and I want to lock someone out of a 3rd party service when that happens. This is done via updating usermeta and running an additional function after all the checks.

    /**
    * Suspends a SPC account when a WP Swings subscription expires.
    *
    * This function hooks into the WP Swings subscription expiration action.
    *
    * @param int $subscription_id The ID of the subscription (wc_get_order)
    */
    add_action('wps_sfw_expire_subscription_scheduler', 'spc_suspend_expired_spc_account', 10, 1);
    function spc_suspend_expired_spc_account( $subscription_id ) {
    // Exit early if no subscription ID is provided.
    if ( ! $subscription_id ) {
    return;
    }

    // Assume the subscription belongs to the second site (ID 2).
    // You may need to verify this assumption by inspecting the database or using a more robust method.
    // NOTE: Replace '2' with the correct site ID if the subscription is on a different sub-site.
    $target_site_id = 2;

    // Switch to the target site to ensure the correct tables are queried.
    switch_to_blog( $target_site_id );

    // Get the WooCommerce order object using the subscription ID.
    $order = wc_get_order( $subscription_id );

    // Get the user ID from the WooCommerce order object (with fallback for guest users).
    $user_id = $order ? $order->get_user_id() : 0;
    if ( ! $user_id ) {
    $billing_email = $order ? $order->get_billing_email() : '';
    if ( $billing_email ) {
    $user = get_user_by( 'email', $billing_email );
    if ( $user ) {
    $user_id = $user->ID;
    }
    }
    }

    // Get the WP Swings specific subscription status using the WC_Order object's get_meta() method.
    $status = $order ? $order->get_meta( 'wps_subscription_status', true ) : '';

    // Restore the original site context.
    restore_current_blog();

    // Exit early if the order was not valid or no user was found.
    if ( ! $order || ! $user_id ) {
    error_log( 'SPC: No valid order or user found for subscription ID ' . $subscription_id . ' on site ' . $target_site_id );
    return;
    }

    // Log the retrieved status to aid in debugging.
    error_log( 'SPC: Retrieved WPS status for subscription ' . $subscription_id . ': ' . $status );

    // Verify that the status is 'expired'.
    if ( 'expired' === $status ) {
    // Update user meta to mark the SPC account as expired.
    // User meta is shared across all sites in a multisite network.
    update_user_meta( $user_id, 'spc_account_status', 'expired' );
    error_log( 'SPC: User meta updated successfully for user ' . $user_id );

    // Call the custom function to suspend the SPC service.
    // suspend_spc_account( $user_id );
    } else {
    error_log( 'SPC: Condition not met. WPS subscription status was ' . $status );
    }
    }

    What this does is simply check the wps_subscription_status and whether it’s expired or not. Every step has logging if you turn debugging on in WP, so you can see where each step might fail.

    1. wps_sfw_expire_subscription_scheduler (I still have no idea when this triggers tbh), whenever this event is triggered, the function will run automatically. Use the code below to test fire it for testing.
    2. Multisite compatible. Whichever site you use it on, change the “2” above to that site number.
    3. Retrieves user’s subscription status and info, then alters wp_usermeta via update_user_meta, found outside of wc_get_order — which is where WP Springs has the subscription info.

    Change spc_account_status to your own meta key, or do something else (or more) entirely. For instance, I will call another function that does something else I need done. Example commented out.

    Test the solution:

    // TEMPORARY function to manually test the cron job. -- TEMPORARY!!!
    // DELETE or COMMENT this completely out when you're done, please.
    function spc_test_spc_suspension() {
    // Replace '429' with the ID of an expired subscription for testing.
    $expired_subscription_id = 429;

    // Call the original function directly with the test subscription ID.
    spc_suspend_expired_spc_account( $expired_subscription_id );

    // Add a confirmation message at the top of the website to ensure it ran.
    echo "Test function has been run. Check the user meta data now.";
    }
    // Temporary trigger for testing.
    // You MUST DELETE this line after testing.
    // Visit example.com/?run_spc_test
    if ( isset( $_GET['run_spc_test'] ) ) {
    add_action('init', 'spc_test_spc_suspension');
    }

    Hello,

    Please use something like below,

    This hook will automatically execute when the subscription is about to expire.
    Therefore, it is not necessary to check the subscription subscription status.

    Additionally, I identified an issue in your code. Please use the following corrected line to retrieve the user_id:

    $user_id = function_exists('wps_sfw_get_meta_data') ? wps_sfw_get_meta_data($subscription_id, 'wps_customer_id', true) : 0;

    Thank you

    Thread Starter Tecca

    (@tecca)

    I appreciate that, thank you!

    Hello,

    Most Welcome! If you want you can share your appreciation on reviews also.

    We are happy to help you

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Updating user meta upon subscription expiring’ is closed to new replies.