Skip to content

Commit e52f302

Browse files
Move proxy to di.xml
1 parent 60f57a2 commit e52f302

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

Observer/SubtractInventoryObserver.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Magento\Framework\Event\Observer as EventObserver;
66
use Magento\Framework\Event\ObserverInterface;
7+
use Magento\CatalogInventory\Api\StockRegistryInterface;
78
use Magento\Store\Api\StoreRepositoryInterface;
8-
use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite\Proxy as GetAssignedStockIdForWebsiteProxy;
9-
use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface\Proxy as ReservationBuilderInterfaceProxy;
10-
use Magento\InventoryReservationsApi\Model\AppendReservationsInterface\Proxy as AppendReservationsInterfaceProxy;
9+
use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite;
10+
use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface;
11+
use Magento\InventoryReservationsApi\Model\AppendReservationsInterface;
1112

1213
class SubtractInventoryObserver implements ObserverInterface
1314
{
@@ -31,23 +32,31 @@ class SubtractInventoryObserver implements ObserverInterface
3132
*/
3233
private $appendReservations;
3334

35+
/**
36+
* @var StockRegistryInterface
37+
*/
38+
private $stockRegistry;
39+
3440
/**
3541
* SubtractInventoryObserver constructor.
3642
* @param StoreRepositoryInterface $storeRepository
37-
* @param GetAssignedStockIdForWebsiteProxy $getAssignedStockIdForWebsite
38-
* @param ReservationBuilderInterfaceProxy $reservationBuilder
39-
* @param AppendReservationsInterfaceProxy $appendReservations
43+
* @param GetAssignedStockIdForWebsite $getAssignedStockIdForWebsite
44+
* @param ReservationBuilderInterface $reservationBuilder
45+
* @param AppendReservationsInterface $appendReservations
46+
* @param StockRegistryInterface $stockRegistry
4047
*/
4148
public function __construct(
4249
StoreRepositoryInterface $storeRepository,
43-
GetAssignedStockIdForWebsiteProxy $getAssignedStockIdForWebsite,
44-
ReservationBuilderInterfaceProxy $reservationBuilder,
45-
AppendReservationsInterfaceProxy $appendReservations
50+
GetAssignedStockIdForWebsite $getAssignedStockIdForWebsite,
51+
ReservationBuilderInterface $reservationBuilder,
52+
AppendReservationsInterface $appendReservations,
53+
StockRegistryInterface $stockRegistry
4654
) {
4755
$this->storeRepository = $storeRepository;
4856
$this->getAssignedStockIdForWebsite = $getAssignedStockIdForWebsite;
4957
$this->reservationBuilder = $reservationBuilder;
5058
$this->appendReservations = $appendReservations;
59+
$this->stockRegistry = $stockRegistry;
5160
}
5261

5362
/**
@@ -71,11 +80,9 @@ public function execute(EventObserver $observer)
7180
$storeId = $order->getStoreId();
7281
$website = $this->storeRepository->getById($storeId)->getWebsite();
7382
$websiteCode = $website->getCode();
83+
$websiteId = $website->getId();
7484

75-
if (
76-
interface_exists(\Magento\InventoryReservationsApi\Model\ReservationBuilderInterface::class)
77-
&& interface_exists(\Magento\InventoryReservationsApi\Model\AppendReservationsInterface::class)
78-
) {
85+
if (class_exists(\Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite::class) && class_exists(\Magento\InventoryReservationsApi\Model\ReservationBuilderInterface::class) && class_exists(\Magento\InventoryReservationsApi\Model\AppendReservationsInterface::class)) {
7986
$stockId = $this->getAssignedStockIdForWebsite->execute($websiteCode);
8087
$reservations = [];
8188

@@ -95,7 +102,21 @@ interface_exists(\Magento\InventoryReservationsApi\Model\ReservationBuilderInter
95102
}
96103

97104
$this->appendReservations->execute($reservations);
105+
} else {
106+
foreach ($order->getAllItems() as $item) {
107+
$itemData = $item->getData();
108+
109+
$itemQty = $itemData['qty_ordered'] ?? null;
110+
$itemSku = $itemData['sku'] ?? null;
111+
112+
$stockItem = $this->stockRegistry->getStockItemBySku($itemSku, $websiteId);
113+
$currentQty = (float) $stockItem->getQty();
114+
$stockItem->setQty($currentQty - $itemQty);
115+
$stockItem->setIsInStock((($currentQty - $itemQty) > 0) ? 1 : 0);
116+
$this->stockRegistry->updateStockItemBySku($itemSku, $stockItem);
117+
}
98118
}
119+
99120
$order->setInventoryProcessed(true)->save();
100121
return $this;
101122
}

etc/di.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@
3030
</argument>
3131
</arguments>
3232
</type>
33+
<type name="Paynl\Payment\Observer\SubtractInventoryObserver">
34+
<arguments>
35+
<argument name="getAssignedStockIdForWebsite" xsi:type="object">
36+
Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite\Proxy
37+
</argument>
38+
<argument name="reservationBuilder" xsi:type="object">
39+
Magento\InventoryReservationsApi\Model\ReservationBuilderInterface\Proxy
40+
</argument>
41+
<argument name="appendReservations" xsi:type="object">
42+
Magento\InventoryReservationsApi\Model\AppendReservationsInterface\Proxy
43+
</argument>
44+
</arguments>
45+
</type>
3346
</config>

0 commit comments

Comments
 (0)