class PaymentOrderUpdater in Commerce Core 8.2
Hierarchy
- class \Drupal\commerce_payment\PaymentOrderUpdater implements PaymentOrderUpdaterInterface, DestructableInterface
Expanded class hierarchy of PaymentOrderUpdater
1 string reference to 'PaymentOrderUpdater'
- commerce_payment.services.yml in modules/
payment/ commerce_payment.services.yml - modules/payment/commerce_payment.services.yml
1 service uses PaymentOrderUpdater
File
- modules/
payment/ src/ PaymentOrderUpdater.php, line 10
Namespace
Drupal\commerce_paymentView source
class PaymentOrderUpdater implements PaymentOrderUpdaterInterface, DestructableInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The order IDs that need updating.
*
* @var int[]
*/
protected $updateList = [];
/**
* Constructs a new PaymentOrderUpdater object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function requestUpdate(OrderInterface $order) {
$this->updateList[$order
->id()] = $order
->id();
}
/**
* {@inheritdoc}
*/
public function needsUpdate(OrderInterface $order) {
return !$order
->isNew() && isset($this->updateList[$order
->id()]);
}
/**
* {@inheritdoc}
*/
public function updateOrders() {
if (!empty($this->updateList)) {
$order_storage = $this->entityTypeManager
->getStorage('commerce_order');
/** @var \Drupal\commerce_order\Entity\OrderInterface[] $orders */
$orders = $order_storage
->loadMultiple($this->updateList);
foreach ($orders as $order) {
$this
->updateOrder($order, TRUE);
}
}
}
/**
* {@inheritdoc}
*/
public function updateOrder(OrderInterface $order, $save_order = FALSE) {
$previous_total = $order
->getTotalPaid();
if (!$previous_total) {
// A NULL total indicates an order that doesn't have any items yet.
return;
}
// The new total is always calculated from scratch, to properly handle
// orders that were created before the total_paid field was introduced.
/** @var \Drupal\commerce_payment\PaymentStorageInterface $payment_storage */
$payment_storage = $this->entityTypeManager
->getStorage('commerce_payment');
$payments = $payment_storage
->loadMultipleByOrder($order);
/** @var \Drupal\commerce_price\Price $new_total */
$new_total = new Price('0', $previous_total
->getCurrencyCode());
foreach ($payments as $payment) {
if ($payment
->isCompleted()) {
$new_total = $new_total
->add($payment
->getBalance());
// Sync payment_gateway and payment_method fields from the first
// payment if the payment_method field is empty.
if ($order
->get('payment_method')
->isEmpty() && !$payment
->get('payment_method')
->isEmpty()) {
$order
->set('payment_gateway', $payment
->getPaymentGateway());
$order
->set('payment_method', $payment
->getPaymentMethod());
}
}
}
if (!$previous_total
->equals($new_total)) {
$order
->setTotalPaid($new_total);
if ($save_order) {
$order
->save();
}
}
unset($this->updateList[$order
->id()]);
}
/**
* {@inheritdoc}
*/
public function destruct() {
$this
->updateOrders();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PaymentOrderUpdater:: |
protected | property | The entity type manager. | |
PaymentOrderUpdater:: |
protected | property | The order IDs that need updating. | |
PaymentOrderUpdater:: |
public | function |
Performs destruct operations. Overrides DestructableInterface:: |
|
PaymentOrderUpdater:: |
public | function |
Checks whether the given order needs to be updated. Overrides PaymentOrderUpdaterInterface:: |
|
PaymentOrderUpdater:: |
public | function |
Requests an update of the given order. Overrides PaymentOrderUpdaterInterface:: |
|
PaymentOrderUpdater:: |
public | function |
Updates the given order. Overrides PaymentOrderUpdaterInterface:: |
|
PaymentOrderUpdater:: |
public | function |
Updates and saves all relevant orders. Overrides PaymentOrderUpdaterInterface:: |
|
PaymentOrderUpdater:: |
public | function | Constructs a new PaymentOrderUpdater object. |