Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AppendReservationsAfterOrderPlacementPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 use Magento\InventorySalesApi\Api\Data\SalesEventInterfaceFactory;
19 use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
21 use Magento\InventorySalesApi\Api\Data\ItemToSellInterfaceFactory;
24 
26 {
30  private $placeReservationsForSalesEvent;
31 
35  private $getSkusByProductIds;
36 
40  private $websiteRepository;
41 
45  private $salesChannelFactory;
46 
50  private $salesEventFactory;
51 
55  private $itemsToSellFactory;
56 
60  private $checkItemsQuantity;
61 
65  private $stockByWebsiteIdResolver;
66 
70  private $getProductTypesBySkus;
71 
75  private $isSourceItemManagementAllowedForProductType;
76 
89  public function __construct(
90  PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent,
91  GetSkusByProductIdsInterface $getSkusByProductIds,
92  WebsiteRepositoryInterface $websiteRepository,
93  SalesChannelInterfaceFactory $salesChannelFactory,
94  SalesEventInterfaceFactory $salesEventFactory,
95  ItemToSellInterfaceFactory $itemsToSellFactory,
96  CheckItemsQuantity $checkItemsQuantity,
97  StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
98  GetProductTypesBySkusInterface $getProductTypesBySkus,
99  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
100  ) {
101  $this->placeReservationsForSalesEvent = $placeReservationsForSalesEvent;
102  $this->getSkusByProductIds = $getSkusByProductIds;
103  $this->websiteRepository = $websiteRepository;
104  $this->salesChannelFactory = $salesChannelFactory;
105  $this->salesEventFactory = $salesEventFactory;
106  $this->itemsToSellFactory = $itemsToSellFactory;
107  $this->checkItemsQuantity = $checkItemsQuantity;
108  $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
109  $this->getProductTypesBySkus = $getProductTypesBySkus;
110  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
111  }
112 
119  public function afterPlace(OrderManagementInterface $subject, OrderInterface $order) : OrderInterface
120  {
121  $itemsById = $itemsBySku = $itemsToSell = [];
122  foreach ($order->getItems() as $item) {
123  if (!isset($itemsById[$item->getProductId()])) {
124  $itemsById[$item->getProductId()] = 0;
125  }
126  $itemsById[$item->getProductId()] += $item->getQtyOrdered();
127  }
128  $productSkus = $this->getSkusByProductIds->execute(array_keys($itemsById));
129  $productTypes = $this->getProductTypesBySkus->execute($productSkus);
130 
131  foreach ($productSkus as $productId => $sku) {
132  if (false === $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$sku])) {
133  continue;
134  }
135 
136  $itemsBySku[$sku] = (float)$itemsById[$productId];
137  $itemsToSell[] = $this->itemsToSellFactory->create([
138  'sku' => $sku,
139  'qty' => -(float)$itemsById[$productId]
140  ]);
141  }
142 
143  $websiteId = (int)$order->getStore()->getWebsiteId();
144  $websiteCode = $this->websiteRepository->getById($websiteId)->getCode();
145  $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
146 
147  $this->checkItemsQuantity->execute($itemsBySku, $stockId);
148 
150  $salesEvent = $this->salesEventFactory->create([
153  'objectId' => (string)$order->getEntityId()
154  ]);
155  $salesChannel = $this->salesChannelFactory->create([
156  'data' => [
158  'code' => $websiteCode
159  ]
160  ]);
161 
162  $this->placeReservationsForSalesEvent->execute($itemsToSell, $salesChannel, $salesEvent);
163  return $order;
164  }
165 }
__construct(PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent, GetSkusByProductIdsInterface $getSkusByProductIds, WebsiteRepositoryInterface $websiteRepository, SalesChannelInterfaceFactory $salesChannelFactory, SalesEventInterfaceFactory $salesEventFactory, ItemToSellInterfaceFactory $itemsToSellFactory, CheckItemsQuantity $checkItemsQuantity, StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver, GetProductTypesBySkusInterface $getProductTypesBySkus, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType)
$order
Definition: order.php:55
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11