public function OrderEventSubscriber::onOrderPlace in Commerce Stock 8
Creates a stock transaction when an order is placed.
Parameters
\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The order workflow event.
File
- src/
EventSubscriber/ OrderEventSubscriber.php, line 91
Class
- OrderEventSubscriber
- Performs stock transactions on order and order item events.
Namespace
Drupal\commerce_stock\EventSubscriberCode
public function onOrderPlace(WorkflowTransitionEvent $event) {
$eventType = $this
->getEventType('commerce_stock_order_place');
$order = $event
->getEntity();
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 = -1 * $item
->getQuantity();
$context = self::createContextFromOrder($order);
$location = $this->stockServiceManager
->getTransactionLocation($context, $entity, $quantity);
$transaction_type = StockTransactionsInterface::STOCK_SALE;
$this
->runTransactionEvent($eventType, $context, $entity, $quantity, $location, $transaction_type, $order);
}
}