Skip to content

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.

View source

Public Properties ​

PropertyDescription
allSalescraft\commerce\models\Sale[]
behaviorsyii\base\Behavior – List of behaviors attached to this component.

allSales ​

Type
craft\commerce\models\Sale[]
Default value
null

View source

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 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

View source

Returns ​

boolean

Throws ​

deleteSaleById() ​

Delete a sale by its id.

View source

Arguments ​

Returns ​

boolean

Throws ​

getAllSales() ​

Get all sales.

View source

Returns ​

craft\commerce\models\Sale[]

getSaleById() ​

Get a sale by its ID.

View source

Arguments ​

  • $id

getSalePriceForPurchasable() ​

Returns the salePrice of the purchasable based on all the sales.

View source

Arguments ​

getSalesForPurchasable() ​

Returns the sales that match the purchasable.

View source

Arguments ​

Returns ​

craft\commerce\models\Sale[]

Throws ​

getSalesRelatedToPurchasable() ​

View source

Arguments ​

Returns ​

array

matchPurchasableAndSale() ​

Match a product and a sale and return the result.

View source

Arguments ​

Throws ​

reorderSales() ​

Reorder Sales based on a list of ids.

View source

Arguments ​

Returns ​

boolean

Throws ​

saveSale() ​

Save a Sale.

View source

Arguments ​

  • $model
  • $runValidation (boolean) – Should we validate this before saving.

Throws ​

Events ​

EVENT_AFTER_DELETE_SALE ​

Type
craft\commerce\events\SaleEvent

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 ​

Type
craft\commerce\events\SaleEvent

The event that is triggered after a sale is saved.


EVENT_BEFORE_MATCH_PURCHASABLE_SALE ​

Type
craft\commerce\events\SaleMatchEvent

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 ​

Type
craft\commerce\events\SaleEvent

The event that is triggered before a sale is saved.