Index: /lib/wpsg_imagehandler.class.php
===================================================================
--- /lib/wpsg_imagehandler.class.php	(revision 7841)
+++ /lib/wpsg_imagehandler.class.php	(revision 7842)
@@ -139,5 +139,5 @@
 			// Die Sortierung steht in der Spalte postids im Produkt
 			$postids = $this->db->fetchOne("SELECT `postids` FROM `".WPSG_TBL_PRODUCTS."` WHERE `id` = '".wpsg_q($product_id)."' ");
-						
+
 			if (strpos($postids, ',') !== false) {
 				
@@ -150,7 +150,8 @@
 				$arDiff = array_diff($arAttachmentIDs, $arPostIDsOrder);
 				$arAttachmentIDs = $arAttachmentIDsIntersect + $arDiff;
-				
-				
-			}  
+
+
+			}
+
 
 			// Jetzt sind in $arAttachmentIDs alle Bilder dieses Produktes
@@ -177,16 +178,10 @@
 					
 				}
-				
+
 				$arAttachmentIDsProductVari = explode(',', $images);
-
-				// Globale Bilder hinzufÃŒgen
-				$vari_images = $this->shop->get_option('wpsg_mod_productvariants_global_images');
-				if (!is_array($vari_images)) $vari_images = [];
-
-				if (isset($vari_images[intval($vari_id)])) $arAttachmentIDs = array_merge($arAttachmentIDs, $vari_images[intval($vari_id)]);
 
 				// Die Elemente, in der Reihenfolge aus $arAttachmentIDs. die auch in $arAttachmentIDsProductVari enthalten sind
 				$arAttachmentIDsIntersect = array_intersect($arAttachmentIDsProductVari, $arAttachmentIDs);
-				
+
 				// Die Elemente, die in $arAttachment drin sind, aber nicht in $arAttachmentIDsProductVari
 				$arDiff = array_diff($arAttachmentIDs, $arAttachmentIDsProductVari);
@@ -224,5 +219,5 @@
 
 						$arAttachmentIDs = wpsg_trim(array_unique(array_merge(
-							$arAttachmentIDs, explode(',', $variation_data['images_set'])
+							$arAttachmentIDs, $variation_data['images_global']
 						)));
 
Index: /mods/wpsg_mod_productvariants.class.php
===================================================================
--- /mods/wpsg_mod_productvariants.class.php	(revision 7841)
+++ /mods/wpsg_mod_productvariants.class.php	(revision 7842)
@@ -44,6 +44,5 @@
 		} // public function __construct()
 
