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 ' ];
@@ -453,6 +463,27 @@ protected function appendPricing($product_id, $item, $storeId = null, $base_curr
453
463
return $ item ;
454
464
}
455
465
466
+ /**
467
+ * @param $productId
468
+ * @param $item
469
+ * @return mixed
470
+ * @throws LocalizedException
471
+ */
472
+ private function appendStock ($ productId , $ item )
473
+ {
474
+ $ websiteId = $ this ->storeManager ->getWebsite ()->getId ();
475
+ $ stockItem = $ this ->stockRegistry ->getStockItem ($ productId , $ websiteId );
476
+
477
+ if (isset ($ stockItem ['is_in_stock ' ])) {
478
+ $ item ['is_in_stock ' ] = $ stockItem ['is_in_stock ' ];
479
+ }
480
+
481
+ if (isset ($ stockItem ['qty ' ])) {
482
+ $ item ['qty ' ] = (float ) $ stockItem ['qty ' ];
483
+ }
484
+
485
+ return $ item ;
486
+ }
456
487
457
488
protected function getProductPrice (
458
489
$ product_id ,
0 commit comments