Discounts

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

Discount service.

View source (opens new window)

# Public Properties

# allActiveDiscounts

Type
array (opens new window)
Access
Read-only
Since
2.2.14

View source (opens new window)

# allDiscounts

Type
craft\commerce\models\Discount[]
Access
Read-only

View source (opens new window)

# 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.
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.
className() (opens new window) Returns the fully qualified name of this class.
clearCouponUsageHistoryById() Clears a coupon's usage history.
deleteDiscountById() Delete a discount by its ID.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getAllActiveDiscounts() Get all currently active discounts
getAllDiscounts() Get all discounts.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getDiscountByCode() Returns an enabled discount by its code.
getDiscountById() Get a discount by its ID.
getDiscountsRelatedToPurchasable()
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.
matchLineItem() Match a line item against a discount.
matchOrder()
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
orderCompleteHandler() Updates discount uses counters.
orderCouponAvailable() Is discount coupon available to the order
populateDiscountRelations() Populates a discount's relations.
reorderDiscounts() Reorder discounts by an array of ids.
saveDiscount() Save a discount.
trigger() (opens new window) Triggers an event.

# clearCouponUsageHistoryById()

Clears a coupon's usage history.

View source (opens new window)

Arguments

# deleteDiscountById()

Delete a discount by its ID.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# getAllActiveDiscounts()

Since
2.2.14

Get all currently active discounts

View source (opens new window)

Arguments

Returns

array (opens new window)

Throws

# getAllDiscounts()

Get all discounts.

View source (opens new window)

Returns

craft\commerce\models\Discount[]

# getDiscountByCode()

Returns an enabled discount by its code.

View source (opens new window)

Arguments

Returns

craft\commerce\models\Discount, null (opens new window)

# getDiscountById()

Get a discount by its ID.

View source (opens new window)

Arguments

Returns

craft\commerce\models\Discount, null (opens new window)

# getDiscountsRelatedToPurchasable()

Since
2.2

View source (opens new window)

Arguments

Returns

array (opens new window)

# matchLineItem()

Match a line item against a discount.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# matchOrder()

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# orderCompleteHandler()

Updates discount uses counters.

View source (opens new window)

Arguments

# orderCouponAvailable()

Is discount coupon available to the order

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# populateDiscountRelations()

Populates a discount's relations.

View source (opens new window)

Arguments

# reorderDiscounts()

Reorder discounts by an array of ids.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# saveDiscount()

Save a discount.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

Throws

# Events

# EVENT_AFTER_DELETE_DISCOUNT

Type
craft\commerce\events\DiscountEvent

The event that is raised after an discount is deleted.

Plugins can get notified after an discount has been deleted.

use craft\commerce\events\DiscountEvent;
use craft\commerce\services\Discounts;
use yii\base\Event;

Event::on(Discounts::class, Discounts::EVENT_AFTER_DELETE_DISCOUNT, function(DiscountEvent $e) {
    // Do something - perhaps remove this discount from a payment gateway.
});

# EVENT_AFTER_SAVE_DISCOUNT

Type
craft\commerce\events\DiscountEvent

The event that is raised after an discount is saved.

Plugins can get notified after an discount has been saved

use craft\commerce\events\DiscountEvent;
use craft\commerce\services\Discounts;
use yii\base\Event;

Event::on(Discounts::class, Discounts::EVENT_AFTER_SAVE_DISCOUNT, function(DiscountEvent $e) {
    // Do something - perhaps set this discount as default in an external CRM system
});

# EVENT_BEFORE_MATCH_LINE_ITEM

Type
craft\commerce\events\MatchLineItemEvent

The event that is triggered when a line item is matched with a discount You may set craft\commerce\events\MatchLineItemEvent::$isValid (opens new window) to false to prevent the application of the matched discount.

Plugins can get notified before an item is removed from the cart.

use craft\commerce\events\MatchLineItemEvent;
use craft\commerce\services\Discounts;
use yii\base\Event;

Event::on(Discounts::class, Discounts::EVENT_BEFORE_MATCH_LINE_ITEM, function(MatchLineItemEvent $e) {
     // Maybe check some business rules and prevent a match from happening in some cases.
});

# EVENT_BEFORE_SAVE_DISCOUNT

Type
craft\commerce\events\DiscountEvent

The event that is raised before an discount is saved.

Plugins can get notified before an discount is being saved

use craft\commerce\events\DiscountEvent;
use craft\commerce\services\Discounts;
use yii\base\Event;

Event::on(Discounts::class, Discounts::EVENT_BEFORE_SAVE_DISCOUNT, function(DiscountEvent $e) {
    // Do something - perhaps let an external CRM system know about a client's new discount
});