66
77namespace Magento \ConfigurableProduct \Model ;
88
9+ use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory ;
10+ use Magento \Catalog \Model \ProductRepository ;
911use Magento \Framework \Exception \InputException ;
1012use Magento \Framework \Exception \NoSuchEntityException ;
1113use Magento \Framework \Exception \StateException ;
@@ -47,6 +49,16 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
4749 */
4850 private $ attributeFactory ;
4951
52+ /**
53+ * @var ProductRepository|mixed
54+ */
55+ public \Magento \Catalog \Model \ProductRepository $ mediaGallery ;
56+
57+ /**
58+ * @var ProductAttributeMediaGalleryEntryInterfaceFactory|mixed
59+ */
60+ public \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
61+
5062 /**
5163 * Constructor
5264 *
@@ -55,20 +67,28 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
5567 * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType
5668 * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
5769 * @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
70+ * @param \Magento\Catalog\Model\ProductRepository $mediaGalleryProcessor
71+ * @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $myModelFactory
5872 */
5973 public function __construct (
6074 \Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
6175 \Magento \Catalog \Api \Data \ProductInterfaceFactory $ productFactory ,
6276 \Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable $ configurableType ,
6377 \Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
64- \Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory $ attributeFactory = null
78+ \Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory $ attributeFactory = null ,
79+ \Magento \Catalog \Model \ProductRepository $ mediaGalleryProcessor = null ,
80+ \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory = null
6581 ) {
6682 $ this ->productRepository = $ productRepository ;
6783 $ this ->productFactory = $ productFactory ;
6884 $ this ->configurableType = $ configurableType ;
6985 $ this ->dataObjectHelper = $ dataObjectHelper ;
7086 $ this ->attributeFactory = $ attributeFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
7187 ->get (\Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory::class);
88+ $ this ->mediaGallery = $ mediaGalleryProcessor ?: \Magento \Framework \App \ObjectManager::getInstance ()
89+ ->get (\Magento \Catalog \Model \ProductRepository::class);
90+ $ this ->myModelFactory = $ myModelFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
91+ ->get (\Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory::class);
7292 }
7393
7494 /**
@@ -81,11 +101,9 @@ public function getChildren($sku)
81101 if ($ product ->getTypeId () != \Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE ) {
82102 return [];
83103 }
84-
85104 /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
86105 $ productTypeInstance = $ product ->getTypeInstance ();
87106 $ productTypeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
88-
89107 $ childrenList = [];
90108 /** @var \Magento\Catalog\Model\Product $child */
91109 foreach ($ productTypeInstance ->getUsedProducts ($ product ) as $ child ) {
@@ -97,7 +115,9 @@ public function getChildren($sku)
97115 $ attributes [$ attrCode ] = $ value ;
98116 }
99117 }
118+ $ images = (array )$ child ->getMediaGallery ('images ' );
100119 $ attributes ['store_id ' ] = $ child ->getStoreId ();
120+ $ attributes ['media_gallery_entries ' ] = $ this ->getMediaEntries ($ images );
101121 /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
102122 $ productDataObject = $ this ->productFactory ->create ();
103123 $ this ->dataObjectHelper ->populateWithArray (
@@ -110,6 +130,28 @@ public function getChildren($sku)
110130 return $ childrenList ;
111131 }
112132
133+ /**
134+ * Get media entries
135+ *
136+ * @param array $images
137+ * @return array
138+ */
139+ public function getMediaEntries ($ images )
140+ {
141+ $ media = $ this ->myModelFactory ->create ();
142+ $ mediaGalleryEntries =[];
143+ foreach ($ images as $ image ) {
144+ $ media ->setId ($ image ["value_id " ]);
145+ $ media ->setMediaType ($ image ["media_type " ]);
146+ $ media ->setLabel ($ image ["label " ]);
147+ $ media ->setPosition ($ image ["position " ]);
148+ $ media ->setDisabled ($ image ["disabled " ]);
149+ $ media ->setFile ($ image ["file " ]);
150+ $ mediaGalleryEntries []=$ media ->getData ();
151+ }
152+ return $ mediaGalleryEntries ;
153+ }
154+
113155 /**
114156 * @inheritdoc
115157 * @throws InputException
@@ -200,12 +242,13 @@ public function removeChild($sku, $childSku)
200242 * @return \Magento\ConfigurableProduct\Helper\Product\Options\Factory
201243 *
202244 * @deprecated 100.2.0
245+ * @see Nothing
203246 */
204247 private function getOptionsFactory ()
205248 {
206249 if (!$ this ->optionsFactory ) {
207250 $ this ->optionsFactory = \Magento \Framework \App \ObjectManager::getInstance ()
208- ->get (\Magento \ConfigurableProduct \Helper \Product \Options \Factory::class);
251+ ->get (\Magento \ConfigurableProduct \Helper \Product \Options \Factory::class);// phpcs:ignore
209252 }
210253 return $ this ->optionsFactory ;
211254 }
0 commit comments