LineItems

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\LineItems » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Line item service.

View source

Public Properties

PropertyDescription
$behaviorsyii\base\Behavior – List of behaviors attached to this component

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.
createLineItem()Create a line item.
deleteAllLineItemsByOrderId()Deletes all line items associated with an order, per the order's 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.
getAllLineItemsByOrderId()Returns an order's line items, per the order's ID.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getLineItemById()Get a line item by its ID.
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.
off()Detaches an existing event handler from this component.
on()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()Triggers an event.

createLineItem()

Create a line item.

View source

Arguments

  • $orderId (integer) – The order ID the line item is associated with
  • $purchasableId (integer) – The ID of the purchasable the line item represents
  • $options (array) – Options to set on the line item
  • $qty (integer) – The quantity to set on the line item
  • $note (string) – The note on the line item

Returns

craft\commerce\models\LineItem

Throws

Signature

public craft\commerce\models\LineItem createLineItem ( \craft\commerce\services\int $orderId, \craft\commerce\services\int $purchasableId, array $options, \craft\commerce\services\int $qty = 1, \craft\commerce\services\string $note = '' )

deleteAllLineItemsByOrderId()

Deletes all line items associated with an order, per the order's ID.

View source

Arguments

  • $orderId (integer) – The order's ID

Returns

boolean – Whether any line items were deleted

Signature

public boolean deleteAllLineItemsByOrderId ( \craft\commerce\services\int $orderId )

getAllLineItemsByOrderId()

Returns an order's line items, per the order's ID.

View source

Arguments

  • $orderId (integer) – The order's ID

Returns

craft\commerce\models\LineItem[] – An array of all the line items for the matched order.

Signature

public craft\commerce\models\LineItem[] getAllLineItemsByOrderId ( \craft\commerce\services\int $orderId )

getLineItemById()

Get a line item by its ID.

View source

Arguments

Returns

craft\commerce\models\LineItem, null – Line item or null, if not found.

Signature

public craft\commerce\models\LineItem, null getLineItemById ( $id )

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

Arguments

  • $orderId (integer)
  • $purchasableId (integer) – The purchasable's ID
  • $options (array) – Options for the line item

Returns

craft\commerce\models\LineItem

Signature

public craft\commerce\models\LineItem resolveLineItem ( \craft\commerce\services\int $orderId, \craft\commerce\services\int $purchasableId, array $options = [] )

saveLineItem()

Save a line item.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean saveLineItem ( craft\commerce\models\LineItem $lineItem, \craft\commerce\services\bool $runValidation = true )

Events

EVENT_AFTER_SAVE_LINE_ITEM

Type
craft\commerce\events\LineItemEvent

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_DEFAULT_ORDER_STATUS, function(LineItemEvent $e) {
    // Do something - perhaps reserve the stock
});

EVENT_BEFORE_SAVE_LINE_ITEM

Type
craft\commerce\events\LineItemEvent

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_DEFAULT_ORDER_STATUS, function(LineItemEvent $e) {
    // Do something - perhaps let a 3rd party service know about changes to an order
});

EVENT_CREATE_LINE_ITEM

Type
craft\commerce\events\LineItemEvent

This event is raised when a new line item is created from a purchasable

EVENT_POPULATE_LINE_ITEM

Type
craft\commerce\events\LineItemEvent

This event is raised when a new line item is populated from a purchasable