44
55use Magento \Framework \Event \Observer as EventObserver ;
66use Magento \Framework \Event \ObserverInterface ;
7+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
78use 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
1213class 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 }
0 commit comments