@@ -109,7 +109,8 @@ public function __construct(
109109 public function saveMediaGallery (array $ mediaGalleryData )
110110 {
111111 $ this ->initMediaGalleryResources ();
112- $ mediaGalleryDataGlobal = $ this ->processMediaGallery ($ mediaGalleryData );
112+ $ mediaGalleryData = $ this ->restoreDisableImage ($ mediaGalleryData );
113+ $ mediaGalleryDataGlobal = array_merge (...$ mediaGalleryData );
113114 $ imageNames = [];
114115 $ multiInsertData = [];
115116 $ valueToProductId = [];
@@ -136,25 +137,22 @@ public function saveMediaGallery(array $mediaGalleryData)
136137 if (!empty ($ multiInsertData )) {
137138 $ this ->connection ->insertOnDuplicate ($ this ->mediaGalleryTableName , $ multiInsertData );
138139 }
139- $ multiInsertData = [];
140140 $ newMediaSelect = $ this ->connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' ])
141141 ->where ('value IN (?) ' , $ imageNames );
142142 if (array_keys ($ oldMediaValues )) {
143143 $ newMediaSelect ->where ('value_id NOT IN (?) ' , array_keys ($ oldMediaValues ));
144144 }
145-
146- $ dataForSkinnyTable = [];
147145 $ newMediaValues = $ this ->connection ->fetchAssoc ($ newMediaSelect );
148- $ this ->restoreDisableImage ($ mediaGalleryData );
149146 foreach ($ mediaGalleryData as $ storeId => $ mediaGalleryDataPerStore ) {
147+ $ dataForSkinnyTable = [];
148+ $ multiInsertData = [];
150149 foreach ($ mediaGalleryDataPerStore as $ productSku => $ mediaGalleryRows ) {
151150 foreach ($ mediaGalleryRows as $ insertValue ) {
152151 foreach ($ newMediaValues as $ value_id => $ values ) {
153152 if ($ values ['value ' ] == $ insertValue ['value ' ]) {
154153 $ insertValue ['value_id ' ] = $ value_id ;
155- $ insertValue [$ this ->getProductEntityLinkField ()]
156- = array_shift ($ valueToProductId [$ values ['value ' ]]);
157- unset($ newMediaValues [$ value_id ]);
154+ $ ids = array_values ($ valueToProductId [$ values ['value ' ]]);
155+ $ insertValue [$ this ->getProductEntityLinkField ()] = array_shift ($ ids );
158156 break ;
159157 }
160158 }
@@ -175,23 +173,23 @@ public function saveMediaGallery(array $mediaGalleryData)
175173 }
176174 }
177175 }
178- }
179- try {
180- $ this ->connection -> insertOnDuplicate (
181- $ this -> mediaGalleryValueTableName ,
182- $ multiInsertData ,
183- [ ' value_id ' , ' store_id ' , $ this -> getProductEntityLinkField (), ' label ' , ' position ' , ' disabled ' ]
184- );
185- $ this ->connection -> insertOnDuplicate (
186- $ this -> mediaGalleryEntityToValueTableName ,
187- $ dataForSkinnyTable ,
188- [ ' value_id ' ]
189- );
190- } catch ( \ Exception $ e ) {
191- $ this ->connection -> delete (
192- $ this ->mediaGalleryTableName ,
193- $ this -> connection -> quoteInto ( ' value_id IN (?) ' , $ newMediaValues )
194- );
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+ }
195193 }
196194 }
197195
@@ -343,24 +341,6 @@ private function restoreDisableImage(array $mediaGalleryData)
343341 return $ mediaGalleryData ;
344342 }
345343
346- /**
347- * Remove store specific information for inserting images.
348- *
349- * @param array $mediaGalleryData
350- * @return array
351- */
352- private function processMediaGallery (array $ mediaGalleryData )
353- {
354- $ mediaGalleryDataGlobal = array_merge (...$ mediaGalleryData );
355- foreach ($ mediaGalleryDataGlobal as $ sku => $ row ) {
356- if (isset ($ mediaGalleryDataGlobal [$ sku ]['all ' ]['restore ' ])) {
357- unset($ mediaGalleryDataGlobal [$ sku ]);
358- }
359- }
360-
361- return $ mediaGalleryDataGlobal ;
362- }
363-
364344 /**
365345 * Get product entity link field.
366346 *
0 commit comments