public function OrderEventSubscriber::onOrderCancel in Commerce Stock 8
Performs a stock transaction for an order Cancel event.
Parameters
\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The order workflow event.
File
- src/
EventSubscriber/ OrderEventSubscriber.php, line 166
Class
- OrderEventSubscriber
- Performs stock transactions on order and order item events.
Namespace
Drupal\commerce_stock\EventSubscriberCode
public function onOrderCancel(WorkflowTransitionEvent $event) {
$eventType = $this
->getEventType('commerce_stock_order_cancel');
$order = $event
->getEntity();
$original_order = $this
->getOriginalEntity($order);
if ($original_order && $original_order
->getState()->value === 'draft') {
return;
}
foreach ($order
->getItems() as $item) {
$entity = $item
->getPurchasedEntity();
if (!$entity) {
continue;
}
$service = $this->stockServiceManager
->getService($entity);
$checker = $service
->getStockChecker();
// If always in stock then no need to create a transaction.
if ($checker
->getIsAlwaysInStock($entity)) {
continue;
}
$quantity = $item
->getQuantity();
$context = self::createContextFromOrder($order);
$location = $this->stockServiceManager
->getTransactionLocation($context, $entity, $quantity);
$transaction_type = StockTransactionsInterface::STOCK_RETURN;
$this
->runTransactionEvent($eventType, $context, $entity, $quantity, $location, $transaction_type, $order);
}
}