You are here

public function CartExpiration::processItem in Commerce Core 8.2

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

modules/cart/src/Plugin/QueueWorker/CartExpiration.php, line 71

Class

CartExpiration
Deletes expired carts.

Namespace

Drupal\commerce_cart\Plugin\QueueWorker

Code

public function processItem($data) {
  $orders = [];
  foreach ($data as $order_id) {

    // Skip the OrderRefresh process to keep the changed timestamp intact.

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $this->orderStorage
      ->loadUnchanged($order_id);
    if (!$order) {
      continue;
    }

    /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
    $order_type = $this->orderTypeStorage
      ->load($order
      ->bundle());
    $cart_expiration = $order_type
      ->getThirdPartySetting('commerce_cart', 'cart_expiration');

    // Confirm that cart expiration has not been disabled after queueing.
    if (empty($cart_expiration)) {
      continue;
    }
    $current_date = new DrupalDateTime('now');
    $interval = new Interval($cart_expiration['number'], $cart_expiration['unit']);
    $expiration_date = $interval
      ->subtract($current_date);
    $expiration_timestamp = $expiration_date
      ->getTimestamp();

    // Make sure that the cart order still qualifies for expiration.
    if ($order
      ->get('cart')->value && $order
      ->getChangedTime() <= $expiration_timestamp) {
      $orders[] = $order;
    }
  }
  $this->orderStorage
    ->delete($orders);
}