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

Signature

public craft\commerce\models\Sale[] getAllSales ( )

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.
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.
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.
populateSaleRelations()Populates a sale's relations.
reorderSales()Reorder Sales based on a list of ids.
saveSale()Save a Sale.
trigger()Triggers an event.

deleteSaleById()

Delete a sale by its id.

View source

Arguments

  • $id

Returns

boolean

Throws

Signature

public boolean deleteSaleById ( $id )

getAllSales()

Signature

public craft\commerce\models\Sale[] getAllSales ( )

getSaleById()

Get a sale by its ID.

View source

Arguments

Returns

craft\commerce\models\Sale, null

Signature

public craft\commerce\models\Sale, null getSaleById ( $id )

getSalePriceForPurchasable()

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

View source

Arguments

Returns

float

Signature

public float getSalePriceForPurchasable ( craft\commerce\base\PurchasableInterface $purchasable, craft\commerce\elements\Order $order = null )

getSalesForPurchasable()

Returns the sales that match the purchasable.

View source

Arguments

Returns

craft\commerce\services\Sales[]

Signature

public craft\commerce\services\Sales[] getSalesForPurchasable ( craft\commerce\base\PurchasableInterface $purchasable, craft\commerce\elements\Order $order = null )

getSalesRelatedToPurchasable()

Signature

public array getSalesRelatedToPurchasable ( craft\commerce\base\PurchasableInterface $purchasable )

matchPurchasableAndSale()

Match a product and a sale and return the result.

View source

Arguments

Returns

boolean

Signature

public boolean matchPurchasableAndSale ( craft\commerce\base\PurchasableInterface $purchasable, craft\commerce\models\Sale $sale, craft\commerce\elements\Order $order = null )

populateSaleRelations()

Populates a sale's relations.

View source

Arguments

Signature

public void populateSaleRelations ( craft\commerce\models\Sale $sale )

reorderSales()

Reorder Sales based on a list of ids.

View source

Arguments

  • $ids

Returns

boolean

Signature

public boolean reorderSales ( $ids )

saveSale()

Save a Sale.

View source

Arguments

Returns

boolean

Throws

Signature

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

Events

EVENT_BEFORE_MATCH_PURCHASABLE_SALE

Type
craft\commerce\events\SaleMatchEvent

This event is raised after a sale has matched all other conditions You may set craft\commerce\events\SaleMatchEvent::$isValid to false to prevent the application of the matched sale.

Plugins can get notified when a purchasable matches a sale.

use craft\commerce\events\SaleMatchEvent;
use craft\commerce\services\Sales;
use yii\base\Event;

Event::on(Sales::class, Sales::EVENT_BEFORE_MATCH_PURCHASABLE_SALE, function(SaleMatchEvent $e) {
     // Perhaps prevent the purchasable match with sale based on some business logic.
});