You are here

class OrderPlacedSubscriber in Commerce Invoice 8.2

Hierarchy

  • class \Drupal\commerce_invoice\EventSubscriber\OrderPlacedSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OrderPlacedSubscriber

1 string reference to 'OrderPlacedSubscriber'
commerce_invoice.services.yml in ./commerce_invoice.services.yml
commerce_invoice.services.yml
1 service uses OrderPlacedSubscriber
commerce_invoice.order_placed_subscriber in ./commerce_invoice.services.yml
Drupal\commerce_invoice\EventSubscriber\OrderPlacedSubscriber

File

src/EventSubscriber/OrderPlacedSubscriber.php, line 10

Namespace

Drupal\commerce_invoice\EventSubscriber
View source
class OrderPlacedSubscriber implements EventSubscriberInterface {

  /**
   * The invoice generator.
   *
   * @var \Drupal\commerce_invoice\InvoiceGeneratorInterface
   */
  protected $invoiceGenerator;

  /**
   * Constructs a new OrderPlacedSubscriber object.
   *
   * @param \Drupal\commerce_invoice\InvoiceGeneratorInterface $invoice_generator
   *   The invoice generator.
   */
  public function __construct(InvoiceGeneratorInterface $invoice_generator) {
    $this->invoiceGenerator = $invoice_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_order.place.post_transition' => [
        'onPlace',
      ],
    ];
    return $events;
  }

  /**
   * Generates an invoice when an order is placed if configured to do so.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The event we subscribed to.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderPlacedSubscriber::$invoiceGenerator protected property The invoice generator.
OrderPlacedSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OrderPlacedSubscriber::onPlace public function Generates an invoice when an order is placed if configured to do so.
OrderPlacedSubscriber::__construct public function Constructs a new OrderPlacedSubscriber object.