You are here

public function OrderNumberSubscriber::setOrderNumber in Commerce Core 8.2

Sets the order number.

The number is generated using the number pattern specified by the order type. If no number pattern was specified, the order ID is used as a fallback.

Skipped if the order number has already been set.

Parameters

\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The transition event.

File

modules/order/src/EventSubscriber/OrderNumberSubscriber.php, line 59

Class

OrderNumberSubscriber
Generates the order number for placed orders.

Namespace

Drupal\commerce_order\EventSubscriber

Code

public function setOrderNumber(WorkflowTransitionEvent $event) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event
    ->getEntity();
  if (!$order
    ->getOrderNumber()) {
    $order_type_storage = $this->entityTypeManager
      ->getStorage('commerce_order_type');

    /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
    $order_type = $order_type_storage
      ->load($order
      ->bundle());

    /** @var \Drupal\commerce_number_pattern\Entity\NumberPatternInterface $number_pattern */
    $number_pattern = $order_type
      ->getNumberPattern();
    if ($number_pattern) {
      $order_number = $number_pattern
        ->getPlugin()
        ->generate($order);
    }
    else {
      $order_number = $order
        ->id();
    }
    $order
      ->setOrderNumber($order_number);
  }
}