3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \InventoryCatalog \Observer ;
7
9
8
10
use Magento \Catalog \Api \Data \ProductInterface ;
9
11
use Magento \Catalog \Controller \Adminhtml \Product \Save ;
10
12
use Magento \Framework \Event \ObserverInterface ;
11
13
use Magento \Framework \Event \Observer as EventObserver ;
14
+ use Magento \InventoryApi \Api \Data \SourceItemInterface ;
15
+ use Magento \InventoryApi \Api \SourceItemsSaveInterface ;
16
+ use Magento \InventoryCatalog \Api \DefaultSourceProviderInterface ;
17
+ use Magento \InventoryApi \Api \Data \SourceItemInterfaceFactory ;
12
18
13
19
/**
14
20
* Save source product relations during product persistence via controller
@@ -21,14 +27,36 @@ class ProcessSourceItemsObserver implements ObserverInterface
21
27
/**
22
28
* @var SourceItemsProcessor
23
29
*/
24
- private $ sourceItemsProcessor ;
30
+ protected $ sourceItemsProcessor ;
31
+ /**
32
+ * @var DefaultSourceProviderInterface
33
+ */
34
+ protected $ defaultSourceProvider ;
35
+ /**
36
+ * @var SourceItemInterfaceFactory
37
+ */
38
+ protected $ sourceItemInterfaceFactory ;
39
+ /**
40
+ * @var SourceItemsSaveInterface
41
+ */
42
+ protected $ sourceItemsSave ;
25
43
26
44
/**
27
45
* @param SourceItemsProcessor $sourceItemsProcessor
46
+ * @param DefaultSourceProviderInterface $defaultSourceProvider
47
+ * @param SourceItemInterfaceFactory $sourceItemInterfaceFactory
48
+ * @param SourceItemsSaveInterface $sourceItemsSave
28
49
*/
29
- public function __construct (SourceItemsProcessor $ sourceItemsProcessor )
30
- {
50
+ public function __construct (
51
+ SourceItemsProcessor $ sourceItemsProcessor ,
52
+ DefaultSourceProviderInterface $ defaultSourceProvider ,
53
+ SourceItemInterfaceFactory $ sourceItemInterfaceFactory ,
54
+ SourceItemsSaveInterface $ sourceItemsSave
55
+ ) {
31
56
$ this ->sourceItemsProcessor = $ sourceItemsProcessor ;
57
+ $ this ->defaultSourceProvider = $ defaultSourceProvider ;
58
+ $ this ->sourceItemInterfaceFactory = $ sourceItemInterfaceFactory ;
59
+ $ this ->sourceItemsSave = $ sourceItemsSave ;
32
60
}
33
61
34
62
/**
@@ -53,5 +81,29 @@ public function execute(EventObserver $observer)
53
81
$ product ->getSku (),
54
82
$ assignedSources
55
83
);
84
+
85
+ $ this ->updateDefaultSourceQty ($ controller );
86
+ }
87
+
88
+ /**
89
+ * @param $controller Save
90
+ */
91
+ public function updateDefaultSourceQty ($ controller )
92
+ {
93
+ /** @var $sourceItem SourceItemInterface */
94
+ $ sourceItem = $ this ->sourceItemInterfaceFactory ->create ();
95
+ $ productParams = $ controller ->getRequest ()->getParam ('product ' );
96
+
97
+ $ sku = $ productParams ['sku ' ];
98
+ $ sourceItem ->setSku ($ sku );
99
+ $ qtyAndStockStatus = $ productParams ['quantity_and_stock_status ' ];
100
+ $ qty = $ qtyAndStockStatus ['qty ' ];
101
+ $ sourceItem ->setQuantity ($ qty );
102
+ $ stockStatus = $ qtyAndStockStatus ['is_in_stock ' ];
103
+ $ sourceItem ->setStatus ($ stockStatus );
104
+ $ defaultSourceId = $ this ->defaultSourceProvider ->getId ();
105
+ $ sourceItem ->setSourceId ($ defaultSourceId );
106
+
107
+ $ this ->sourceItemsSave ->execute ([$ sourceItem ]);
56
108
}
57
109
}
0 commit comments