@@ -514,8 +514,13 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
514
514
$ newEntries = $ mediaGalleryEntries ;
515
515
}
516
516
517
- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
518
517
$ images = $ product ->getMediaGallery ('images ' );
518
+ if ($ images ) {
519
+ $ images = $ this ->determineImageRoles ($ product , $ images );
520
+ }
521
+
522
+ $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
523
+
519
524
if ($ images ) {
520
525
foreach ($ images as $ image ) {
521
526
if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
@@ -758,6 +763,32 @@ public function cleanCache()
758
763
$ this ->instancesById = null ;
759
764
}
760
765
766
+ /**
767
+ * Ascertain image roles, if they are not set against the gallery entries
768
+ *
769
+ * @param ProductInterface $product
770
+ * @param array $images
771
+ * @return array
772
+ */
773
+ private function determineImageRoles (ProductInterface $ product , array $ images )
774
+ {
775
+ $ imagesWithRoles = [];
776
+ foreach ($ images as $ image ) {
777
+ if (!isset ($ image ['types ' ])) {
778
+ $ image ['types ' ] = [];
779
+ if (isset ($ image ['file ' ])) {
780
+ foreach (array_keys ($ product ->getMediaAttributes ()) as $ attribute ) {
781
+ if ($ image ['file ' ] == $ product ->getData ($ attribute )) {
782
+ $ image ['types ' ][] = $ attribute ;
783
+ }
784
+ }
785
+ }
786
+ }
787
+ $ imagesWithRoles [] = $ image ;
788
+ }
789
+ return $ imagesWithRoles ;
790
+ }
791
+
761
792
/**
762
793
* @return Product\Gallery\Processor
763
794
*/
0 commit comments