LineItems
- Type
- Class
- Namespace
- craft\commerce\services
- Inherits
- craft\commerce\services\LineItems » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
- Implements
- yii\base\Configurable (opens new window)
- Since
- 2.0
Line item service.
View source (opens new window)
# Public Properties
Property | Description |
---|---|
behaviors (opens new window) | yii\base\Behavior (opens new window) – List of behaviors attached to this component. |
# 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. |
createLineItem() | Create a line item. |
deleteAllLineItemsByOrderId() | Deletes all line items associated with an order, per the order's ID. |
detachBehavior() (opens new window) | Detaches a behavior from the component. |
detachBehaviors() (opens new window) | Detaches all behaviors from the component. |
eagerLoadLineItemsForOrders() | |
ensureBehaviors() (opens new window) | Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component. |
getAllLineItemsByOrderId() | Returns an order's line items, per the order's ID. |
getBehavior() (opens new window) | Returns the named behavior object. |
getBehaviors() (opens new window) | Returns all behaviors attached to this component. |
getLineItemById() | Get a line item by its ID. |
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. |
off() (opens new window) | Detaches an existing event handler from this component. |
on() (opens new window) | Attaches an event handler to an event. |
orderCompleteHandler() | |
resolveLineItem() | Takes an order, a purchasable ID, options, and resolves it to a line item. |
saveLineItem() | Save a line item. |
trigger() (opens new window) | Triggers an event. |
# createLineItem()
Create a line item.
View source (opens new window)
Arguments
$order
(craft\commerce\elements\Order) – The order the line item is associated with$purchasableId
(integer (opens new window)) – The ID of the purchasable the line item represents$options
(array (opens new window)) – Options to set on the line item$qty
(integer (opens new window)) – The quantity to set on the line item$note
(string (opens new window)) – The note on the line item$uid
(string (opens new window), null (opens new window))
Throws
# deleteAllLineItemsByOrderId()
Deletes all line items associated with an order, per the order's ID.
View source (opens new window)
Arguments
$orderId
(integer (opens new window)) – The order's ID
Returns
boolean (opens new window) – Whether any line items were deleted
# eagerLoadLineItemsForOrders()
- Since
- 3.2.0
View source (opens new window)
Arguments
$orders
(array (opens new window), craft\commerce\elements\Order[])
Returns
craft\commerce\elements\Order[]
# getAllLineItemsByOrderId()
Returns an order's line items, per the order's ID.
View source (opens new window)
Arguments
$orderId
(integer (opens new window)) – The order's ID
Returns
craft\commerce\models\LineItem[] – An array of all the line items for the matched order.
# getLineItemById()
Get a line item by its ID.
View source (opens new window)
Arguments
$id
(integer (opens new window)) – The line item ID
Returns
craft\commerce\models\LineItem, null (opens new window) – Line item or null, if not found.
# orderCompleteHandler()
- Since
- 3.2.5
View source (opens new window)
Arguments
$lineItem
$order
Throws
# resolveLineItem()
Takes an order, a purchasable ID, options, and resolves it to a line item.
If a line item is found for that order ID with those exact options, that line item is returned. Otherwise, a new line item is returned.
View source (opens new window)
Arguments
$order
(craft\commerce\elements\Order)$purchasableId
(integer (opens new window)) – The purchasable's ID$options
(array (opens new window)) – Options for the line item
Returns
craft\commerce\models\LineItem
Throws
# saveLineItem()
Save a line item.
View source (opens new window)
Arguments
$lineItem
(craft\commerce\models\LineItem) – The line item to save.$runValidation
(boolean (opens new window)) – Whether the Line Item should be validated.
Throws
# Events
# EVENT_AFTER_SAVE_LINE_ITEM
The event that is triggered after a line item is saved.
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use craft\commerce\models\LineItem;
use yii\base\Event;
Event::on(
LineItems::class,
LineItems::EVENT_AFTER_SAVE_LINE_ITEM,
function(LineItemEvent $event) {
// @var LineItem $lineItem
$lineItem = $event->lineItem;
// @var bool $isNew
$isNew = $event->isNew;
// Reserve stock
// ...
}
);
# EVENT_BEFORE_SAVE_LINE_ITEM
The event that is triggered before a line item is saved.
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use craft\commerce\models\LineItem;
use yii\base\Event;
Event::on(
LineItems::class,
LineItems::EVENT_BEFORE_SAVE_LINE_ITEM,
function(LineItemEvent $event) {
// @var LineItem $lineItem
$lineItem = $event->lineItem;
// @var bool $isNew
$isNew = $event->isNew;
// Notify a third party service about changes to an order
// ...
}
);
# EVENT_CREATE_LINE_ITEM
The event that is triggered after a line item has been created from a purchasable.
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use craft\commerce\models\LineItem;
use yii\base\Event;
Event::on(
LineItems::class,
LineItems::EVENT_CREATE_LINE_ITEM,
function(LineItemEvent $event) {
// @var LineItem $lineItem
$lineItem = $event->lineItem;
// @var bool $isNew
$isNew = $event->isNew;
// Call a third party service based on the line item options
// ...
}
);
# EVENT_POPULATE_LINE_ITEM
The event that is triggered as a line item is being populated from a purchasable.
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use craft\commerce\models\LineItem;
use yii\base\Event;
Event::on(
LineItems::class,
LineItems::EVENT_POPULATE_LINE_ITEM,
function(LineItemEvent $event) {
// @var LineItem $lineItem
$lineItem = $event->lineItem;
// @var bool $isNew
$isNew = $event->isNew;
// Modify the price of a line item
// ...
}
);