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 ;
12
+ use Magento \Framework \Exception \LocalizedException ;
10
13
use Magento \Framework \Exception \NoSuchEntityException ;
11
14
use Magento \Framework \Exception \StateException ;
15
+ use Magento \Catalog \Api \Data \ProductInterface ;
16
+
12
17
13
18
/**
14
19
* Configurable product link management.
@@ -47,6 +52,11 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
47
52
*/
48
53
private $ attributeFactory ;
49
54
55
+ //private $mediaGalleryProcessor;
56
+ public \Magento \Catalog \Model \ProductRepository $ mediaGallery ;
57
+ public \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
58
+ private array $ images ;
59
+
50
60
/**
51
61
* Constructor
52
62
*
@@ -55,24 +65,47 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
55
65
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType
56
66
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
57
67
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
68
+ * @param \Magento\Catalog\Model\ProductRepository $mediaGalleryProcessor
69
+ * @param \Magento\Catalog\Api\Data\ProductInterface $productInterface
58
70
*/
59
71
public function __construct (
60
72
\Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
61
73
\Magento \Catalog \Api \Data \ProductInterfaceFactory $ productFactory ,
62
74
\Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable $ configurableType ,
63
75
\Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
64
- \Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory $ attributeFactory = null
76
+ \Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory $ attributeFactory = null ,
77
+ \Magento \Catalog \Model \ProductRepository $ mediaGalleryProcessor ,
78
+ // \Magento\Catalog\Api\Data\ProductInterface $productInterface,
79
+ \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory
65
80
) {
66
81
$ this ->productRepository = $ productRepository ;
67
82
$ this ->productFactory = $ productFactory ;
68
83
$ this ->configurableType = $ configurableType ;
69
84
$ this ->dataObjectHelper = $ dataObjectHelper ;
70
85
$ this ->attributeFactory = $ attributeFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
71
86
->get (\Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory::class);
87
+ $ this ->mediaGallery = $ mediaGalleryProcessor ;
88
+ // $this->productInterface=$productInterface;
89
+ $ this ->myModelFactory = $ myModelFactory ;
72
90
}
73
91
92
+ /**
93
+ * Process Media gallery data before save product.
94
+ *
95
+ * Compare Media Gallery Entries Data with existing Media Gallery
96
+ * * If Media entry has not value_id set it as new
97
+ * * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
98
+ * * Merge Existing and new media gallery
99
+ *
100
+ * @param ProductInterface $product contains only existing media gallery items
101
+ * @param array $mediaGalleryEntries array which contains all media gallery items
102
+ * @throws InputException
103
+ * @throws StateException
104
+ * @throws LocalizedException
105
+ */
74
106
/**
75
107
* @inheritdoc
108
+ * @throws LocalizedException
76
109
*/
77
110
public function getChildren ($ sku )
78
111
{
@@ -81,11 +114,9 @@ public function getChildren($sku)
81
114
if ($ product ->getTypeId () != \Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE ) {
82
115
return [];
83
116
}
84
-
85
117
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
86
118
$ productTypeInstance = $ product ->getTypeInstance ();
87
119
$ productTypeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
88
-
89
120
$ childrenList = [];
90
121
/** @var \Magento\Catalog\Model\Product $child */
91
122
foreach ($ productTypeInstance ->getUsedProducts ($ product ) as $ child ) {
@@ -97,7 +128,9 @@ public function getChildren($sku)
97
128
$ attributes [$ attrCode ] = $ value ;
98
129
}
99
130
}
131
+ $ images = (array )$ child ->getMediaGallery ('images ' );
100
132
$ attributes ['store_id ' ] = $ child ->getStoreId ();
133
+ $ attributes ['media_gallery_entries ' ] = $ this ->getMediaEntries ($ images );
101
134
/** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
102
135
$ productDataObject = $ this ->productFactory ->create ();
103
136
$ this ->dataObjectHelper ->populateWithArray (
@@ -109,6 +142,21 @@ public function getChildren($sku)
109
142
}
110
143
return $ childrenList ;
111
144
}
145
+ public function getMediaEntries ($ images )
146
+ {
147
+ $ media = $ this ->myModelFactory ->create ();
148
+ $ mediaGalleryEntries =[];
149
+ foreach ($ images as $ image ) {
150
+ $ media ->setId ($ image ["value_id " ]);
151
+ $ media ->setMediaType ($ image ["media_type " ]);
152
+ $ media ->setLabel ($ image ["label " ]);
153
+ $ media ->setPosition ($ image ["position " ]);
154
+ $ media ->setDisabled ($ image ["disabled " ]);
155
+ $ media ->setFile ($ image ["file " ]);
156
+ $ mediaGalleryEntries []=$ media ->getData ();
157
+ }
158
+ return $ mediaGalleryEntries ;
159
+ }
112
160
113
161
/**
114
162
* @inheritdoc
0 commit comments