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 an object property. |
__isset() (opens new window) | Checks if a property is set, i.e. defined and not null. |
__set() (opens new window) | Sets value of an object property. |
__unset() (opens new window) | Sets an object property to 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. |
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. |
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. |
resolveLineItem() | Takes an order ID, 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
$orderId
(integer (opens new window)) – The order ID 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
Returns
craft\commerce\models\LineItem
Throws
- yii\base\InvalidArgumentException (opens new window)
if the purchasable ID is not valid
# 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
# 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.
# resolveLineItem()
Takes an order ID, 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
$orderId
(integer (opens new window))$purchasableId
(integer (opens new window)) – The purchasable's ID$options
(array (opens new window)) – Options for the line item
Returns
craft\commerce\models\LineItem
# 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.
Returns
Throws
# Events
# EVENT_AFTER_SAVE_LINE_ITEM
The event that is raised after a line item is saved.
Plugins can get notified after a line item is being saved
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use yii\base\Event;
Event::on(LineItems::class, LineItems::EVENT_AFTER_SAVE_LINE_ITEM, function(LineItemEvent $e) {
// Do something - perhaps reserve the stock
});
# EVENT_BEFORE_SAVE_LINE_ITEM
The event that is raised before a line item is saved.
Plugins can get notified before a line item is being saved
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use yii\base\Event;
Event::on(LineItems::class, LineItems::EVENT_BEFORE_SAVE_LINE_ITEM, function(LineItemEvent $e) {
// Do something - perhaps let a 3rd party service know about changes to an order
});
# EVENT_CREATE_LINE_ITEM
This event is raised when a new line item is created from a purchasable
# EVENT_POPULATE_LINE_ITEM
This event is raised when a new line item is populated from a purchasable