If you would like to alter the behavior of how the gallery switching happens, I think the function you are looking for is in
vendor/magento/module-swatches/view/frontend/web/js/swatch-renderer.js
called processUpdateBaseImage
This is completely untested, but you can create a mixin and change that function's behavior, something like:
Namespace/Module/view/frontend/requirejs-config.js -
var config = {
config: {
mixins: {
'Magento_Swatches/js/swatch-renderer': {
'Namespace_Module/js/swatch-renderer-mixin': true
},
}
}
};
Namespace/Module/view/web/js/swatch-renderer-mixin.js -
define(['jquery'], function ($) {
'use strict';
var mixin = {
/**
* Update [gallery-placeholder] or [product-image-photo]
* @param {Array} images
* @param {jQuery} context
* @param {Boolean} isInProductView
* @param {Object} gallery
*/
processUpdateBaseImage: function (images, context, isInProductView, gallery) {
var justAnImage = images[0],
initialImages = this.options.mediaGalleryInitial,
imagesToUpdate,
isInitial;
if (isInProductView) {
imagesToUpdate = images.length ? this._setImageType($.extend(true, [], images)) : [];
isInitial = _.isEqual(imagesToUpdate, initialImages);
if (this.options.gallerySwitchStrategy === 'prepend' && !isInitial) {
imagesToUpdate = imagesToUpdate.concat(initialImages);
}
imagesToUpdate = this._setImageIndex(imagesToUpdate);
gallery.updateData(imagesToUpdate);
if (isInitial) {
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
} else {
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents({
selectedOption: this.getProduct(),
dataMergeStrategy: this.options.gallerySwitchStrategy
});
}
gallery.first();
} else if (justAnImage && justAnImage.img) {
context.find('.product-image-photo').attr('src', justAnImage.img);
}
}
};
return function (target) {
return target.extend(mixin);
}
});