placeReservationsForSalesEvent = $placeReservationsForSalesEvent; $this->getSkusByProductIds = $getSkusByProductIds; $this->websiteRepository = $websiteRepository; $this->salesChannelFactory = $salesChannelFactory; $this->salesEventFactory = $salesEventFactory; $this->itemsToSellFactory = $itemsToSellFactory; $this->checkItemsQuantity = $checkItemsQuantity; $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver; $this->getProductTypesBySkus = $getProductTypesBySkus; $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType; } /** * @param OrderManagementInterface $subject * @param OrderInterface $order * @return OrderInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterPlace(OrderManagementInterface $subject, OrderInterface $order) : OrderInterface { $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(); $websiteCode = $this->websiteRepository->getById($websiteId)->getCode(); $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId(); $this->checkItemsQuantity->execute($itemsBySku, $stockId); /** @var SalesEventInterface $salesEvent */ $salesEvent = $this->salesEventFactory->create([ 'type' => SalesEventInterface::EVENT_ORDER_PLACED, 'objectType' => SalesEventInterface::OBJECT_TYPE_ORDER, 'objectId' => (string)$order->getEntityId() ]); $salesChannel = $this->salesChannelFactory->create([ 'data' => [ 'type' => SalesChannelInterface::TYPE_WEBSITE, 'code' => $websiteCode ] ]); $this->placeReservationsForSalesEvent->execute($itemsToSell, $salesChannel, $salesEvent); return $order; } }