@@ -326,13 +326,40 @@ protected function processNewAndExistingImages($product, array &$images)
326
326
327
327
$ data [$ this ->metadata ->getLinkField ()] = (int )$ product ->getData ($ this ->metadata ->getLinkField ());
328
328
329
- if ($ isNew || $ this ->hasGalleryStoreValueChanged ($ data , $ existingGalleryStoreValues )) {
330
- $ this ->saveGalleryStoreValue ($ product , $ data , $ isNew );
329
+ if (!$ isNew ) {
330
+ $ data += (array ) $ this ->getExistingGalleryStoreValue (
331
+ $ existingGalleryStoreValues ,
332
+ $ data ['value_id ' ],
333
+ $ data ['store_id ' ]
334
+ );
331
335
}
336
+
337
+ $ this ->saveGalleryStoreValue ($ data , $ isNew );
332
338
}
333
339
}
334
340
}
335
341
342
+ /**
343
+ * Returns existing gallery store value by value id and store id
344
+ *
345
+ * @param array $existingGalleryStoreValues
346
+ * @param int $valueId
347
+ * @param int $storeId
348
+ * @return array|null
349
+ */
350
+ private function getExistingGalleryStoreValue (array $ existingGalleryStoreValues , int $ valueId , int $ storeId ): ?array
351
+ {
352
+ foreach ($ existingGalleryStoreValues as $ existingGalleryStoreValue ) {
353
+ if (((int ) $ existingGalleryStoreValue ['value_id ' ]) === $ valueId
354
+ && ((int ) $ existingGalleryStoreValue ['store_id ' ]) === $ storeId
355
+ ) {
356
+ return $ existingGalleryStoreValue ;
357
+ }
358
+ }
359
+
360
+ return null ;
361
+ }
362
+
336
363
/**
337
364
* Get existing gallery store values
338
365
*
@@ -345,50 +372,18 @@ private function getExistingGalleryStoreValues(Product $product): array
345
372
$ existingMediaGalleryValues = [];
346
373
if (!$ product ->isObjectNew ()) {
347
374
$ productId = (int )$ product ->getData ($ this ->metadata ->getLinkField ());
348
- foreach ($ this ->mediaGalleryValue ->getAllByEntityId ($ productId ) as $ data ) {
349
- $ existingMediaGalleryValues [] = [
350
- 'value_id ' => (int ) $ data ['value_id ' ],
351
- 'store_id ' => (int ) $ data ['store_id ' ],
352
- 'label ' => $ data ['label ' ] ?: null ,
353
- 'position ' => $ data ['position ' ] !== null ? (int )$ data ['position ' ] : null ,
354
- 'disabled ' => (int ) $ data ['disabled ' ],
355
- ];
356
- }
375
+ $ existingMediaGalleryValues = $ this ->mediaGalleryValue ->getAllByEntityId ($ productId );
357
376
}
358
377
return $ existingMediaGalleryValues ;
359
378
}
360
379
361
- /**
362
- * Check if gallery store value has changed
363
- *
364
- * @param array $data
365
- * @param array $existingGalleryStoreValues
366
- * @return bool
367
- */
368
- private function hasGalleryStoreValueChanged (array $ data , array $ existingGalleryStoreValues ): bool
369
- {
370
- foreach ($ existingGalleryStoreValues as $ existingGalleryStoreValue ) {
371
- if ($ existingGalleryStoreValue ['value_id ' ] === $ data ['value_id ' ]
372
- && $ existingGalleryStoreValue ['store_id ' ] === $ data ['store_id ' ]
373
- && $ existingGalleryStoreValue ['label ' ] === $ data ['label ' ]
374
- && $ existingGalleryStoreValue ['position ' ] === $ data ['position ' ]
375
- && $ existingGalleryStoreValue ['disabled ' ] === $ data ['disabled ' ]
376
- ) {
377
- return false ;
378
- }
379
- }
380
-
381
- return true ;
382
- }
383
-
384
380
/**
385
381
* Save media gallery store value
386
382
*
387
- * @param Product $product
388
383
* @param array $data
389
384
* @param bool $isNewImage
390
385
*/
391
- private function saveGalleryStoreValue (Product $ product , array $ data , bool $ isNewImage ): void
386
+ private function saveGalleryStoreValue (array $ data , bool $ isNewImage ): void
392
387
{
393
388
$ items = [];
394
389
$ items [] = $ data ;
@@ -401,14 +396,7 @@ private function saveGalleryStoreValue(Product $product, array $data, bool $isNe
401
396
}
402
397
403
398
foreach ($ items as $ item ) {
404
- if (!$ product ->isObjectNew ()) {
405
- $ this ->resourceModel ->deleteGalleryValueInStore (
406
- $ item ['value_id ' ],
407
- $ item [$ this ->metadata ->getLinkField ()],
408
- $ item ['store_id ' ]
409
- );
410
- }
411
- $ this ->resourceModel ->insertGalleryValueInStore ($ item );
399
+ $ this ->mediaGalleryValue ->saveGalleryStoreValue ($ item );
412
400
}
413
401
}
414
402
0 commit comments