Skip to main content

Is there an easy way to find out if the user has valid access with code?

Hi FluentCart Team,

I'm currently testing FluentCart before purchasing it and integrating it with Etch.

When a subscription is cancelled, attributes like expire_at return null, which makes it difficult to determine whether the user should still have access during the remaining billing period.

I temporarily used created_at, but this is not reliable for multi-year subscriptions or different billing cycles.

What is the correct way to programmatically detect if a cancelled subscription is still within its valid access period?

Thanks in advance.

add_filter('etch/dynamic_data/user', function($data, $user_id) {

    $customer = \FluentCart\App\Models\Customer::where('user_id', $user_id)->first();

    if (!$customer) {
        $data['subscriptions'] = [
            'has_subscription' => false,
            'has_valid_access' => false
        ];
        return $data;
    }

    $subscriptions = \FluentCart\App\Models\Subscription::where('customer_id', $customer->id)->get();

    if (!$subscriptions->count()) {
        $data['subscriptions'] = [
            'has_subscription' => false,
            'has_valid_access' => false
        ];
        return $data;
    }

    $active_subscription = \FluentCart\App\Models\Subscription::where('customer_id', $customer->id)
        ->where('status', 'active')
        ->first();

    $formatted_subscriptions = [];
    $has_valid_access = false;

    foreach ($subscriptions as $subscription) {

        $expires = null;
        $status  = strtolower($subscription->status);

        /* ACTIVE */
        if ($status === 'active') {
            $expires = true;
            $has_valid_access = true;
        }

        /* CANCELLED */
        elseif ($status === 'cancelled' || $status === 'canceled') {

            if (!empty($subscription->created_at)) {

                try {

                    if ($subscription->created_at instanceof \DateTimeInterface) {
                        $created = clone $subscription->created_at;
                    } else {
                        $created = new \DateTime($subscription->created_at);
                    }

                    $expiry = clone $created;
                    $expiry->modify('+1 year');

                    $now = new \DateTime();

                    $is_valid = $now < $expiry;

                    $expires = [
                        'valid' => $is_valid,
                        'date'  => $expiry->format('Y-m-d H:i:s')
                    ];

                    /* Valid access */
                    if ($is_valid) {
                        $has_valid_access = true;
                    }

                } catch (\Exception $e) {
                    $expires = null;
                }
            }
        }

        /* Other status */
        else {
            $expires = null;
        }

        $formatted_subscriptions[] = [
            'id'      => $subscription->id,
            'status'  => $subscription->status,
            'plan'    => $subscription->plan_id,
            'expires' => $expires
        ];
    }

    $data['subscriptions'] = [
        'has_subscription' => true,
        'items'            => $formatted_subscriptions,
        'is_active'        => !!$active_subscription,
        'has_valid_access' => $has_valid_access
    ];

    return $data;

}, 10, 2);

Taylor Drayson

Fluent Cart handles subscription status automatically, You can use the built-in functions for this. Here's an example based on the code you wrote - https://snippets.tdrayson.com/p83fcq/

John Diaz

Hi Taylor DraysonΒ thank you very much for your help.

However, my main issue is related to subscription status handling. When a user cancels their subscription, the status changes to cancelled. Even though the subscription may still be within the paid billing period, the status alone no longer allows me to reliably determine whether the user should retain access.

For my use case, I need a way to accurately detect whether a subscription is still valid for access purposes, regardless of whether it has been cancelled. In other words, if a user cancels but still has remaining time in their billing cycle, they should continue accessing protected resources.

Taylor Drayson

John DiazΒ you should be able to call - $subscription->hasAccessValidity() function on the subscription object. This should handle all the billing period inclusion stuff

John Diaz

Taylor DraysonΒ Thank you very much, that's what I was looking for. I'm going to keep testing, but for now I think the refunds aren't working properly and hasAccessValidity returns true regardless of whether a purchase has been refunded, but maybe that's because of the error.