You are here

protected function OrderSubscriber::getLicensableOrderItems in Commerce License 8.2

Returns the order items from an order which are for licensed products.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order entity.

Return value

\Drupal\commerce_order\Entity\OrderItemInterface[] An array of the order items whose purchased products are for licenses.

3 calls to OrderSubscriber::getLicensableOrderItems()
OrderSubscriber::onCancel in src/EventSubscriber/OrderSubscriber.php
Reacts to an order being cancelled.
OrderSubscriber::onPaid in src/EventSubscriber/OrderSubscriber.php
Activates the licenses on order paid.
OrderSubscriber::onPlace in src/EventSubscriber/OrderSubscriber.php
Reacts to an order being placed.

File

src/EventSubscriber/OrderSubscriber.php, line 148

Class

OrderSubscriber
Changes a license's state in sync with an order's workflow.

Namespace

Drupal\commerce_license\EventSubscriber

Code

protected function getLicensableOrderItems(OrderInterface $order) {
  $order_items = [];
  foreach ($order
    ->getItems() as $order_item) {

    // Skip order items that do not have a license reference field.
    // We check order items rather than the purchased entity to allow products
    // with licenses to be purchased without the checkout flow triggering
    // our synchronization. This is for cases such as recurring orders, where
    // the license entity should not be put through the normal workflow.
    // Checking the order item's bundle for our entity trait is expensive, as
    // it requires loading the bundle entity to call hasTrait() on it.
    // For now, just check whether the order item has our trait's field on it.
    // @see https://www.drupal.org/node/2894805
    if (!$order_item
      ->hasField('license')) {
      continue;
    }
    $purchased_entity = $order_item
      ->getPurchasedEntity();

    // This order item isn't "licensable" if the purchased entity it
    // references isn't properly configured.
    if (!$purchased_entity
      ->hasField('license_type') || $purchased_entity
      ->get('license_type')
      ->isEmpty()) {
      continue;
    }
    $order_items[] = $order_item;
  }
  return $order_items;
}