-		public function install()
-		{
+		public function install() {
 
 			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
@@ -70,4 +69,5 @@
 				deleted int(1) NOT NULL,
 				pos int(11) NOT NULL,
+				images text NOT NULL,
 				KEY variant_id (variant_id),
 				PRIMARY KEY  (id)
@@ -1520,12 +1520,14 @@
 		private function admin_removeImageAction() {
 
-			$vari_images = $this->shop->get_option('wpsg_mod_productvariants_global_images');
-			if (!is_array($vari_images)) $vari_images = [];
-
 			$vari_id = intval($_REQUEST['vari_id']);
 
-			if (!isset($vari_images[$vari_id]) || !is_array($vari_images[$vari_id])) $vari_images[$vari_id] = [];
-
-			$vari_images[$vari_id] = array_filter($vari_images[$vari_id], function($e) {
+			if (!wpsg_isSizedInt($vari_id)) exit;
+
+			$vari_db = $this->db->fetchRow("SELECT `id`, `images` FROM `".WPSG_TBL_VARIANTS_VARI."` WHERE `id` = '".wpsg_q($vari_id)."' ");
+
+			if (intval($vari_db['id']) !== $vari_id) exit;
+
+			$arImages = wpsg_trim(explode(',', $vari_db['images']));
+			$arImages = array_filter($arImages, function($e) {
 
 				return intval($e) !== intval($_REQUEST['attachment_id']);
@@ -1533,5 +1535,7 @@
 			});
 
-			$this->shop->update_option('wpsg_mod_productvariants_global_images', $vari_images);
+			$this->db->UpdateQuery(WPSG_TBL_VARIANTS_VARI, [
+				'images' => wpsg_q(implode(',', $arImages))
+			], " `id` = '".wpsg_q($vari_id)."' ");
 
 			exit;
@@ -1541,24 +1545,25 @@
 		private function admin_uploadAction() {
 
-			$vari_images = $this->shop->get_option('wpsg_mod_productvariants_global_images');
-			if (!is_array($vari_images)) $vari_images = [];
-
 			$vari_id = intval($_REQUEST['vari_id']);
 
 			if (!wpsg_isSizedInt($vari_id)) exit;
 
-			if (!is_array($vari_images[$vari_id])) $vari_images[$vari_id] = [];
-
-			foreach ($_REQUEST['arAtt'] as $a) {
-
-				$post_id = intval($a);
-
-				if ($post_id > 0) $vari_images[$vari_id][] = $post_id;
-
-			}
-
-			$vari_images[$vari_id] = array_unique($vari_images[$vari_id]);
-
-			$this->shop->update_option('wpsg_mod_productvariants_global_images', $vari_images);
+			$vari_db = $this->db->fetchRow("SELECT `id`, `images` FROM `".WPSG_TBL_VARIANTS_VARI."` WHERE `id` = '".wpsg_q($vari_id)."' ");
+
+			if (intval($vari_db['id']) !== $vari_id) exit;
+
+			$arImages = wpsg_trim(explode(',', $vari_db['images']));
+
+			foreach ($_REQUEST['arAtt'] as $attachment_id) {
+
+				$attachment_id = intval($attachment_id);
+
+				if ($attachment_id > 0) $arImages[] = $attachment_id;
+
+			}
+
+			$this->db->UpdateQuery(WPSG_TBL_VARIANTS_VARI, [
+				'images' => wpsg_q(implode(',', $arImages))
+			], " `id` = '".wpsg_q($vari_id)."' ");
 
 			$this->admin_editAction();
@@ -1630,5 +1635,5 @@
 			$strQuery = "
 				SELECT
-					VI.`id`, VI.`name`, VI.`shortname`, VI.`deleted`, VI.`pos`
+					VI.`id`, VI.`name`, VI.`shortname`, VI.`deleted`, VI.`pos`, VI.`images`
 					".$strQuerySELECT."
 				FROM
@@ -1649,4 +1654,10 @@
 
 			$arVari = $this->db->fetchAssoc($strQuery, "id");
+
+			array_walk($arVari, function(&$e) {
+
+				$e['images'] = wpsg_trim(explode(',', $e['images']));
+
+			});
 
 			// Daschmi: 
@@ -1983,7 +1994,4 @@
 			", "id");
 
-			$vari_images = $this->shop->get_option('wpsg_mod_productvariants_global_images');
-			if (!is_array($vari_images)) $vari_images = [];
-
 			foreach ($arData as $k => $v) {
 
@@ -2002,5 +2010,5 @@
 						$arData[$k]['arVariation'] = $this->db->fetchAssoc("
 							SELECT
-								PVI.*,
+								PVI.*, VI.`images` AS `global_images`,
 								VI.`name`, VI.`shortname`, VI.`deleted`, VI.`pos`
 							FROM
@@ -2016,21 +2024,19 @@
 						", "variation_id");
 
-						array_walk($arData[$k]['arVariation'], function(&$e) use ($vari_images) {
-
-							if (isset($vari_images[intval($e['variation_id'])])) {
-
-								$images = explode(',', $e['images']);
-								$images_set = explode(',', $e['images_set']);
-
-								$images = array_merge($images, $vari_images[intval($e['variation_id'])]);
-								$images_set = array_merge($images_set, $vari_images[intval($e['variation_id'])]);
-
-								$images = array_unique($images);
-								$images_set = array_unique($images_set);
-
-								$e['images'] = implode(',', $images);
-								$e['images_set'] = implode(',', $images_set);
-
-							}
+						array_walk($arData[$k]['arVariation'], function(&$e) {
+
+							$images = explode(',', $e['images']);
+							$images_set = explode(',', $e['images_set']);
+							$images_global = explode(',', $e['global_images']);
+
+							$images = array_merge($images, $images_global);
+							$images_set = array_merge($images_set, $images_global);
+
+							$images = array_unique($images);
+							$images_set = array_unique($images_set);
+
+							$e['images'] = implode(',', $images);
+							$e['images_set'] = implode(',', $images_set);
+							$e['images_global'] = $images_global;
 
 						});
@@ -2138,5 +2144,7 @@
 			//$vp_data = $this->loadVarianten($produkt_id);
 			$vp_data = $this->getVariants($produkt_id, true, true, true);
-			 
+
+			//wpsg_debug($vp_data);
+
 			$arReturn = array(
 				'product_id' => $produkt_id
@@ -2166,18 +2174,17 @@
 
 			/** Globale Varianten kÃ¶nnten auch erlaubte Bilder haben */
-			$vari_images = $this->shop->get_option('wpsg_mod_productvariants_global_images');
-			if (!is_array($vari_images)) $vari_images = [];
-
-			foreach ($arVari as $k => $var_key) {
-
-				$var_value = intval(preg_replace('/(.*)\:/', '', $var_key));
-
-				if (isset($vari_images[$var_value])) {
-
-					$arImages = array_unique(array_merge($arImages, $vari_images[$var_value]));
-
-				}
-
-			}
+			$arGlobalImages = [];
+			foreach ($vp_data as $var_id => $var_data) {
+
+				foreach ($var_data['arVariation'] as $vari_id => $vari_data) {
+
+					$arGlobalImages = array_merge($arGlobalImages, wpsg_getArray($vari_data['images_global']));
+
+				}
+
+			}
+
+			$arImages = array_merge($arImages, $arGlobalImages);
+			$arImagesFix = [];
 
 			foreach ($arVari as $var_key) {
@@ -2189,8 +2196,17 @@
 
 				/* Bilder dieser Variation */
-				$arImagesVariation = explode(',', @$vp_data[$var_id]['arVariation'][$var_value]['images_set']);
+				$arImagesVariation = wpsg_trim(
+					explode(',', @$vp_data[$var_id]['arVariation'][$var_value]['images_set'])
+				);
+
+				/* Globale Bilder der Variation bleiben erhalten auch wenn nicht ausgewÃ€hlt */
+				$arImagesFix = wpsg_trim(array_merge(
+					$arImagesFix, wpsg_getArray($vp_data[$var_id]['arVariation'][$var_value]['images_global'])
+				));
 
 				/* Schnittmenge */
 				$arImages = array_intersect($arImages, $arImagesVariation);
+
+				foreach ($arImagesFix as $i) if (!in_array($i, $arImages)) $arImages[] = $i;
 
 				$r = array(
Index: /views/mods/mod_productvariants/admin_edit.phtml
===================================================================
--- /views/mods/mod_productvariants/admin_edit.phtml	(revision 7841)
+++ /views/mods/mod_productvariants/admin_edit.phtml	(revision 7842)
@@ -208,8 +208,5 @@
                     } else {
 
-                        $vari_images = $this->get_option('wpsg_mod_productvariants_global_images');
-                        if (!is_array($vari_images)) $vari_images = [];
-
-                        $arAttachmentIDs = ((isset($vari_images[$vari_id]))?$vari_images[$vari_id]:[]);
+                        $arAttachmentIDs = $vari['images'];
 
                     }
Index: /views/mods/mod_productvariants/produkt.phtml
===================================================================
--- /views/mods/mod_productvariants/produkt.phtml	(revision 7841)
+++ /views/mods/mod_productvariants/produkt.phtml	(revision 7842)
@@ -74,5 +74,5 @@
 									if ($vari['active'] == '1' && (!$this->hasMod('wpsg_mod_stock') || $vari['stock'] > 0)) { ?>
 
-										<?php $simulated_product_key = $this->callMod('wpsg_mod_productvariants', 'getSimulatedVariKey', array($this->view['data']['product_key'], $var['id'], $vari['id'])); ?>
+										<?php $simulated_product_key = $this->callMod('wpsg_mod_productvariants', 'getSimulatedVariKey', array($this->view['data']['product_key'], $var['id'], $vari['variation_id'])); ?>
 										<?php $attachmentID = $this->imagehandler->getAttachmentID($simulated_product_key); ?>
 										
Index: /views/produkttemplates/standard.phtml
===================================================================
--- /views/produkttemplates/standard.phtml	(revision 7841)
+++ /views/produkttemplates/standard.phtml	(revision 7842)
@@ -41,8 +41,8 @@
 			/* Das ist das primÃ€re Anzeigebild des Produktes (AttachmentID) */
 			$arAttachmentID = $this->imagehandler->getAttachmentID($this->view['data']['product_key']);
-		
+
 			/* Array mit allen AttachmentIDs der Produktbilder */
 			$arAttachmentIDsAll = $this->imagehandler->getAttachmentIDs($this->view['data']['product_id']);
-
+ 
 		?>
 
