public function OrderEventSubscriber::onOrderItemDelete in Commerce Stock 8
Performs a stock transaction when an order item is deleted.
Parameters
\Drupal\commerce_order\Event\OrderItemEvent $event: The order item event.
File
- src/
EventSubscriber/ OrderEventSubscriber.php, line 278
Class
- OrderEventSubscriber
- Performs stock transactions on order and order item events.
Namespace
Drupal\commerce_stock\EventSubscriberCode
public function onOrderItemDelete(OrderItemEvent $event) {
$eventType = $this
->getEventType('commerce_stock_order_item_delete');
$item = $event
->getOrderItem();
$order = $item
->getOrder();
if ($order && !in_array($order
->getState()->value, [
'draft',
'canceled',
])) {
if ($order
->getState()
->getWorkflow()
->getGroup() !== 'commerce_order') {
return;
}
$entity = $item
->getPurchasedEntity();
if (!$entity) {
return;
}
/** @var \Drupal\commerce_stock\StockServiceInterface $service */
$service = $this->stockServiceManager
->getService($entity);
$checker = $service
->getStockChecker();
// If always in stock then no need to create a transaction.
if ($checker
->getIsAlwaysInStock($entity)) {
return;
}
$context = self::createContextFromOrder($order);
$location = $this->stockServiceManager
->getTransactionLocation($context, $entity, $item
->getQuantity());
$transaction_type = StockTransactionsInterface::STOCK_RETURN;
$this
->runTransactionEvent($eventType, $context, $entity, $item
->getQuantity(), $location, $transaction_type, $order);
}
}