Subscriptions

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Subscriptions » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
2.0

Subscriptions service.

View source (opens new window)

# Public Properties

Property Description
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of a component property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets the value of a component property.
__unset() (opens new window) Sets a component property to be null.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
beforeDeleteUserHandler() Prevent deleting a user if they have any subscriptions - active or otherwise.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
cancelSubscription() Cancel a subscription.
className() (opens new window) Returns the fully qualified name of this class.
createSubscription() Subscribe a user to a subscription plan.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
doesUserHaveAnySubscriptions() Return true if the user has any subscriptions at all, even expired ones.
doesUserHaveSubscriptions() Return true if the user has any subscriptions at all, even expired ones.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
expireSubscription() Expire a subscription.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getSubscriptionCountByPlanId() Returns subscription count for a plan.
getSubscriptionCountForPlanById() Returns subscription count for a plan.
handleChangedFieldLayout() Handle field layout change
handleDeletedFieldLayout() Handle field layout being deleted
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined for this component.
init() (opens new window) Initializes the object.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
pruneDeletedField()
reactivateSubscription() Reactivate a subscription.
receivePayment() Receive a payment for a subscription
switchSubscriptionPlan() Switch a subscription to a different subscription plan.
trigger() (opens new window) Triggers an event.
updateSubscription() Update a subscription.

# beforeDeleteUserHandler()

Prevent deleting a user if they have any subscriptions - active or otherwise.

View source (opens new window)

Arguments

# cancelSubscription()

Cancel a subscription.

View source (opens new window)

Arguments

  • $subscription
  • $parameters

Throws

# createSubscription()

Subscribe a user to a subscription plan.

View source (opens new window)

Arguments

Returns

craft\commerce\elements\Subscription – The subscription

Throws

# doesUserHaveAnySubscriptions()

DEPRECATED

Deprecated in 4.0. Use doesUserHaveSubscriptions() instead.

Return true if the user has any subscriptions at all, even expired ones.

View source (opens new window)

Arguments

  • $userId

# doesUserHaveSubscriptions()

Return true if the user has any subscriptions at all, even expired ones.

View source (opens new window)

Arguments

  • $userId

# expireSubscription()

Expire a subscription.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether successfully expired subscription

Throws

# getSubscriptionCountByPlanId()

Returns subscription count for a plan.

View source (opens new window)

Arguments

  • $planId

# getSubscriptionCountForPlanById()

DEPRECATED

Deprecated in 4.0. Use getSubscriptionCountByPlanId() instead.

Returns subscription count for a plan.

View source (opens new window)

Arguments

  • $planId

# handleChangedFieldLayout()

Handle field layout change

View source (opens new window)

Arguments

  • $event

Throws

# handleDeletedFieldLayout()

Handle field layout being deleted

View source (opens new window)

# pruneDeletedField()

DEPRECATED

Deprecated in 3.4.17. Unused fields will be pruned automatically as field layouts are resaved.

View source (opens new window)

# reactivateSubscription()

Reactivate a subscription.

View source (opens new window)

Arguments

  • $subscription

Throws

# receivePayment()

Receive a payment for a subscription

View source (opens new window)

Arguments

  • $subscription
  • $payment
  • $paidUntil

Throws

# switchSubscriptionPlan()

Switch a subscription to a different subscription plan.

View source (opens new window)

Arguments

Throws

# updateSubscription()

Update a subscription.

View source (opens new window)

Arguments

  • $subscription

Throws

# Constants

Constant Description
CONFIG_FIELDLAYOUT_KEY

# Events

# EVENT_AFTER_CANCEL_SUBSCRIPTION

Type
craft\commerce\events\CancelSubscriptionEvent

The event that is triggered after a subscription gets canceled.

use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\CancelSubscriptionForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_CANCEL_SUBSCRIPTION,
    function(CancelSubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var CancelSubscriptionForm $params
        $params = $event->parameters;

        // Refund the user for the remainder of the subscription
        // ...
    }
);

