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
Property | Description |
---|---|
allActiveDiscounts | craft\commerce\models\Discount[] |
allDiscounts | array (opens new window), craft\commerce\models\Discount[] |
behaviors (opens new window) | yii\base\Behavior (opens new window) – List of behaviors attached to this component. |
# allActiveDiscounts
- Type
- craft\commerce\models\Discount[]
- Default value
null
- Access
- Read-only
- Since
- 2.2.14
View source (opens new window)
# allDiscounts
- Type
- array (opens new window), craft\commerce\models\Discount[]
- Default value
null
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 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. |
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. |
clearCustomerUsageHistoryById() | |
clearDiscountUsesById() | Clear total discount uses |
clearEmailUsageHistoryById() | |
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 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() (opens new window) | Returns the named behavior object. |
getBehaviors() (opens new window) | Returns all behaviors attached to this component. |
getCustomerUsageStatsById() | Customer usage stats for discount |
getDiscountByCode() | Returns an enabled discount by its code. |
getDiscountById() | Get a discount by its ID. |
getDiscountsRelatedToPurchasable() | |
getEmailUsageStatsById() | Email usage stats for discount |
getOrderConditionParams() | Return condition params for order discount condition formula |
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. |
isDiscountUserGroupValid() | |
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. |
# clearCustomerUsageHistoryById()
- Since
- 3.0
View source (opens new window)
Arguments
Throws
# clearDiscountUsesById()
- Since
- 3.0
Clear total discount uses
View source (opens new window)
Arguments
Throws
# clearEmailUsageHistoryById()
- Since
- 3.0
View source (opens new window)
Arguments
Throws
# deleteDiscountById()
Delete a discount by its ID.
View source (opens new window)
Arguments
Returns
# 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.
View source (opens new window)
Arguments
Returns
craft\commerce\models\Discount[]
Throws
# getAllDiscounts()
Get all discounts.
View source (opens new window)
Returns
craft\commerce\models\Discount[]
# getCustomerUsageStatsById()
Customer usage stats for discount
View source (opens new window)
Arguments
Returns
array (opens new window) – Return in the format ['uses' => int, 'customers' => int]
# getDiscountByCode()
Returns an enabled discount by its code.
View source (opens new window)
Arguments
$code
(string (opens new window))
Returns
craft\commerce\models\Discount, null (opens new window)
Throws
# 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
$purchasable
(craft\commerce\base\PurchasableInterface)
Returns
# getEmailUsageStatsById()
Email usage stats for discount
View source (opens new window)
Arguments
Returns
array (opens new window) – Return in the format ['uses' => int, 'emails' => int]
# getOrderConditionParams()
- Since
- 3.4.11
Return condition params for order discount condition formula
View source (opens new window)
Arguments
$order
(craft\commerce\elements\Order)
Returns
# isDiscountUserGroupValid()
View source (opens new window)
Arguments
$discount
(craft\commerce\models\Discount)$user
Returns
# matchLineItem()
Match a line item against a discount.
View source (opens new window)
Arguments
$lineItem
(craft\commerce\models\LineItem)$discount
(craft\commerce\models\Discount)$matchOrder
(boolean (opens new window))
Returns
# matchOrder()
View source (opens new window)
Arguments
$order
(craft\commerce\elements\Order)$discount
(craft\commerce\models\Discount)
Returns
Throws
# orderCompleteHandler()
Updates discount uses counters.
View source (opens new window)
Arguments
$order
(craft\commerce\elements\Order)
# orderCouponAvailable()
Is discount coupon available to the order
View source (opens new window)
Arguments
$order
(craft\commerce\elements\Order)$explanation
(string (opens new window), null (opens new window))
Returns
Throws
# populateDiscountRelations()
DEPRECATED
Deprecated in 3.2.4.
Populates a discount's relations.
View source (opens new window)
Arguments
$discount
(craft\commerce\models\Discount)
Throws
# reorderDiscounts()
Reorder discounts by an array of ids.
View source (opens new window)
Arguments
$ids
(array (opens new window))
Returns
# saveDiscount()
Save a discount.
View source (opens new window)
Arguments
$model
(craft\commerce\models\Discount) – The discount being saved$runValidation
(boolean (opens new window)) – Should we validate this discount before saving.
Returns
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_MATCH_LINE_ITEM
DEPRECATED
Deprecated in 3.2.4. Discounts::EVENT_BEFORE_MATCH_LINE_ITEM has been deprecated. Use Discounts::EVENT_DISCOUNT_MATCHES_LINE_ITEM instead.
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_BEFORE_MATCH_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_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(MatchLineOrder $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.
}
);