99
1010use Magento \Catalog \Api \Data \ProductInterface ;
1111use Magento \Catalog \Model \Product ;
12- use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \CollectionFactory ;
13- use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \Collection as ChildCollection ;
1412use Magento \Catalog \Model \ProductFactory ;
13+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \Collection as ChildCollection ;
14+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \CollectionFactory ;
1515use Magento \Framework \EntityManager \MetadataPool ;
1616use Magento \Framework \Api \SearchCriteriaBuilder ;
1717use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product as DataProvider ;
@@ -47,9 +47,9 @@ class Collection
4747 private $ metadataPool ;
4848
4949 /**
50- * @var int []
50+ * @var Product []
5151 */
52- private $ parentIds = [];
52+ private $ parentProducts = [];
5353
5454 /**
5555 * @var array
@@ -83,19 +83,22 @@ public function __construct(
8383 }
8484
8585 /**
86- * Add parent Id to collection filter
86+ * Add parent to collection filter
8787 *
88- * @param int $id
88+ * @param Product $product
8989 * @return void
9090 */
91- public function addParentId ( int $ id ) : void
91+ public function addParentProduct ( Product $ product ) : void
9292 {
93- if (!in_array ($ id , $ this ->parentIds ) && !empty ($ this ->childrenMap )) {
93+ if (isset ($ this ->parentProducts [$ product ->getId ()])) {
94+ return ;
95+ }
96+
97+ if (!empty ($ this ->childrenMap )) {
9498 $ this ->childrenMap = [];
95- $ this ->parentIds [] = $ id ;
96- } elseif (!in_array ($ id , $ this ->parentIds )) {
97- $ this ->parentIds [] = $ id ;
99+
98100 }
101+ $ this ->parentProducts [$ product ->getId ()] = $ product ;
99102 }
100103
101104 /**
@@ -130,20 +133,23 @@ public function getChildProductsByParentId(int $id) : array
130133 * Fetch all children products from parent id's.
131134 *
132135 * @return array
136+ * @throws \Exception
133137 */
134138 private function fetch () : array
135139 {
136- if (empty ($ this ->parentIds ) || !empty ($ this ->childrenMap )) {
140+ if (empty ($ this ->parentProducts ) || !empty ($ this ->childrenMap )) {
137141 return $ this ->childrenMap ;
138142 }
139143
140144 $ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
141- foreach ($ this ->parentIds as $ id ) {
145+ foreach ($ this ->parentProducts as $ product ) {
146+ $ attributeData = $ this ->getAttributesCodes ($ product );
142147 /** @var ChildCollection $childCollection */
143148 $ childCollection = $ this ->childCollectionFactory ->create ();
149+ $ childCollection ->addAttributeToSelect ($ attributeData );
150+
144151 /** @var Product $product */
145- $ product = $ this ->productFactory ->create ();
146- $ product ->setData ($ linkField , $ id );
152+ $ product ->setData ($ linkField , $ product ->getId ());
147153 $ childCollection ->setProductFilter ($ product );
148154
149155 /** @var Product $childProduct */
@@ -160,4 +166,24 @@ private function fetch() : array
160166
161167 return $ this ->childrenMap ;
162168 }
169+
170+ /**
171+ * Get attributes code
172+ *
173+ * @param \Magento\Catalog\Model\Product $currentProduct
174+ * @return array
175+ */
176+ private function getAttributesCodes (Product $ currentProduct ): array
177+ {
178+ $ attributeCodes = [];
179+ $ allowAttributes = $ currentProduct ->getTypeInstance ()->getConfigurableAttributes ($ currentProduct );
180+ foreach ($ allowAttributes as $ attribute ) {
181+ $ productAttribute = $ attribute ->getProductAttribute ();
182+ if (!\in_array ($ productAttribute ->getAttributeCode (), $ attributeCodes )) {
183+ $ attributeCodes [] = $ productAttribute ->getAttributeCode ();
184+ }
185+ }
186+
187+ return $ attributeCodes ;
188+ }
163189}
0 commit comments