placeReservationsForSalesEvent = $placeReservationsForSalesEvent; $this->getSkusByProductIds = $getSkusByProductIds; $this->salesEventFactory = $salesEventFactory; $this->itemsToSellFactory = $itemsToSellFactory; $this->getProductTypesBySkus = $getProductTypesBySkus; $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType; $this->appendReservations = $appendReservations; $this->reservationExecution = $reservationExecution; } /** * Add inventory reservation before placing synchronous order or if stock reservation is deferred. * * @param OrderManagementInterface $subject * @param callable $proceed * @param OrderInterface $order * @return OrderInterface * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundPlace( OrderManagementInterface $subject, callable $proceed, OrderInterface $order ): OrderInterface { if ($this->reservationExecution->isDeferred()) { $itemsById = $itemsBySku = $itemsToSell = []; foreach ($order->getItems() as $item) { if (!isset($itemsById[$item->getProductId()])) { $itemsById[$item->getProductId()] = 0; } $itemsById[$item->getProductId()] += $item->getQtyOrdered(); } $productSkus = $this->getSkusByProductIds->execute(array_keys($itemsById)); $productTypes = $this->getProductTypesBySkus->execute($productSkus); foreach ($productSkus as $productId => $sku) { if (false === $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$sku])) { continue; } $itemsBySku[$sku] = (float)$itemsById[$productId]; $itemsToSell[] = $this->itemsToSellFactory->create([ 'sku' => $sku, 'qty' => -(float)$itemsById[$productId] ]); } $websiteId = (int)$order->getStore()->getWebsiteId(); [$salesChannel, $salesEventExtension] = $this->appendReservations->reserve($websiteId, $itemsBySku, $order, $itemsToSell); $order = $this->createOrder($proceed, $order, $itemsToSell, $salesChannel, $salesEventExtension); } else { $order = $proceed($order); } return $order; } /** * Create new Order * * In case of error during order placement exception add compensation * * @param callable $proceed * @param OrderInterface $order * @param array $itemsToSell * @param SalesChannelInterface $salesChannel * @param SalesEventExtensionInterface $salesEventExtension * @return OrderInterface * @throws \Magento\Framework\Exception\CouldNotSaveException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\LocalizedException */ private function createOrder($proceed, $order, $itemsToSell, $salesChannel, $salesEventExtension) { try { $order = $proceed($order); } catch (\Exception $e) { //add compensation foreach ($itemsToSell as $item) { $item->setQuantity(-(float)$item->getQuantity()); } /** @var SalesEventInterface $salesEvent */ $salesEvent = $this->salesEventFactory->create([ 'type' => SalesEventInterface::EVENT_ORDER_PLACE_FAILED, 'objectType' => SalesEventInterface::OBJECT_TYPE_ORDER, 'objectId' => (string)$order->getEntityId() ]); $salesEvent->setExtensionAttributes($salesEventExtension); $this->placeReservationsForSalesEvent->execute($itemsToSell, $salesChannel, $salesEvent); throw $e; } return $order; } }