class OrderPaidSubscriber in Commerce Invoice 8.2
Hierarchy
- class \Drupal\commerce_invoice\EventSubscriber\OrderPaidSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of OrderPaidSubscriber
1 string reference to 'OrderPaidSubscriber'
1 service uses OrderPaidSubscriber
File
- src/
EventSubscriber/ OrderPaidSubscriber.php, line 9
Namespace
Drupal\commerce_invoice\EventSubscriberView source
class OrderPaidSubscriber implements EventSubscriberInterface {
/**
* The invoice storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $invoiceStorage;
/**
* Constructs a new OrderPaidSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->invoiceStorage = $entity_type_manager
->getStorage('commerce_invoice');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
'commerce_order.order.paid' => 'onPaid',
];
return $events;
}
/**
* Updates the invoice total paid when an order is paid.
*
* @param \Drupal\commerce_order\Event\OrderEvent $event
* The order event.
*/
public function onPaid(OrderEvent $event) {
$order = $event
->getOrder();
$invoice_ids = $this->invoiceStorage
->getQuery()
->condition('state', 'pending')
->condition('orders', [
$order
->id(),
], 'IN')
->accessCheck(FALSE)
->execute();
// No pending invoice references the order being paid, aborting.
if (!$invoice_ids) {
return;
}
/** @var \Drupal\commerce_invoice\Entity\InvoiceInterface[] $invoices */
$invoices = $this->invoiceStorage
->loadMultiple($invoice_ids);
foreach ($invoices as $invoice) {
if ($invoice
->isPaid()) {
continue;
}
$total_paid = $invoice
->getTotalPaid();
$total_paid = $total_paid ? $total_paid
->add($order
->getTotalPaid()) : $order
->getTotalPaid();
$invoice
->setTotalPaid($total_paid);
$invoice
->save();
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderPaidSubscriber:: |
protected | property | The invoice storage. | |
OrderPaidSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
OrderPaidSubscriber:: |
public | function | Updates the invoice total paid when an order is paid. | |
OrderPaidSubscriber:: |
public | function | Constructs a new OrderPaidSubscriber object. |