15
15
use Magento \Framework \EntityManager \MetadataPool ;
16
16
use Magento \Framework \Api \SearchCriteriaBuilder ;
17
17
use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product as DataProvider ;
18
+ use Magento \CatalogInventory \Helper \Stock ;
19
+ use Magento \CatalogInventory \Api \StockConfigurationInterface ;
18
20
19
21
/**
20
22
* Collection for fetching configurable child product data.
@@ -41,6 +43,16 @@ class Collection
41
43
*/
42
44
private $ productDataProvider ;
43
45
46
+ /**
47
+ * @var Stock
48
+ */
49
+ private $ stockHelper ;
50
+
51
+ /**
52
+ * @var StockConfigurationInterface
53
+ */
54
+ protected $ stockConfig ;
55
+
44
56
/**
45
57
* @var MetadataPool
46
58
*/
@@ -73,13 +85,17 @@ public function __construct(
73
85
ProductFactory $ productFactory ,
74
86
SearchCriteriaBuilder $ searchCriteriaBuilder ,
75
87
DataProvider $ productDataProvider ,
76
- MetadataPool $ metadataPool
88
+ MetadataPool $ metadataPool ,
89
+ Stock $ stockHelper ,
90
+ StockConfigurationInterface $ stockConfig
77
91
) {
78
92
$ this ->childCollectionFactory = $ childCollectionFactory ;
79
93
$ this ->productFactory = $ productFactory ;
80
94
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
81
95
$ this ->productDataProvider = $ productDataProvider ;
82
96
$ this ->metadataPool = $ metadataPool ;
97
+ $ this ->stockHelper = $ stockHelper ;
98
+ $ this ->stockConfig = $ stockConfig ;
83
99
}
84
100
85
101
/**
@@ -143,13 +159,19 @@ private function fetch() : array
143
159
return $ this ->childrenMap ;
144
160
}
145
161
162
+ $ showOutOfStock = $ this ->stockConfig ->isShowOutOfStock ();
163
+
146
164
foreach ($ this ->parentProducts as $ product ) {
147
165
$ attributeData = $ this ->getAttributesCodes ($ product );
148
166
/** @var ChildCollection $childCollection */
149
167
$ childCollection = $ this ->childCollectionFactory ->create ();
150
168
$ childCollection ->setProductFilter ($ product );
151
169
$ childCollection ->addAttributeToSelect ($ attributeData );
152
170
171
+ if (!$ showOutOfStock ) {
172
+ $ this ->stockHelper ->addInStockFilterToCollection ($ childCollection );
173
+ }
174
+
153
175
/** @var Product $childProduct */
154
176
foreach ($ childCollection ->getItems () as $ childProduct ) {
155
177
$ formattedChild = ['model ' => $ childProduct , 'sku ' => $ childProduct ->getSku ()];
0 commit comments