@@ -109,7 +109,8 @@ public function __construct(
109
109
public function saveMediaGallery (array $ mediaGalleryData )
110
110
{
111
111
$ this ->initMediaGalleryResources ();
112
- $ mediaGalleryDataGlobal = $ this ->processMediaGallery ($ mediaGalleryData );
112
+ $ mediaGalleryData = $ this ->restoreDisableImage ($ mediaGalleryData );
113
+ $ mediaGalleryDataGlobal = array_merge (...$ mediaGalleryData );
113
114
$ imageNames = [];
114
115
$ multiInsertData = [];
115
116
$ valueToProductId = [];
@@ -136,25 +137,22 @@ public function saveMediaGallery(array $mediaGalleryData)
136
137
if (!empty ($ multiInsertData )) {
137
138
$ this ->connection ->insertOnDuplicate ($ this ->mediaGalleryTableName , $ multiInsertData );
138
139
}
139
- $ multiInsertData = [];
140
140
$ newMediaSelect = $ this ->connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' ])
141
141
->where ('value IN (?) ' , $ imageNames );
142
142
if (array_keys ($ oldMediaValues )) {
143
143
$ newMediaSelect ->where ('value_id NOT IN (?) ' , array_keys ($ oldMediaValues ));
144
144
}
145
-
146
- $ dataForSkinnyTable = [];
147
145
$ newMediaValues = $ this ->connection ->fetchAssoc ($ newMediaSelect );
148
- $ this ->restoreDisableImage ($ mediaGalleryData );
149
146
foreach ($ mediaGalleryData as $ storeId => $ mediaGalleryDataPerStore ) {
147
+ $ dataForSkinnyTable = [];
148
+ $ multiInsertData = [];
150
149
foreach ($ mediaGalleryDataPerStore as $ productSku => $ mediaGalleryRows ) {
151
150
foreach ($ mediaGalleryRows as $ insertValue ) {
152
151
foreach ($ newMediaValues as $ value_id => $ values ) {
153
152
if ($ values ['value ' ] == $ insertValue ['value ' ]) {
154
153
$ 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 );
158
156
break ;
159
157
}
160
158
}
@@ -175,23 +173,23 @@ public function saveMediaGallery(array $mediaGalleryData)
175
173
}
176
174
}
177
175
}
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
+ }
195
193
}
196
194
}
197
195
@@ -343,24 +341,6 @@ private function restoreDisableImage(array $mediaGalleryData)
343
341
return $ mediaGalleryData ;
344
342
}
345
343
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
-
364
344
/**
365
345
* Get product entity link field.
366
346
*
0 commit comments