You are here

public function OrderPlacedSubscriber::onPlace in Commerce Invoice 8.2

Generates an invoice when an order is placed if configured to do so.

Parameters

\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The event we subscribed to.

File

src/EventSubscriber/OrderPlacedSubscriber.php, line 45

Class

OrderPlacedSubscriber

Namespace

Drupal\commerce_invoice\EventSubscriber

Code

public function onPlace(WorkflowTransitionEvent $event) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event
    ->getEntity();

  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
  $order_type = OrderType::load($order
    ->bundle());
  $invoice_settings = $order_type
    ->getThirdPartySettings('commerce_invoice');

  // Check if invoices should be generated automatically when an order
  // is placed for this order type.
  if (empty($invoice_settings['invoice_type']) || empty($invoice_settings['order_placed_generation'])) {
    return;
  }
  $values = [
    'uid' => $order
      ->getCustomerId(),
    'type' => $invoice_settings['invoice_type'],
  ];
  $this->invoiceGenerator
    ->generate([
    $order,
  ], $order
    ->getStore(), $order
    ->getBillingProfile(), $values);
}