@@ -472,8 +472,13 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
472
472
$ newEntries = $ mediaGalleryEntries ;
473
473
}
474
474
475
- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
476
475
$ images = $ product ->getMediaGallery ('images ' );
476
+ if ($ images ) {
477
+ $ images = $ this ->determineImageRoles ($ product , $ images );
478
+ }
479
+
480
+ $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
481
+
477
482
if ($ images ) {
478
483
foreach ($ images as $ image ) {
479
484
if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
@@ -674,6 +679,32 @@ protected function addFilterGroupToCollection(
674
679
}
675
680
}
676
681
682
+ /**
683
+ * Ascertain image roles, if they are not set against the gallery entries
684
+ *
685
+ * @param ProductInterface $product
686
+ * @param array $images
687
+ * @return array
688
+ */
689
+ private function determineImageRoles (ProductInterface $ product , array $ images )
690
+ {
691
+ $ imagesWithRoles = [];
692
+ foreach ($ images as $ image ) {
693
+ if (!isset ($ image ['types ' ])) {
694
+ $ image ['types ' ] = [];
695
+ if (isset ($ image ['file ' ])) {
696
+ foreach (array_keys ($ product ->getMediaAttributes ()) as $ attribute ) {
697
+ if ($ image ['file ' ] == $ product ->getData ($ attribute )) {
698
+ $ image ['types ' ][] = $ attribute ;
699
+ }
700
+ }
701
+ }
702
+ }
703
+ $ imagesWithRoles [] = $ image ;
704
+ }
705
+ return $ imagesWithRoles ;
706
+ }
707
+
677
708
/**
678
709
* Apply custom filters to product collection.
679
710
*
0 commit comments