6
6
7
7
namespace Magento \ConfigurableProduct \Model ;
8
8
9
+ use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory ;
10
+ use Magento \Catalog \Model \ProductRepository ;
9
11
use Magento \Framework \Exception \InputException ;
10
12
use Magento \Framework \Exception \NoSuchEntityException ;
11
13
use Magento \Framework \Exception \StateException ;
@@ -47,6 +49,16 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
47
49
*/
48
50
private $ attributeFactory ;
49
51
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
+
50
62
/**
51
63
* Constructor
52
64
*
@@ -55,20 +67,28 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
55
67
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType
56
68
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
57
69
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
70
+ * @param \Magento\Catalog\Model\ProductRepository $mediaGalleryProcessor
71
+ * @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $myModelFactory
58
72
*/
59
73
public function __construct (
60
74
\Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
61
75
\Magento \Catalog \Api \Data \ProductInterfaceFactory $ productFactory ,
62
76
\Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable $ configurableType ,
63
77
\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
65
81
) {
66
82
$ this ->productRepository = $ productRepository ;
67
83
$ this ->productFactory = $ productFactory ;
68
84
$ this ->configurableType = $ configurableType ;
69
85
$ this ->dataObjectHelper = $ dataObjectHelper ;
70
86
$ this ->attributeFactory = $ attributeFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
71
87
->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);
72
92
}
73
93
74
94
/**
@@ -81,11 +101,9 @@ public function getChildren($sku)
81
101
if ($ product ->getTypeId () != \Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE ) {
82
102
return [];
83
103
}
84
-
85
104
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
86
105
$ productTypeInstance = $ product ->getTypeInstance ();
87
106
$ productTypeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
88
-
89
107
$ childrenList = [];
90
108
/** @var \Magento\Catalog\Model\Product $child */
91
109
foreach ($ productTypeInstance ->getUsedProducts ($ product ) as $ child ) {
@@ -97,7 +115,9 @@ public function getChildren($sku)
97
115
$ attributes [$ attrCode ] = $ value ;
98
116
}
99
117
}
118
+ $ images = (array )$ child ->getMediaGallery ('images ' );
100
119
$ attributes ['store_id ' ] = $ child ->getStoreId ();
120
+ $ attributes ['media_gallery_entries ' ] = $ this ->getMediaEntries ($ images );
101
121
/** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
102
122
$ productDataObject = $ this ->productFactory ->create ();
103
123
$ this ->dataObjectHelper ->populateWithArray (
@@ -110,6 +130,28 @@ public function getChildren($sku)
110
130
return $ childrenList ;
111
131
}
112
132
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
+
113
155
/**
114
156
* @inheritdoc
115
157
* @throws InputException
@@ -200,12 +242,13 @@ public function removeChild($sku, $childSku)
200
242
* @return \Magento\ConfigurableProduct\Helper\Product\Options\Factory
201
243
*
202
244
* @deprecated 100.2.0
245
+ * @see Nothing
203
246
*/
204
247
private function getOptionsFactory ()
205
248
{
206
249
if (!$ this ->optionsFactory ) {
207
250
$ 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
209
252
}
210
253
return $ this ->optionsFactory ;
211
254
}
0 commit comments