1
1
<?php
2
2
namespace Ometria \Api \Controller \V1 ;
3
+
4
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
5
+ use Magento \Framework \Exception \LocalizedException ;
3
6
use Ometria \Api \Helper \Format \V1 \Products as Helper ;
4
- use \Ometria \Api \Controller \V1 \Base ;
7
+ use Ometria \Api \Controller \V1 \Base ;
8
+
5
9
class Products extends Base
6
10
{
7
11
const PRODUCT_TYPE_IDX = 'magento_product_type ' ;
@@ -28,6 +32,9 @@ class Products extends Base
28
32
protected $ directoryHelper ;
29
33
protected $ storeUrlHelper ;
30
34
35
+ /** @var StockRegistryInterface */
36
+ private $ stockRegistry ;
37
+
31
38
protected $ storeIdCache =false ;
32
39
protected $ productTypeFactory ;
33
40
@@ -64,7 +71,8 @@ public function __construct(
64
71
\Magento \Framework \App \ResourceConnection $ resourceConnection ,
65
72
\Magento \Directory \Helper \Data $ directoryHelper ,
66
73
\Ometria \Api \Helper \StoreUrl $ storeUrlHelper ,
67
- \Magento \Catalog \Model \Product \TypeFactory $ productTypeFactory
74
+ \Magento \Catalog \Model \Product \TypeFactory $ productTypeFactory ,
75
+ StockRegistryInterface $ stockRegistry
68
76
) {
69
77
parent ::__construct ($ context );
70
78
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -86,6 +94,7 @@ public function __construct(
86
94
$ this ->directoryHelper = $ directoryHelper ;
87
95
$ this ->storeUrlHelper = $ storeUrlHelper ;
88
96
$ this ->productTypeFactory = $ productTypeFactory ;
97
+ $ this ->stockRegistry = $ stockRegistry ;
89
98
}
90
99
91
100
protected function getArrayKey ($ array , $ key )
@@ -159,6 +168,7 @@ protected function serializeItem($item)
159
168
}
160
169
161
170
$ tmp = $ this ->appendPricing ($ tmp ['id ' ], $ tmp );
171
+ $ tmp = $ this ->appendStock ($ tmp ['id ' ], $ tmp );
162
172
163
173
if (isset ($ item ['store_listings ' ])) {
164
174
$ tmp ['store_listings ' ] = $ item ['store_listings ' ];
@@ -448,6 +458,27 @@ protected function appendPricing($product_id, $item, $store_currency=null, $base
448
458
return $ item ;
449
459
}
450
460
461
+ /**
462
+ * @param $productId
463
+ * @param $item
464
+ * @return mixed
465
+ * @throws LocalizedException
466
+ */
467
+ private function appendStock ($ productId , $ item )
468
+ {
469
+ $ websiteId = $ this ->storeManager ->getWebsite ()->getId ();
470
+ $ stockItem = $ this ->stockRegistry ->getStockItem ($ productId , $ websiteId );
471
+
472
+ if (isset ($ stockItem ['is_in_stock ' ])) {
473
+ $ item ['is_in_stock ' ] = $ stockItem ['is_in_stock ' ];
474
+ }
475
+
476
+ if (isset ($ stockItem ['qty ' ])) {
477
+ $ item ['qty ' ] = (float ) $ stockItem ['qty ' ];
478
+ }
479
+
480
+ return $ item ;
481
+ }
451
482
452
483
protected function getProductPrice (
453
484
$ product_id ,
0 commit comments