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 an object property. |
__isset() (opens new window) | Checks if a property is set, i.e. defined and not null. |
__set() (opens new window) | Sets value of an object property. |
__unset() (opens new window) | Sets an object property to 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. |
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. |
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. |
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() | Prune a deleted field from subscription field layouts. |
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
$event
(craft\events\ModelEvent (opens new window)) – The event.
# cancelSubscription()
Cancel a subscription.
View source (opens new window)
Arguments
$subscription
(craft\commerce\elements\Subscription)$parameters
(craft\commerce\models\subscriptions\CancelSubscriptionForm)
Returns
Throws
- yii\base\InvalidConfigException (opens new window)
if the gateway does not support subscriptions - craft\commerce\errors\SubscriptionException
if something went wrong when canceling subscription
# createSubscription()
Subscribe a user to a subscription plan.
View source (opens new window)
Arguments
$user
(craft\elements\User (opens new window)) – The user subscribing to a plan$plan
(craft\commerce\base\Plan) – The plan the user is being subscribed to$parameters
(craft\commerce\models\subscriptions\SubscriptionForm) – Array of additional parameters to use$fieldValues
(array (opens new window)) – Array of content field values to set
Returns
craft\commerce\elements\Subscription – The subscription
Throws
- yii\base\InvalidConfigException (opens new window)
if the gateway does not support subscriptions - craft\commerce\errors\SubscriptionException
if something went wrong during subscription
# doesUserHaveAnySubscriptions()
Return true if the user has any subscriptions at all, even expired ones.
View source (opens new window)
Arguments
$userId
(integer (opens new window))
Returns
# expireSubscription()
Expire a subscription.
View source (opens new window)
Arguments
$subscription
(craft\commerce\elements\Subscription) – Subscription to expire$dateTime
(DateTime (opens new window)) – Expiry date time
Returns
boolean (opens new window) – Whether successfully expired subscription
Throws
- Throwable (opens new window)
if cannot expire subscription
# getSubscriptionCountForPlanById()
Returns subscription count for a plan.
View source (opens new window)
Arguments
$planId
(integer (opens new window))
Returns
# handleChangedFieldLayout()
Handle field layout change
View source (opens new window)
Arguments
# handleDeletedFieldLayout()
Handle field layout being deleted
View source (opens new window)
Arguments
# pruneDeletedField()
Prune a deleted field from subscription field layouts.
View source (opens new window)
Arguments
# reactivateSubscription()
Reactivate a subscription.
View source (opens new window)
Arguments
$subscription
(craft\commerce\elements\Subscription)
Returns
Throws
- yii\base\InvalidConfigException (opens new window)
if the gateway does not support subscriptions - Throwable (opens new window)
- craft\errors\ElementNotFoundException (opens new window)
- yii\base\Exception (opens new window)
# receivePayment()
Receive a payment for a subscription
View source (opens new window)
Arguments
$subscription
(craft\commerce\elements\Subscription)$payment
(craft\commerce\models\subscriptions\SubscriptionPayment)$paidUntil
(DateTime (opens new window))
Returns
Throws
- Throwable (opens new window)
- craft\errors\ElementNotFoundException (opens new window)
- yii\base\Exception (opens new window)
# switchSubscriptionPlan()
Switch a subscription to a different subscription plan.
View source (opens new window)
Arguments
$subscription
(craft\commerce\elements\Subscription) – The subscription to modify$plan
(craft\commerce\base\Plan) – The plan to change the subscription to$parameters
(craft\commerce\models\subscriptions\SwitchPlansForm) – Additional parameters to use
Returns
Throws
# updateSubscription()
Update a subscription.
View source (opens new window)
Arguments
$subscription
(craft\commerce\elements\Subscription)
Returns
Throws
- Throwable (opens new window)
- craft\errors\ElementNotFoundException (opens new window)
- yii\base\Exception (opens new window)
# Constants
Constant | Description |
---|---|
CONFIG_FIELDLAYOUT_KEY |
# Events
# EVENT_AFTER_CANCEL_SUBSCRIPTION
The event that is triggered after a subscription is canceled
Plugins can get notified after a subscription gets canceled.
use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_CANCEL_SUBSCRIPTION, function(CancelSubscriptionEvent $e) {
// Do something - maybe refund the user for the remainder of the subscription.
});
# EVENT_AFTER_CREATE_SUBSCRIPTION
The event that is triggered after a subscription is created
Plugins can get notified after a subscription is created.
use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_CREATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
// Do something about it - perhaps make a call to 3rd party service to authorize a user
});
# EVENT_AFTER_EXPIRE_SUBSCRIPTION
The event that is triggered when a subscription is expired.
Plugins can get notified when a subscription is being expired.
use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_EXPIRE_SUBSCRIPTION, function(SubscriptionEvent $e) {
// Do something about it - perhaps make a call to 3rd party service to de-authorize a user.
});
# EVENT_AFTER_REACTIVATE_SUBSCRIPTION
The event that is triggered after a subscription is reactivated
Plugins can get notified before a subscription gets reactivated.
use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_REACTIVATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
// Do something - maybe the user needs to be re-authorized with a 3rd party service.
});
# EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN
The event that is triggered after a subscription is switched to a different plan
Plugins can get notified after a subscription gets switched to a different plan.
use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN, function(SubscriptionSwitchPlansEvent $e) {
// Do something - maybe the user needs their permissions adjusted on a 3rd party service.
});
# EVENT_BEFORE_CANCEL_SUBSCRIPTION
The event that is triggered before a subscription is canceled
You may set craft\commerce\events\CancelSubscriptionEvent::$isValid (opens new window) to false
to prevent the subscription from being canceled
Plugins can get notified before a subscription is canceled.
use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_CANCEL_SUBSCRIPTION, function(CancelSubscriptionEvent $e) {
// Do something - maybe the user is not permitted to cancel the subscription for some reason.
});
# EVENT_BEFORE_CREATE_SUBSCRIPTION
The event that is triggered before a subscription is created.
You may set craft\commerce\events\CreateSubscriptionEvent::$isValid (opens new window) to false
to prevent the user from being subscribed to the plan.
Plugins can get notified before a subscription is created.
use craft\commerce\events\CreateSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_CREATE_SUBSCRIPTION, function(CreateSubscriptionEvent $e) {
// Do something - perhaps check if the user is eligible for the parameters set and prevent the subscription if not.
});
# EVENT_BEFORE_REACTIVATE_SUBSCRIPTION
The event that is triggered before a subscription is reactivated
You may set craft\commerce\events\SubscriptionEvent::$isValid (opens new window) to false
to prevent the subscription from being reactivated
Plugins can get notified before a subscription gets reactivated.
use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_REACTIVATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
// Do something - maybe the user does not qualify for reactivation due to some business logic.
});
# EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN
The event that is triggered before a plan switch happens for a subscription
You may set craft\commerce\events\SubscriptionSwitchPlansEvent::$isValid (opens new window) to false
to prevent the switch from happening
Plugins can get notified before a subscription is switched to a different plan.
use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN, function(SubscriptionSwitchPlansEvent $e) {
// Do something - maybe the user is not permitted to switch to that plan due to some business logic.
});
# EVENT_BEFORE_UPDATE_SUBSCRIPTION
The event that is triggered after an existing
Plugins can get notified 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 yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_UPDATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
// Do something - maybe refund the user for the remainder of the subscription.
});
# EVENT_RECEIVE_SUBSCRIPTION_PAYMENT
The event that is triggered after receiving a subscription payment
Plugins can get notified when a subscription payment is received.
use craft\commerce\events\SubscriptionPaymentEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;
Event::on(Subscriptions::class, Subscriptions::EVENT_RECEIVE_SUBSCRIPTION_PAYMENT, function(SubscriptionPaymentEvent $e) {
// Do something - perhaps update the loyalty reward data.
});
← States TaxCategories →