Discounts

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Discounts » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Discount service.

View source

Public Properties

PropertyDescription
$allDiscountscraft\commerce\models\Discount[]
$behaviorsyii\base\Behavior – List of behaviors attached to this component

$allDiscounts

Signature

public craft\commerce\models\Discount[] getAllDiscounts ( )

Public Methods

MethodDescription
__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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
clearCouponUsageHistoryById()Clears a coupon's usage history.
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.
getAllDiscounts()Get all discounts.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getDiscountByCode()Returns an enabled discount by its code.
getDiscountById()Get a discount by its ID.
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.
init()Initializes the object.
matchLineItem()Match a line item against a discount.
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
populateDiscountRelations()Populates a discount's relations.
reorderDiscounts()Reorder discounts by an array of ids.
saveDiscount()Save a discount.
trigger()Triggers an event.

clearCouponUsageHistoryById()

Clears a coupon's usage history.

View source

Arguments

Signature

public void clearCouponUsageHistoryById ( \craft\commerce\services\int $id )

deleteDiscountById()

Delete a discount by its ID.

View source

Arguments

Returns

boolean

Signature

public boolean deleteDiscountById ( $id )

getAllDiscounts()

Signature

public craft\commerce\models\Discount[] getAllDiscounts ( )

getDiscountByCode()

Returns an enabled discount by its code.

View source

Arguments

Returns

craft\commerce\models\Discount, null

Signature

public craft\commerce\models\Discount, null getDiscountByCode ( $code )

getDiscountById()

Get a discount by its ID.

View source

Arguments

Returns

craft\commerce\models\Discount, null

Signature

public craft\commerce\models\Discount, null getDiscountById ( $id )

matchLineItem()

Match a line item against a discount.

View source

Arguments

Returns

boolean

Signature

public boolean matchLineItem ( craft\commerce\models\LineItem $lineItem, craft\commerce\models\Discount $discount )

orderCompleteHandler()

Updates discount uses counters.

View source

Arguments

Signature

public void orderCompleteHandler ( $order )

orderCouponAvailable()

Is discount coupon available to the order

View source

Arguments

Returns

boolean

Signature

public boolean orderCouponAvailable ( craft\commerce\elements\Order $order, \craft\commerce\services\string &$explanation = null )

populateDiscountRelations()

Populates a discount's relations.

View source

Arguments

Signature

public void populateDiscountRelations ( craft\commerce\models\Discount $discount )

reorderDiscounts()

Reorder discounts by an array of ids.

View source

Arguments

Returns

boolean

Signature

public boolean reorderDiscounts ( array $ids )

saveDiscount()

Save a discount.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean saveDiscount ( craft\commerce\models\Discount $model, \craft\commerce\services\bool $runValidation = true )

Events

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 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.
});