Subscription Queries

You can fetch subscriptions in your templates or PHP code using subscription queries.

{# Create a new subscription query #}
{% set mySubscriptionQuery = craft.subscriptions() %}

Once you’ve created a subscription query, you can set parameters on it to narrow down the results, and then execute it by calling .all(). An array of Subscription objects will be returned.

TIP

See Introduction to Element Queries in the Craft docs to learn about how element queries work.

Example

We can display all of the current user’s subscriptions by doing the following:

  1. Create a subscription query with craft.subscriptions().
  2. Set the user parameter on it.
  3. Fetch the subscriptions with .all().
  4. Loop through the subscriptions using a for tag to output their HTML.
{# Make sure someone is logged in #}
{% requireLogin %}

{# Create a subscription query with the 'user' parameter #}
{% set mySubscriptionQuery = craft.subscriptions()
    .user(currentUser) %}

{# Fetch the subscriptions #}
{% set subscriptions = mySubscriptionQuery.all() %}

{# Display the subscriptions #}
{% for subscription in subscriptions %}
    <article>
        <h1><a href="{{ subscription.url }}">{{ subscription.title }}</a></h1>
        {{ subscription.summary }}
        <a href="{{ subscription.url }}">Learn more</a>
    </article>
{% endfor %}

Parameters

Subscription queries support the following parameters:

anyStatus

Clears out the status and enabledForSite() parameters.

{# Fetch all subscriptions, regardless of status #}
{% set subscriptions = craft.subscriptions()
    .anyStatus()
    .all() %}

asArray

Causes the query to return matching subscriptions as arrays of data, rather than \craft\commerce\elements\Subscription objects.

{# Fetch subscriptions as arrays #}
{% set subscriptions = craft.subscriptions()
    .asArray()
    .all() %}

dateCanceled

Narrows the query results based on the subscriptions’ cancellation date.

Possible values include:

ValueFetches subscriptions…
'>= 2018-04-01'that were canceled on or after 2018-04-01.
'< 2018-05-01'that were canceled before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were canceled between 2018-04-01 and 2018-05-01.
{# Fetch subscriptions that were canceled recently #}
{% set aWeekAgo = date('7 days ago')|atom %}

{% set subscriptions = craft.subscriptions()
    .dateCanceled(">= #{aWeekAgo}")
    .all() %}

dateCreated

Narrows the query results based on the subscriptions’ creation dates.

Possible values include:

ValueFetches subscriptions…
'>= 2018-04-01'that were created on or after 2018-04-01.
'< 2018-05-01'that were created before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were created between 2018-04-01 and 2018-05-01.
{# Fetch subscriptions created last month #}
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}

{% set subscriptions = craft.subscriptions()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}

dateExpired

Narrows the query results based on the subscriptions’ expiration date.

Possible values include:

ValueFetches subscriptions…
'>= 2018-04-01'that expired on or after 2018-04-01.
'< 2018-05-01'that expired before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that expired between 2018-04-01 and 2018-05-01.
{# Fetch subscriptions that expired recently #}
{% set aWeekAgo = date('7 days ago')|atom %}

{% set subscriptions = craft.subscriptions()
    .dateExpired(">= #{aWeekAgo}")
    .all() %}

dateUpdated

Narrows the query results based on the subscriptions’ last-updated dates.

Possible values include:

ValueFetches subscriptions…
'>= 2018-04-01'that were updated on or after 2018-04-01.
'< 2018-05-01'that were updated before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were updated between 2018-04-01 and 2018-05-01.
{# Fetch subscriptions updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

{% set subscriptions = craft.subscriptions()
    .dateUpdated(">= #{lastWeek}")
    .all() %}

fixedOrder

Causes the query results to be returned in the order specified by id.

{# Fetch subscriptions in a specific order #}
{% set subscriptions = craft.subscriptions()
    .id([1, 2, 3, 4, 5])
    .fixedOrder()
    .all() %}

gatewayId

Narrows the query results based on the gateway, per its ID.

Possible values include:

ValueFetches subscriptions…
1with a gateway with an ID of 1.
'not 1'not with a gateway with an ID of 1.
[1, 2]with a gateway with an ID of 1 or 2.
['not', 1, 2]not with a gateway with an ID of 1 or 2.

id

Narrows the query results based on the subscriptions’ IDs.

Possible values include:

ValueFetches subscriptions…
1with an ID of 1.
'not 1'not with an ID of 1.
[1, 2]with an ID of 1 or 2.
['not', 1, 2]not with an ID of 1 or 2.
{# Fetch the subscription by its ID #}
{% set subscription = craft.subscriptions()
    .id(1)
    .one() %}

TIP

This can be combined with fixedOrder if you want the results to be returned in a specific order.

inReverse

Causes the query results to be returned in reverse order.

{# Fetch subscriptions in reverse #}
{% set subscriptions = craft.subscriptions()
    .inReverse()
    .all() %}

isCanceled

Narrows the query results to only subscriptions that are canceled.

{# Fetch canceled subscriptions #}
{% set subscriptions = {twig-function}
    .isCanceled()
    .all() %}

isExpired

Narrows the query results to only subscriptions that have expired.

{# Fetch expired subscriptions #}
{% set subscriptions = {twig-function}
    .isExpired()
    .all() %}

limit

Determines the number of subscriptions that should be returned.

{# Fetch up to 10 subscriptions  #}
{% set subscriptions = craft.subscriptions()
    .limit(10)
    .all() %}

nextPaymentDate

Narrows the query results based on the subscriptions’ next payment dates.

Possible values include:

ValueFetches subscriptions…
'>= 2018-04-01'with a next payment on or after 2018-04-01.
'< 2018-05-01'with a next payment before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']with a next payment between 2018-04-01 and 2018-05-01.
{# Fetch subscriptions with a payment due soon #}
{% set aWeekFromNow = date('+7 days')|atom %}

{% set subscriptions = craft.subscriptions()
    .nextPaymentDate("< #{aWeekFromNow}")
    .all() %}

offset

Determines how many subscriptions should be skipped in the results.

{# Fetch all subscriptions except for the first 3 #}
{% set subscriptions = craft.subscriptions()
    .offset(3)
    .all() %}

onTrial

Narrows the query results to only subscriptions that are on trial.

{# Fetch trialed subscriptions #}
{% set subscriptions = {twig-function}
    .onTrial()
    .all() %}

orderBy

Determines the order that the subscriptions should be returned in.

{# Fetch all subscriptions in order of date created #}
{% set subscriptions = craft.subscriptions()
    .orderBy('elements.dateCreated asc')
    .all() %}

orderId

Narrows the query results based on the order, per its ID.

Possible values include:

ValueFetches subscriptions…
1with an order with an ID of 1.
'not 1'not with an order with an ID of 1.
[1, 2]with an order with an ID of 1 or 2.
['not', 1, 2]not with an order with an ID of 1 or 2.

plan

Narrows the query results based on the subscription plan.

Possible values include:

ValueFetches subscriptions…
'foo'for a plan with a handle of foo.
['foo', 'bar']for plans with a handle of foo or bar.
a Plan objectfor a plan represented by the object.
{# Fetch Supporter plan subscriptions #}
{% set subscriptions = craft.subscriptions()
    .plan('supporter')
    .all() %}

planId

Narrows the query results based on the subscription plans’ IDs.

Possible values include:

ValueFetches subscriptions…
1for a plan with an ID of 1.
[1, 2]for plans with an ID of 1 or 2.
['not', 1, 2]for plans not with an ID of 1 or 2.

reference

Narrows the query results based on the reference.

relatedTo

Narrows the query results to only subscriptions that are related to certain other elements.

See Relations for a full explanation of how to work with this parameter.

{# Fetch all subscriptions that are related to myCategory #}
{% set subscriptions = craft.subscriptions()
    .relatedTo(myCategory)
    .all() %}

Narrows the query results to only subscriptions that match a search query.

See Searching for a full explanation of how to work with this parameter.

{# Get the search query from the 'q' query string param #}
{% set searchQuery = craft.request.getQueryParam('q') %}

{# Fetch all subscriptions that match the search query #}
{% set subscriptions = craft.subscriptions()
    .search(searchQuery)
    .all() %}

status

Narrows the query results based on the subscriptions’ statuses.

Possible values include:

ValueFetches subscriptions…
'active'(default)that are active.
'expired'that have expired.
{# Fetch expired subscriptions #}
{% set subscriptions = {twig-function}
    .status('expired')
    .all() %}

trialDays

Narrows the query results based on the number of trial days.

uid

Narrows the query results based on the subscriptions’ UIDs.

{# Fetch the subscription by its UID #}
{% set subscription = craft.subscriptions()
    .uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
    .one() %}

user

Narrows the query results based on the subscriptions’ user accounts.

Possible values include:

ValueFetches subscriptions…
'foo'for a user account with a username of foo
['foo', 'bar']for user accounts with a username of foo or bar.
a User objectfor a user account represented by the object.
{# Fetch the current user's subscriptions #}
{% set subscriptions = craft.subscriptions()
    .user(currentUser)
    .all() %}

userId

Narrows the query results based on the subscriptions’ user accounts’ IDs.

Possible values include:

ValueFetches subscriptions…
1for a user account with an ID of 1.
[1, 2]for user accounts with an ID of 1 or 2.
['not', 1, 2]for user accounts not with an ID of 1 or 2.
{# Fetch the current user's subscriptions #}
{% set subscriptions = craft.subscriptions()
    .userId(currentUser.id)
    .all() %}

with

Causes the query to return matching subscriptions eager-loaded with related elements.

See Eager-Loading Elements for a full explanation of how to work with this parameter.

{# Fetch subscriptions eager-loaded with the "Related" field’s relations #}
{% set subscriptions = craft.subscriptions()
    .with(['related'])
    .all() %}