Discounts ​
- Type
- Class
- Namespace
- craft\commerce\services
- Inherits
- craft\commerce\services\Discounts » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 2.0y
Discount service.
Public Properties ​
Property | Description |
---|---|
allActiveDiscounts | craft\commerce\models\Discount[] |
allDiscounts | array, craft\commerce\models\Discount[] |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
allActiveDiscounts
​
- Type
- craft\commerce\models\Discount[]
- Default value
null
- Access
- Read-only
- Since
- 2.2.14
allDiscounts
​
- Type
- array, craft\commerce\models\Discount[]
- Default value
null
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__clone() | This method is called after the object is created by cloning an existing one. |
__construct() | Constructor. |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() | Sets a component property to be null. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
behaviors() | Returns a list of behaviors that this component should behave as. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
clearCustomerUsageHistoryById() | |
clearDiscountUsesById() | Clear total discount uses |
clearEmailUsageHistoryById() | |
deleteDiscountById() | Delete a discount by its ID. |
detachBehavior() | Detaches a behavior from the component. |
detachBehaviors() | Detaches all behaviors from the component. |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
ensureSortOrder() | |
getAllActiveDiscounts() | Get all currently active discounts We pass the Order to attempt ot optimize the query to only possible discounts that might match, eliminating ones that definitely will not match. |
getAllDiscounts() | Get all discounts. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | Returns all behaviors attached to this component. |
getCustomerUsageStatsById() | User usage stats for discount |
getDiscountByCode() | Returns an enabled discount by its code, regardless of the discount's requireCouponCode value. |
getDiscountById() | Get a discount by its ID. |
getDiscountsRelatedToPurchasable() | |
getEmailUsageStatsById() | Email usage stats for discount |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined for this component. |
init() | Initializes the object. |
matchLineItem() | Match a line item against a discount. |
matchOrder() | |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
orderCompleteHandler() | Updates discount uses counters. |
orderCouponAvailable() | Is discount coupon available to the order |
reorderDiscounts() | Reorder discounts by an array of ids. |
saveDiscount() | Save a discount. |
trigger() | Triggers an event. |
clearCustomerUsageHistoryById()
​
- Since
- 4.0
Arguments ​
$id
Throws ​
clearDiscountUsesById()
​
- Since
- 3.0
Clear total discount uses
Arguments ​
$id
Throws ​
clearEmailUsageHistoryById()
​
- Since
- 3.0
Arguments ​
$id
Throws ​
deleteDiscountById()
​
Delete a discount by its ID.
Arguments ​
$id
Throws ​
ensureSortOrder()
​
- Since
- 4.4.0
Arguments ​
$storeId
Returns ​
void
Throws ​
getAllActiveDiscounts()
​
- Since
- 2.2.14
Get all currently active discounts We pass the Order to attempt ot optimize the query to only possible discounts that might match, eliminating ones that definitely will not match.
Arguments ​
$order
(craft\commerce\elements\Order, null)
Returns ​
craft\commerce\models\Discount[]
Throws ​
getAllDiscounts()
​
Get all discounts.
Arguments ​
Returns ​
\Illuminate\Support\Collection
Throws ​
getCustomerUsageStatsById()
​
User usage stats for discount
Arguments ​
$id
(integer)
Returns ​
array – In the format ['uses' => int, 'users' => int]
getDiscountByCode()
​
Returns an enabled discount by its code, regardless of the discount's requireCouponCode
value.
Arguments ​
$code
$storeId
Throws ​
getDiscountById()
​
Get a discount by its ID.
Arguments ​
Returns ​
craft\commerce\models\Discount, null
Throws ​
getDiscountsRelatedToPurchasable()
​
- Since
- 2.2
Arguments ​
$purchasable
getEmailUsageStatsById()
​
Email usage stats for discount
Arguments ​
$id
Returns ​
array – Return in the format ['uses' => int, 'emails' => int]
matchLineItem()
​
Match a line item against a discount.
Arguments ​
$lineItem
$discount
$matchOrder
Throws ​
matchOrder()
​
Arguments ​
$order
$discount
Throws ​
orderCompleteHandler()
​
Updates discount uses counters.
Arguments ​
$order
Throws ​
orderCouponAvailable()
​
Is discount coupon available to the order
Arguments ​
Throws ​
reorderDiscounts()
​
Reorder discounts by an array of ids.
Arguments ​
$ids
Throws ​
saveDiscount()
​
Save a discount.
Arguments ​
$model
(craft\commerce\models\Discount) – The discount being saved$runValidation
(boolean) – Should we validate this discount before saving.
Throws ​
Events ​
EVENT_AFTER_DELETE_DISCOUNT ​
The event that is triggered after a discount is deleted.
use craft\commerce\events\DiscountEvent;
use craft\commerce\services\Discounts;
use craft\commerce\models\Discount;
use yii\base\Event;
Event::on(
Discounts::class,
Discounts::EVENT_AFTER_DELETE_DISCOUNT,
function(DiscountEvent $event) {
// @var Discount $discount
$discount = $event->discount;
// Remove this discount from a payment gateway
// ...
}
);
EVENT_AFTER_SAVE_DISCOUNT ​
The event that is triggered after a discount is saved.
use craft\commerce\events\DiscountEvent;
use craft\commerce\services\Discounts;
use craft\commerce\models\Discount;
use yii\base\Event;
Event::on(
Discounts::class,
Discounts::EVENT_AFTER_SAVE_DISCOUNT,
function(DiscountEvent $event) {
// @var Discount $discount
$discount = $event->discount;
// @var bool $isNew
$isNew = $event->isNew;
// Set this discount as default in an external CRM
// ...
}
);
EVENT_BEFORE_SAVE_DISCOUNT ​
The event that is triggered before a discount is saved.
use craft\commerce\events\DiscountEvent;
use craft\commerce\services\Discounts;
use craft\commerce\models\Discount;
use yii\base\Event;
Event::on(
Discounts::class,
Discounts::EVENT_BEFORE_SAVE_DISCOUNT,
function(DiscountEvent $event) {
// @var Discount $discount
$discount = $event->discount;
// @var bool $isNew
$isNew = $event->isNew;
// Let an external CRM know about a client’s new discount
// ...
}
);
EVENT_DISCOUNT_MATCHES_LINE_ITEM ​
The event that is triggered when a line item is matched with a discount.
This event will be raised if all standard conditions are met. You may set the isValid
property to false
on the event to prevent the matching of the discount to the line item.
use craft\commerce\services\Discounts;
use craft\commerce\events\MatchLineItemEvent;
use craft\commerce\models\Discount;
use craft\commerce\models\LineItem;
use yii\base\Event;
Event::on(
Discounts::class,
Discounts::EVENT_DISCOUNT_MATCHES_LINE_ITEM,
function(MatchLineItemEvent $event) {
// @var LineItem $lineItem
$lineItem = $event->lineItem;
// @var Discount $discount
$discount = $event->discount;
// Check some business rules and prevent a match in special cases
// ...
}
);
EVENT_DISCOUNT_MATCHES_ORDER ​
The event that is triggered when an order is matched with a discount.
You may set the isValid
property to false
on the event to prevent the matching of the discount with the order.
use craft\commerce\services\Discounts;
use craft\commerce\events\MatchOrderEvent;
use craft\commerce\models\Discount;
use craft\commerce\elements\Order;
use yii\base\Event;
Event::on(
Discounts::class,
Discounts::EVENT_DISCOUNT_MATCHES_ORDER,
function(MatchOrderEvent $event) {
// @var Order $order
$order = $event->order;
// @var Discount $discount
$discount = $event->discount;
// Check some business rules and prevent a match in special cases
// ... $event->isValid = false; // set to false if you want it to NOT match as it would.
}
);