9
9
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Model \Product ;
12
- use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
13
- use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \Collection as ChildCollection ;
14
- use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \CollectionFactory ;
12
+ use Magento \ConfigurableProductGraphQl \Model \ResourceModel \Product \Type \GetChildrenIdsByParentId ;
13
+ use Magento \Catalog \Model \ResourceModel \Product \Collection as ChildCollection ;
14
+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
15
+ use Magento \Framework \App \ObjectManager ;
15
16
use Magento \Framework \EntityManager \MetadataPool ;
16
17
use Magento \Framework \Api \SearchCriteriaBuilder ;
18
+ use Magento \Framework \Exception \LocalizedException ;
17
19
use Magento \Framework \ObjectManager \ResetAfterRequestInterface ;
18
20
use Magento \GraphQl \Model \Query \ContextInterface ;
19
21
use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product \CollectionProcessorInterface ;
22
24
23
25
/**
24
26
* Collection for fetching configurable child product data.
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
28
*/
26
29
class Collection implements ResetAfterRequestInterface
27
30
{
@@ -65,32 +68,42 @@ class Collection implements ResetAfterRequestInterface
65
68
*/
66
69
private $ collectionPostProcessor ;
67
70
71
+ /**
72
+ * @var GetChildrenIdsByParentId
73
+ */
74
+ private $ getChildrenIdsByParentId ;
75
+
68
76
/**
69
77
* @param CollectionFactory $childCollectionFactory
70
78
* @param SearchCriteriaBuilder $searchCriteriaBuilder
71
79
* @param MetadataPool $metadataPool
72
80
* @param CollectionProcessorInterface $collectionProcessor
73
81
* @param CollectionPostProcessor $collectionPostProcessor
82
+ * @param GetChildrenIdsByParentId|null $getChildrenIdsByParentId
74
83
*/
75
84
public function __construct (
76
85
CollectionFactory $ childCollectionFactory ,
77
86
SearchCriteriaBuilder $ searchCriteriaBuilder ,
78
87
MetadataPool $ metadataPool ,
79
88
CollectionProcessorInterface $ collectionProcessor ,
80
- CollectionPostProcessor $ collectionPostProcessor
89
+ CollectionPostProcessor $ collectionPostProcessor ,
90
+ ?GetChildrenIdsByParentId $ getChildrenIdsByParentId = null
81
91
) {
82
92
$ this ->childCollectionFactory = $ childCollectionFactory ;
83
93
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
84
94
$ this ->metadataPool = $ metadataPool ;
85
95
$ this ->collectionProcessor = $ collectionProcessor ;
86
96
$ this ->collectionPostProcessor = $ collectionPostProcessor ;
97
+ $ this ->getChildrenIdsByParentId = $ getChildrenIdsByParentId
98
+ ?: ObjectManager::getInstance ()->get (GetChildrenIdsByParentId::class);
87
99
}
88
100
89
101
/**
90
102
* Add parent to collection filter
91
103
*
92
104
* @param Product $product
93
105
* @return void
106
+ * @throws \Exception
94
107
*/
95
108
public function addParentProduct (Product $ product ) : void
96
109
{
@@ -125,6 +138,7 @@ public function addEavAttributes(array $attributeCodes) : void
125
138
* @param ContextInterface $context
126
139
* @param array $attributeCodes
127
140
* @return array
141
+ * @throws LocalizedException
128
142
*/
129
143
public function getChildProductsByParentId (int $ id , ContextInterface $ context , array $ attributeCodes ) : array
130
144
{
@@ -143,6 +157,7 @@ public function getChildProductsByParentId(int $id, ContextInterface $context, a
143
157
* @param ContextInterface $context
144
158
* @param array $attributeCodes
145
159
* @return array
160
+ * @throws LocalizedException
146
161
*/
147
162
private function fetch (ContextInterface $ context , array $ attributeCodes ) : array
148
163
{
@@ -152,11 +167,9 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array
152
167
153
168
/** @var ChildCollection $childCollection */
154
169
$ childCollection = $ this ->childCollectionFactory ->create ();
155
- foreach ($ this ->parentProducts as $ product ) {
156
- $ childCollection ->setProductFilter ($ product );
157
- }
170
+ $ childrenIdsByParent = $ this ->getChildrenIdsByParentId ->execute (array_keys ($ this ->parentProducts ));
158
171
$ childCollection ->addWebsiteFilter ($ context ->getExtensionAttributes ()->getStore ()->getWebsiteId ());
159
-
172
+ $ childCollection -> addIdFilter ( array_keys ( $ childrenIdsByParent ));
160
173
$ attributeCodes = array_unique (array_merge ($ this ->attributeCodes , $ attributeCodes ));
161
174
162
175
$ this ->collectionProcessor ->process (
@@ -173,14 +186,13 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array
173
186
continue ;
174
187
}
175
188
$ formattedChild = ['model ' => $ childProduct , 'sku ' => $ childProduct ->getSku ()];
176
- $ parentId = (int )$ childProduct ->getParentId ();
177
- if (!isset ($ this ->childrenMap [$ parentId ])) {
178
- $ this ->childrenMap [$ parentId ] = [];
189
+ foreach ($ childrenIdsByParent [$ childProduct ->getId ()] as $ parentId ) {
190
+ if (!isset ($ this ->childrenMap [$ parentId ])) {
191
+ $ this ->childrenMap [$ parentId ] = [];
192
+ }
193
+ $ this ->childrenMap [$ parentId ][] = $ formattedChild ;
179
194
}
180
-
181
- $ this ->childrenMap [$ parentId ][] = $ formattedChild ;
182
195
}
183
-
184
196
return $ this ->childrenMap ;
185
197
}
186
198
0 commit comments