19
19
use Magento \Store \Model \ScopeInterface ;
20
20
use ScandiPWA \Performance \Api \ProductsDataPostProcessorInterface ;
21
21
use ScandiPWA \Performance \Model \Resolver \ResolveInfoFieldsTrait ;
22
+ use Magento \InventoryApi \Api \GetStockSourceLinksInterface ;
23
+ use Magento \InventoryApi \Api \Data \StockSourceLinkInterface ;
24
+ use Magento \InventoryCatalog \Model \GetStockIdForCurrentWebsite ;
25
+ use Magento \InventorySalesApi \Api \GetProductSalableQtyInterface ;
26
+ use Magento \InventoryConfigurationApi \Api \GetStockItemConfigurationInterface ;
22
27
23
28
/**
24
29
* Class Images
@@ -51,6 +56,26 @@ class Stocks implements ProductsDataPostProcessorInterface
51
56
*/
52
57
protected $ scopeConfig ;
53
58
59
+ /**
60
+ * @var GetStockSourceLinksInterface
61
+ */
62
+ private $ getStockSourceLinks ;
63
+
64
+ /**
65
+ * @var GetStockIdForCurrentWebsite
66
+ */
67
+ private $ getStockIdForCurrentWebsite ;
68
+
69
+ /**
70
+ * @var GetProductSalableQtyInterface
71
+ */
72
+ private $ getProductSalableQty ;
73
+
74
+ /**
75
+ * @var GetStockItemConfigurationInterface
76
+ */
77
+ private $ getStockItemConfiguration ;
78
+
54
79
/**
55
80
* Stocks constructor.
56
81
* @param SourceItemRepositoryInterface $stockRepository
@@ -60,11 +85,19 @@ class Stocks implements ProductsDataPostProcessorInterface
60
85
public function __construct (
61
86
SourceItemRepositoryInterface $ stockRepository ,
62
87
SearchCriteriaBuilder $ searchCriteriaBuilder ,
63
- ScopeConfigInterface $ scopeConfig
88
+ ScopeConfigInterface $ scopeConfig ,
89
+ GetStockSourceLinksInterface $ getStockSourceLinks ,
90
+ GetStockIdForCurrentWebsite $ getStockIdForCurrentWebsite ,
91
+ GetStockItemConfigurationInterface $ getStockItemConfiguration ,
92
+ GetProductSalableQtyInterface $ getProductSalableQty
64
93
) {
65
94
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
66
95
$ this ->stockRepository = $ stockRepository ;
67
96
$ this ->scopeConfig = $ scopeConfig ;
97
+ $ this ->getStockSourceLinks = $ getStockSourceLinks ;
98
+ $ this ->getStockIdForCurrentWebsite = $ getStockIdForCurrentWebsite ;
99
+ $ this ->getStockItemConfiguration = $ getStockItemConfiguration ;
100
+ $ this ->getProductSalableQty = $ getProductSalableQty ;
68
101
}
69
102
70
103
/**
@@ -115,6 +148,13 @@ public function process(
115
148
};
116
149
}
117
150
151
+ $ stockId = $ this ->getStockIdForCurrentWebsite ->execute ();
152
+
153
+ if (!$ stockId ) {
154
+ return function (&$ productData ) {
155
+ };
156
+ }
157
+
118
158
$ productSKUs = array_map (function ($ product ) {
119
159
return $ product ->getSku ();
120
160
}, $ products );
@@ -128,8 +168,24 @@ public function process(
128
168
);
129
169
}
130
170
171
+ $ criteria = $ this ->searchCriteriaBuilder
172
+ ->addFilter (StockSourceLinkInterface::STOCK_ID , $ stockId )
173
+ ->create ();
174
+
175
+ $ sourceLinks = $ this ->getStockSourceLinks ->execute ($ criteria )->getItems ();
176
+
177
+ if (!count ($ sourceLinks )) {
178
+ return function (&$ productData ) {
179
+ };
180
+ }
181
+
182
+ $ sourceCodes = array_map (function ($ sourceLink ) {
183
+ return $ sourceLink ->getSourceCode ();
184
+ }, $ sourceLinks );
185
+
131
186
$ criteria = $ this ->searchCriteriaBuilder
132
187
->addFilter (SourceItemInterface::SKU , $ productSKUs , 'in ' )
188
+ ->addFilter (SourceItemInterface::SOURCE_CODE , $ sourceCodes , 'in ' )
133
189
->create ();
134
190
135
191
$ stockItems = $ this ->stockRepository ->getList ($ criteria )->getItems ();
@@ -142,17 +198,40 @@ public function process(
142
198
$ formattedStocks = [];
143
199
144
200
foreach ($ stockItems as $ stockItem ) {
145
- $ inStock = $ stockItem ->getStatus () === SourceItemInterface::STATUS_IN_STOCK ;
146
-
147
201
$ leftInStock = null ;
148
202
$ qty = $ stockItem ->getQuantity ();
203
+ $ sku = $ stockItem ->getSku ();
204
+
205
+ $ inStock = (($ stockItem ->getStatus () === SourceItemInterface::STATUS_IN_STOCK )
206
+ and $ qty > 0 )? true : false ;
207
+
208
+ if ($ inStock ) {
209
+ $ productSalableQty = $ this ->getProductSalableQty ->execute ($ sku , $ stockId );
210
+
211
+ if ($ productSalableQty > 0 ) {
212
+ $ stockItemConfiguration = $ this ->getStockItemConfiguration ->execute ($ sku , $ stockId );
213
+ $ minQty = $ stockItemConfiguration ->getMinQty ();
214
+ if ($ productSalableQty >= $ minQty ){
215
+ $ stockLeft = $ productSalableQty - $ minQty ;
216
+ $ thresholdQty = $ stockItemConfiguration ->getStockThresholdQty ();
217
+ if ($ thresholdQty !== 0 ) {
218
+ $ leftInStock = $ stockLeft <= $ thresholdQty ? (float )$ stockLeft : null ;
219
+ }
220
+ } else {
221
+ $ inStock = false ;
222
+ }
223
+ } else {
224
+ $ inStock = false ;
225
+ }
149
226
150
- if ($ thresholdQty !== (float ) 0 ) {
151
- $ isThresholdPassed = $ qty <= $ thresholdQty ;
152
- $ leftInStock = $ isThresholdPassed ? $ qty : null ;
153
227
}
154
228
155
- $ formattedStocks [$ stockItem ->getSku ()] = [
229
+ if (isset ($ formattedStocks [$ sku ])
230
+ && $ formattedStocks [$ sku ][self ::STOCK_STATUS ] == self ::IN_STOCK ) {
231
+ continue ;
232
+ }
233
+
234
+ $ formattedStocks [$ sku ] = [
156
235
self ::STOCK_STATUS => $ inStock ? self ::IN_STOCK : self ::OUT_OF_STOCK ,
157
236
self ::ONLY_X_LEFT_IN_STOCK => $ leftInStock
158
237
];
0 commit comments