10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Controller \Adminhtml \Product \Save ;
12
12
use Magento \CatalogInventory \Api \Data \StockItemInterface ;
13
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
13
14
use Magento \Framework \Api \SearchCriteriaBuilder ;
14
15
use Magento \Framework \Api \SearchCriteriaBuilderFactory ;
15
16
use Magento \Framework \Event \Observer as EventObserver ;
30
31
*/
31
32
class ProcessSourceItemsObserver implements ObserverInterface
32
33
{
33
- /**
34
- * @var IsSourceItemManagementAllowedForProductTypeInterface
35
- */
36
- private $ isSourceItemManagementAllowedForProductType ;
37
-
38
- /**
39
- * @var SourceItemsProcessorInterface
40
- */
41
- private $ sourceItemsProcessor ;
42
-
43
- /**
44
- * @var IsSingleSourceModeInterface
45
- */
46
- private $ isSingleSourceMode ;
47
-
48
- /**
49
- * @var DefaultSourceProviderInterface
50
- */
51
- private $ defaultSourceProvider ;
52
-
53
- /**
54
- * @var SearchCriteriaBuilderFactory
55
- */
56
- private $ searchCriteriaBuilderFactory ;
57
-
58
- /**
59
- * @var SourceItemRepositoryInterface
60
- */
61
- private $ sourceItemRepository ;
62
-
63
34
/**
64
35
* @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
65
36
* @param SourceItemsProcessorInterface $sourceItemsProcessor
66
37
* @param IsSingleSourceModeInterface $isSingleSourceMode
67
38
* @param DefaultSourceProviderInterface $defaultSourceProvider
68
39
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
69
40
* @param SourceItemRepositoryInterface $sourceItemRepository
41
+ * @param StockRegistryInterface $stockRegistry
70
42
*/
71
43
public function __construct (
72
- IsSourceItemManagementAllowedForProductTypeInterface $ isSourceItemManagementAllowedForProductType ,
73
- SourceItemsProcessorInterface $ sourceItemsProcessor ,
74
- IsSingleSourceModeInterface $ isSingleSourceMode ,
75
- DefaultSourceProviderInterface $ defaultSourceProvider ,
76
- SearchCriteriaBuilderFactory $ searchCriteriaBuilderFactory ,
77
- SourceItemRepositoryInterface $ sourceItemRepository
44
+ private IsSourceItemManagementAllowedForProductTypeInterface $ isSourceItemManagementAllowedForProductType ,
45
+ private SourceItemsProcessorInterface $ sourceItemsProcessor ,
46
+ private IsSingleSourceModeInterface $ isSingleSourceMode ,
47
+ private DefaultSourceProviderInterface $ defaultSourceProvider ,
48
+ private SearchCriteriaBuilderFactory $ searchCriteriaBuilderFactory ,
49
+ private SourceItemRepositoryInterface $ sourceItemRepository ,
50
+ private StockRegistryInterface $ stockRegistry
78
51
) {
79
- $ this ->isSourceItemManagementAllowedForProductType = $ isSourceItemManagementAllowedForProductType ;
80
- $ this ->sourceItemsProcessor = $ sourceItemsProcessor ;
81
- $ this ->isSingleSourceMode = $ isSingleSourceMode ;
82
- $ this ->defaultSourceProvider = $ defaultSourceProvider ;
83
- $ this ->searchCriteriaBuilderFactory = $ searchCriteriaBuilderFactory ;
84
- $ this ->sourceItemRepository = $ sourceItemRepository ;
85
52
}
86
53
87
54
/**
@@ -101,7 +68,6 @@ public function execute(EventObserver $observer)
101
68
/** @var Save $controller */
102
69
$ controller = $ observer ->getEvent ()->getController ();
103
70
$ productData = $ controller ->getRequest ()->getParam ('product ' , []);
104
- $ singleSourceData = $ productData ['quantity_and_stock_status ' ] ?? [];
105
71
106
72
if (!$ this ->isSingleSourceMode ->execute ()) {
107
73
$ sources = $ controller ->getRequest ()->getParam ('sources ' , []);
@@ -111,20 +77,19 @@ public function execute(EventObserver $observer)
111
77
? $ this ->prepareAssignedSources ($ sources ['assigned_sources ' ])
112
78
: [];
113
79
$ this ->sourceItemsProcessor ->execute ((string )$ productData ['sku ' ], $ assignedSources );
114
- } elseif (! empty ( $ singleSourceData )) {
80
+ } else {
115
81
/** @var StockItemInterface $stockItem */
116
- $ stockItem = $ product ->getExtensionAttributes ()->getStockItem ();
117
- $ qty = $ singleSourceData ['qty ' ] ?? (empty ($ stockItem ) ? 0 : $ stockItem ->getQty ());
118
- $ isInStock = $ singleSourceData ['is_in_stock ' ] ?? (empty ($ stockItem ) ? 1 : (int )$ stockItem ->getIsInStock ());
82
+ $ stockItem = $ product ->getExtensionAttributes ()?->getStockItem()
83
+ ?? $ this ->stockRegistry ->getStockItem ($ product ->getId ());
119
84
$ defaultSourceData = [
120
- SourceItemInterface::SKU => $ productData [ ' sku ' ] ,
85
+ SourceItemInterface::SKU => $ product -> getSku () ,
121
86
SourceItemInterface::SOURCE_CODE => $ this ->defaultSourceProvider ->getCode (),
122
- SourceItemInterface::QUANTITY => $ qty ,
123
- SourceItemInterface::STATUS => $ isInStock ,
87
+ SourceItemInterface::QUANTITY => $ stockItem -> getQty () ,
88
+ SourceItemInterface::STATUS => $ stockItem -> getIsInStock () ,
124
89
];
125
- $ sourceItems = $ this ->getSourceItemsWithoutDefault ($ productData [ ' sku ' ] );
90
+ $ sourceItems = $ this ->getSourceItemsWithoutDefault ($ product -> getSku () );
126
91
$ sourceItems [] = $ defaultSourceData ;
127
- $ this ->sourceItemsProcessor ->execute ((string )$ productData [ ' sku ' ] , $ sourceItems );
92
+ $ this ->sourceItemsProcessor ->execute ((string )$ product -> getSku () , $ sourceItems );
128
93
}
129
94
}
130
95
0 commit comments