class CartExpiration in Commerce Core 8.2
Deletes expired carts.
Plugin annotation
@QueueWorker(
id = "commerce_cart_expiration",
title = @Translation("Cart expiration"),
cron = {"time" = 30}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
- class \Drupal\commerce_cart\Plugin\QueueWorker\CartExpiration implements ContainerFactoryPluginInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
Expanded class hierarchy of CartExpiration
File
- modules/
cart/ src/ Plugin/ QueueWorker/ CartExpiration.php, line 21
Namespace
Drupal\commerce_cart\Plugin\QueueWorkerView source
class CartExpiration extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* The order storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $orderStorage;
/**
* The order type storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $orderTypeStorage;
/**
* Constructs a new CartExpiration object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->orderStorage = $entity_type_manager
->getStorage('commerce_order');
$this->orderTypeStorage = $entity_type_manager
->getStorage('commerce_order_type');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CartExpiration:: |
protected | property | The order storage. | |
CartExpiration:: |
protected | property | The order type storage. | |
CartExpiration:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
CartExpiration:: |
public | function |
Works on a single queue item. Overrides QueueWorkerInterface:: |
|
CartExpiration:: |
public | function |
Constructs a new CartExpiration object. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |