InvoiceSubscriber.php in Commerce Invoice 8.2
File
src/EventSubscriber/InvoiceSubscriber.php
View source
<?php
namespace Drupal\commerce_invoice\EventSubscriber;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class InvoiceSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
$events = [
'commerce_invoice.pay.pre_transition' => [
'onPayPreTransition',
],
'commerce_invoice.pay.post_transition' => [
'onPayPostTransition',
],
];
return $events;
}
public function onPayPreTransition(WorkflowTransitionEvent $event) {
$invoice = $event
->getEntity();
if (!$invoice
->isPaid()) {
$invoice
->setTotalPaid($invoice
->getTotalPrice());
}
}
public function onPayPostTransition(WorkflowTransitionEvent $event) {
$invoice = $event
->getEntity();
foreach ($invoice
->getOrders() as $order) {
if ($order
->isPaid()) {
continue;
}
$order
->setTotalPaid($order
->getTotalPrice());
$order
->save();
}
}
}