@@ -326,13 +326,40 @@ protected function processNewAndExistingImages($product, array &$images)
326326
327327 $ data [$ this ->metadata ->getLinkField ()] = (int )$ product ->getData ($ this ->metadata ->getLinkField ());
328328
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+ );
331335 }
336+
337+ $ this ->saveGalleryStoreValue ($ data , $ isNew );
332338 }
333339 }
334340 }
335341
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+
336363 /**
337364 * Get existing gallery store values
338365 *
@@ -345,50 +372,18 @@ private function getExistingGalleryStoreValues(Product $product): array
345372 $ existingMediaGalleryValues = [];
346373 if (!$ product ->isObjectNew ()) {
347374 $ 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 );
357376 }
358377 return $ existingMediaGalleryValues ;
359378 }
360379
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-
384380 /**
385381 * Save media gallery store value
386382 *
387- * @param Product $product
388383 * @param array $data
389384 * @param bool $isNewImage
390385 */
391- private function saveGalleryStoreValue (Product $ product , array $ data , bool $ isNewImage ): void
386+ private function saveGalleryStoreValue (array $ data , bool $ isNewImage ): void
392387 {
393388 $ items = [];
394389 $ items [] = $ data ;
@@ -401,14 +396,7 @@ private function saveGalleryStoreValue(Product $product, array $data, bool $isNe
401396 }
402397
403398 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 );
412400 }
413401 }
414402
0 commit comments