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