# EVENT_AFTER_CREATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription is created.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_CREATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Call a third party service to authorize a user
        // ...
    }
);

# EVENT_AFTER_EXPIRE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription has expired.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_EXPIRE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Make a call to third party service to de-authorize a user
        // ...
    }
);

# EVENT_AFTER_REACTIVATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription gets reactivated.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_REACTIVATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Re-authorize the user with a third-party service
        // ...
    }
);

# EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN

Type
craft\commerce\events\SubscriptionSwitchPlansEvent

The event that is triggered after a subscription gets switched to a different plan.

use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\base\Plan;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\SwitchPlansForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN,
    function(SubscriptionSwitchPlansEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var Plan $oldPlan
        $oldPlan = $event->oldPlan;
        // @var Plan $newPlan
        $newPlan = $event->newPlan;
        // @var SwitchPlansForm $params
        $params = $event->parameters;

        // Adjust the user’s permissions on a third party service
        // ...
    }
);

# EVENT_BEFORE_CANCEL_SUBSCRIPTION

Type
craft\commerce\events\CancelSubscriptionEvent

The event that is triggered before a subscription is canceled.

You may set the isValid property to false on the event to prevent the subscription from being canceled.

use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\CancelSubscriptionForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_CANCEL_SUBSCRIPTION,
    function(CancelSubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var CancelSubscriptionForm $params
        $params = $event->parameters;

        // Check whether the user is permitted to cancel the subscription
        // ...
    }
);

# EVENT_BEFORE_CREATE_SUBSCRIPTION

Type
craft\commerce\events\CreateSubscriptionEvent

The event that is triggered before a subscription is created.

You may set the isValid property to false on the event to prevent the user from being subscribed to the plan.

use craft\commerce\events\CreateSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\elements\User;
use craft\commerce\base\Plan;
use craft\commerce\models\subscriptions\SubscriptionForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_CREATE_SUBSCRIPTION,
    function(CreateSubscriptionEvent $event) {
        // @var User $user
        $user = $event->user;
        // @var Plan $plan
        $plan = $event->plan;
        // @var SubscriptionForm $params
        $params = $event->parameters;

        // Set the trial days based on some business logic
        // ...
    }
);

# EVENT_BEFORE_REACTIVATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

TThe event that is triggered before a subscription gets reactivated.

You may set the isValid property to false on the event to prevent the subscription from being reactivated.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_REACTIVATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Use business logic to determine whether the user can reactivate
        // ...
    }
);

# EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN

Type
craft\commerce\events\SubscriptionSwitchPlansEvent

The event that is triggered before a subscription is switched to a different plan.

You may set the isValid property to false on the event to prevent the switch from happening.

use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\base\Plan;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\SwitchPlansForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN,
    function(SubscriptionSwitchPlansEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var Plan $oldPlan
        $oldPlan = $event->oldPlan;
        // @var Plan $newPlan
        $newPlan = $event->newPlan;
        // @var SwitchPlansForm $params
        $params = $event->parameters;

        // Modify the switch parameters based on some business logic
        // ...
    }
);

# EVENT_BEFORE_UPDATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered before a subscription gets updated. Typically this event is fired when subscription data is updated on the gateway.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_UPDATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // ...
    }
);

# EVENT_RECEIVE_SUBSCRIPTION_PAYMENT

Type
craft\commerce\events\SubscriptionPaymentEvent

The event that is triggered when a subscription payment is received.

use craft\commerce\events\SubscriptionPaymentEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\SubscriptionPayment;
use DateTime;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_RECEIVE_SUBSCRIPTION_PAYMENT,
    function(SubscriptionPaymentEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var SubscriptionPayment $payment
        $payment = $event->payment;
        // @var DateTime $until
        $until = $event->paidUntil;

        // Update loyalty reward data
        // ...
    }
);