Skip to content

Commit cb9c08d

Browse files
committed
Updated msi check
1 parent 13b6b19 commit cb9c08d

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

Observer/SubtractInventoryObserver.php

100644100755
Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Magento\Framework\Event\ObserverInterface;
77
use Magento\CatalogInventory\Api\StockRegistryInterface;
88
use Magento\Store\Api\StoreRepositoryInterface;
9-
use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite;
10-
use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface;
11-
use Magento\InventoryReservationsApi\Model\AppendReservationsInterface;
9+
use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite\Proxy as GetAssignedStockIdForWebsiteProxy;
10+
use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface\Proxy as ReservationBuilderInterfaceProxy;
11+
use Magento\InventoryReservationsApi\Model\AppendReservationsInterface\Proxy as AppendReservationsInterfaceProxy;
1212

1313
class SubtractInventoryObserver implements ObserverInterface
1414
{
@@ -32,20 +32,31 @@ class SubtractInventoryObserver implements ObserverInterface
3232
*/
3333
private $appendReservations;
3434

35+
/**
36+
* @var StockRegistryInterface
37+
*/
38+
private $stockRegistry;
39+
3540
/**
3641
* SubtractInventoryObserver constructor.
42+
* @param StoreRepositoryInterface $storeRepository
43+
* @param GetAssignedStockIdForWebsiteProxy $getAssignedStockIdForWebsite
44+
* @param ReservationBuilderInterfaceProxy $reservationBuilder
45+
* @param AppendReservationsInterfaceProxy $appendReservations
3746
* @param StockRegistryInterface $stockRegistry
3847
*/
3948
public function __construct(
4049
StoreRepositoryInterface $storeRepository,
41-
GetAssignedStockIdForWebsite $getAssignedStockIdForWebsite,
42-
ReservationBuilderInterface $reservationBuilder,
43-
AppendReservationsInterface $appendReservations
50+
GetAssignedStockIdForWebsiteProxy $getAssignedStockIdForWebsite,
51+
ReservationBuilderInterfaceProxy $reservationBuilder,
52+
AppendReservationsInterfaceProxy $appendReservations,
53+
StockRegistryInterface $stockRegistry
4454
) {
4555
$this->storeRepository = $storeRepository;
4656
$this->getAssignedStockIdForWebsite = $getAssignedStockIdForWebsite;
4757
$this->reservationBuilder = $reservationBuilder;
4858
$this->appendReservations = $appendReservations;
59+
$this->stockRegistry = $stockRegistry;
4960
}
5061

5162
/**
@@ -69,27 +80,46 @@ public function execute(EventObserver $observer)
6980
$storeId = $order->getStoreId();
7081
$website = $this->storeRepository->getById($storeId)->getWebsite();
7182
$websiteCode = $website->getCode();
72-
$stockId = $this->getAssignedStockIdForWebsite->execute($websiteCode);
73-
74-
$reservations = [];
83+
$websiteId = $website->getId();
84+
85+
if (
86+
interface_exists(\Magento\InventoryReservationsApi\Model\ReservationBuilderInterface::class)
87+
&& interface_exists(\Magento\InventoryReservationsApi\Model\AppendReservationsInterface::class)
88+
) {
89+
$stockId = $this->getAssignedStockIdForWebsite->execute($websiteCode);
90+
$reservations = [];
91+
92+
foreach ($order->getAllItems() as $item) {
93+
$itemData = $item->getData();
94+
95+
$itemQty = $itemData['qty_ordered'] ?? null;
96+
$itemSku = $itemData['sku'] ?? null;
97+
98+
if (!empty($itemQty) && !empty($itemSku)) {
99+
$reservations[] = $this->reservationBuilder
100+
->setSku($itemSku)
101+
->setQuantity(-$itemQty)
102+
->setStockId($stockId)
103+
->build();
104+
}
105+
}
75106

76-
foreach ($order->getAllItems() as $item) {
77-
$itemData = $item->getData();
107+
$this->appendReservations->execute($reservations);
108+
} else {
109+
foreach ($order->getAllItems() as $item) {
110+
$itemData = $item->getData();
78111

79-
$itemQty = $itemData['qty_ordered'] ?? null;
80-
$itemSku = $itemData['sku'] ?? null;
112+
$itemQty = $itemData['qty_ordered'] ?? null;
113+
$itemSku = $itemData['sku'] ?? null;
81114

82-
if (!empty($itemQty) && !empty($itemSku)) {
83-
$reservations[] = $this->reservationBuilder
84-
->setSku($itemSku)
85-
->setQuantity(-$itemQty)
86-
->setStockId($stockId)
87-
->build();
115+
$stockItem = $this->stockRegistry->getStockItemBySku($itemSku, $websiteId);
116+
$currentQty = (float) $stockItem->getQty();
117+
$stockItem->setQty($currentQty - $itemQty);
118+
$stockItem->setIsInStock((($currentQty - $itemQty) > 0) ? 1 : 0);
119+
$this->stockRegistry->updateStockItemBySku($itemSku, $stockItem);
88120
}
89121
}
90122

91-
$this->appendReservations->execute($reservations);
92-
93123
$order->setInventoryProcessed(true)->save();
94124
return $this;
95125
}

0 commit comments

Comments
 (0)