Sales ​
- Type
- Class
- Namespace
- craft\commerce\services
- Inherits
- craft\commerce\services\Sales » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 2.0
Sale service.
Public Properties ​
| Property | Description | 
|---|---|
| allSales | craft\commerce\models\Sale[] | 
| behaviors | yii\base\Behavior – List of behaviors attached to this component. | 
allSales ​
- Type
- craft\commerce\models\Sale[]
- 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. | 
| canUseSales() | |
| className() | Returns the fully qualified name of this class. | 
| deleteSaleById() | Delete a sale 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. | 
| getAllSales() | Get all sales. | 
| getBehavior() | Returns the named behavior object. | 
| getBehaviors() | Returns all behaviors attached to this component. | 
| getSaleById() | Get a sale by its ID. | 
| getSalePriceForPurchasable() | Returns the salePrice of the purchasable based on all the sales. | 
| getSalesForPurchasable() | Returns the sales that match the purchasable. | 
| getSalesRelatedToPurchasable() | |
| 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. | 
| matchPurchasableAndSale() | Match a product and a sale and return the result. | 
| off() | Detaches an existing event handler from this component. | 
| on() | Attaches an event handler to an event. | 
| reorderSales() | Reorder Sales based on a list of ids. | 
| saveSale() | Save a Sale. | 
| trigger() | Triggers an event. | 
canUseSales() ​
- Since
- 5.0.0
Returns ​
Throws ​
deleteSaleById() ​
Delete a sale by its id.
Arguments ​
- $id(integer)
Returns ​
Throws ​
getAllSales() ​
Get all sales.
Returns ​
getSaleById() ​
Get a sale by its ID.
Arguments ​
- $id
getSalePriceForPurchasable() ​
Returns the salePrice of the purchasable based on all the sales.
Arguments ​
- $purchasable
- $order(craft\commerce\elements\Order, null)
getSalesForPurchasable() ​
Returns the sales that match the purchasable.
Arguments ​
- $purchasable
- $order(craft\commerce\elements\Order, null)
Returns ​
Throws ​
getSalesRelatedToPurchasable() ​
Arguments ​
- $purchasable(craft\commerce\base\PurchasableInterface)
Returns ​
matchPurchasableAndSale() ​
Match a product and a sale and return the result.
Arguments ​
- $purchasable
- $sale
- $order(craft\commerce\elements\Order, null)
Throws ​
reorderSales() ​
Reorder Sales based on a list of ids.
Arguments ​
- $ids(integer[])
Returns ​
Throws ​
saveSale() ​
Save a Sale.
Arguments ​
- $model
- $runValidation(boolean) – Should we validate this before saving.
Throws ​
Events ​
EVENT_AFTER_DELETE_SALE ​
The event that is triggered after a sale is deleted.
php
use craft\commerce\events\SaleEvent;
use craft\commerce\services\Sales;
use craft\commerce\models\Sale;
use yii\base\Event;
Event::on(
    Sales::class,
    Sales::EVENT_AFTER_DELETE_SALE,
    function(SaleEvent $event) {
        // @var Sale $sale
        $sale = $event->sale;
        // do something
        // ...
    }
);EVENT_AFTER_SAVE_SALE ​
The event that is triggered after a sale is saved.
EVENT_BEFORE_MATCH_PURCHASABLE_SALE ​
The event that is triggered before Commerce attempts to match a sale to a purchasable.
The isValid event property can be set to false to prevent the application of the matched sale.
php
use craft\commerce\events\SaleMatchEvent;
use craft\commerce\services\Sales;
use craft\commerce\base\PurchasableInterface;
use craft\commerce\models\Sale;
use yii\base\Event;
Event::on(
    Sales::class,
    Sales::EVENT_BEFORE_MATCH_PURCHASABLE_SALE,
    function(SaleMatchEvent $event) {
        // @var Sale $sale
        $sale = $event->sale;
        // @var PurchasableInterface $purchasable
        $purchasable = $event->purchasable;
        // @var bool $isNew
        $isNew = $event->isNew;
        // Use custom business logic to exclude purchasable from sale
        // with `$event->isValid = false`
        // ...
    }
);EVENT_BEFORE_SAVE_SALE ​
The event that is triggered before a sale is saved.