@@ -109,7 +109,7 @@ public function __construct(
109109 public function saveMediaGallery (array $ mediaGalleryData )
110110 {
111111 $ this ->initMediaGalleryResources ();
112- $ mediaGalleryData = $ this ->restoreDisableImage ($ mediaGalleryData );
112+ $ mediaGalleryData = $ this ->restoreDisabledImage ($ mediaGalleryData );
113113 $ mediaGalleryDataGlobal = array_merge (...$ mediaGalleryData );
114114 $ imageNames = [];
115115 $ multiInsertData = [];
@@ -143,53 +143,8 @@ public function saveMediaGallery(array $mediaGalleryData)
143143 $ newMediaSelect ->where ('value_id NOT IN (?) ' , array_keys ($ oldMediaValues ));
144144 }
145145 $ newMediaValues = $ this ->connection ->fetchAssoc ($ newMediaSelect );
146- foreach ($ mediaGalleryData as $ storeId => $ mediaGalleryDataPerStore ) {
147- $ dataForSkinnyTable = [];
148- $ multiInsertData = [];
149- foreach ($ mediaGalleryDataPerStore as $ productSku => $ mediaGalleryRows ) {
150- foreach ($ mediaGalleryRows as $ insertValue ) {
151- foreach ($ newMediaValues as $ value_id => $ values ) {
152- if ($ values ['value ' ] == $ insertValue ['value ' ]) {
153- $ insertValue ['value_id ' ] = $ value_id ;
154- $ ids = array_values ($ valueToProductId [$ values ['value ' ]]);
155- $ insertValue [$ this ->getProductEntityLinkField ()] = array_shift ($ ids );
156- break ;
157- }
158- }
159- if (isset ($ insertValue ['value_id ' ])) {
160- $ valueArr = [
161- 'value_id ' => $ insertValue ['value_id ' ],
162- 'store_id ' => $ storeId ,
163- $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
164- 'label ' => $ insertValue ['label ' ],
165- 'position ' => $ insertValue ['position ' ],
166- 'disabled ' => $ insertValue ['disabled ' ],
167- ];
168- $ multiInsertData [] = $ valueArr ;
169- $ dataForSkinnyTable [] = [
170- 'value_id ' => $ insertValue ['value_id ' ],
171- $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
172- ];
173- }
174- }
175- }
176- try {
177- $ this ->connection ->insertOnDuplicate (
178- $ this ->mediaGalleryValueTableName ,
179- $ multiInsertData ,
180- ['value_id ' , 'store_id ' , $ this ->getProductEntityLinkField (), 'label ' , 'position ' , 'disabled ' ]
181- );
182- $ this ->connection ->insertOnDuplicate (
183- $ this ->mediaGalleryEntityToValueTableName ,
184- $ dataForSkinnyTable ,
185- ['value_id ' ]
186- );
187- } catch (\Exception $ e ) {
188- $ this ->connection ->delete (
189- $ this ->mediaGalleryTableName ,
190- $ this ->connection ->quoteInto ('value_id IN (?) ' , $ newMediaValues )
191- );
192- }
146+ foreach ($ mediaGalleryData as $ storeId => $ storeMediaGalleryData ) {
147+ $ this ->processMediaPerStore ((int )$ storeId , $ storeMediaGalleryData , $ newMediaValues , $ valueToProductId );
193148 }
194149 }
195150
@@ -314,7 +269,7 @@ private function initMediaGalleryResources()
314269 * @param array $mediaGalleryData
315270 * @return array
316271 */
317- private function restoreDisableImage (array $ mediaGalleryData )
272+ private function restoreDisabledImage (array $ mediaGalleryData )
318273 {
319274 $ restoreData = [];
320275 foreach (array_keys ($ mediaGalleryData ) as $ storeId ) {
@@ -341,6 +296,70 @@ private function restoreDisableImage(array $mediaGalleryData)
341296 return $ mediaGalleryData ;
342297 }
343298
299+ /**
300+ * Save media gallery data per store.
301+ *
302+ * @param $storeId
303+ * @param array $mediaGalleryData
304+ * @param array $newMediaValues
305+ * @param array $valueToProductId
306+ * @return void
307+ */
308+ private function processMediaPerStore (
309+ int $ storeId ,
310+ array $ mediaGalleryData ,
311+ array $ newMediaValues ,
312+ array $ valueToProductId
313+ ) {
314+ $ multiInsertData = [];
315+ $ dataForSkinnyTable = [];
316+ foreach ($ mediaGalleryData as $ mediaGalleryRows ) {
317+ foreach ($ mediaGalleryRows as $ insertValue ) {
318+ foreach ($ newMediaValues as $ value_id => $ values ) {
319+ if ($ values ['value ' ] == $ insertValue ['value ' ]) {
320+ $ insertValue ['value_id ' ] = $ value_id ;
321+ $ insertValue [$ this ->getProductEntityLinkField ()]
322+ = array_shift ($ valueToProductId [$ values ['value ' ]]);
323+ unset($ newMediaValues [$ value_id ]);
324+ break ;
325+ }
326+ }
327+ if (isset ($ insertValue ['value_id ' ])) {
328+ $ valueArr = [
329+ 'value_id ' => $ insertValue ['value_id ' ],
330+ 'store_id ' => $ storeId ,
331+ $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
332+ 'label ' => $ insertValue ['label ' ],
333+ 'position ' => $ insertValue ['position ' ],
334+ 'disabled ' => $ insertValue ['disabled ' ],
335+ ];
336+ $ multiInsertData [] = $ valueArr ;
337+ $ dataForSkinnyTable [] = [
338+ 'value_id ' => $ insertValue ['value_id ' ],
339+ $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
340+ ];
341+ }
342+ }
343+ }
344+ try {
345+ $ this ->connection ->insertOnDuplicate (
346+ $ this ->mediaGalleryValueTableName ,
347+ $ multiInsertData ,
348+ ['value_id ' , 'store_id ' , $ this ->getProductEntityLinkField (), 'label ' , 'position ' , 'disabled ' ]
349+ );
350+ $ this ->connection ->insertOnDuplicate (
351+ $ this ->mediaGalleryEntityToValueTableName ,
352+ $ dataForSkinnyTable ,
353+ ['value_id ' ]
354+ );
355+ } catch (\Exception $ e ) {
356+ $ this ->connection ->delete (
357+ $ this ->mediaGalleryTableName ,
358+ $ this ->connection ->quoteInto ('value_id IN (?) ' , $ newMediaValues )
359+ );
360+ }
361+ }
362+
344363 /**
345364 * Get product entity link field.
346365 *
0 commit comments