I had a same situation.
I imported products with images, but I wanted to hide the first image of all products on the detail pages.
(This first image is set as small_image, thumbnail_image, and swatch_image in the product import CSV. I want them to show up on the product list pages, but I do NOT want them to show up on the galley section of the product detail pages.)
I searched online, but there seem to be no easy solution to achieve this.
So, I decided to create a comnand line custom PHP script to change DB values without using Magento2 PHP classes.
(It is NOT advised to directly change DB values. Please use this solution with your own discretion. Always take DB backups before trying this solution.)
As a preparation, I checked how the DB data is changed by setting "Hide from Product Page". I changed it for one product, then compared DB dumps of BEFORE and AFTER states.
It turned out catalog_product_entity_media_gallery_value is the only table affected by this setting change.
This is the BEFORE state for product entity_id=42
This is the AFTER state of product entity_id=42
As you can see, "value_id", "stor_id", "entity_id", "position", and "disabled" are unchanged. However, the last field "record_id" is updated.
Also, the "disabled" value is changed from 0 to 1 for the 2nd entry with "position value == 1".
It turned out, every time I updated the image settings on a product, the entries on this table are re-created with new "record_id" field values.
With this knowledge, I created this command line PHP script (file name: img-update.php).
<?php
// create ID list for products
$idList = array();
$handle = fopen("img-update-product-id-list.txt", "r");
if ($handle) {
while (($id = fgets($handle)) !== false) {
$idList[] = rtrim($id);
}
fclose($handle);
}
$idListStr = join(",", $idList);
// create PHP array representing catalog_product_entity_media_gallery_value data
$productList = array();
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
echo "Connection success\n";
$sql1 = "SELECT * FROM `catalog_product_entity_media_gallery_value` WHERE `entity_id` IN (" . $idListStr . ")";
$result = $conn->query($sql1);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$productList[] = array(
"value_id" => $row["value_id"],
"store_id" => $row["store_id"],
"entity_id" => $row["entity_id"],
"position" => $row["position"],
"disabled" => $row["disabled"],
"record_id" => $row["record_id"],
"is2nd" => false,
);
}
} else {
echo "0 results.\n";
}
// determine the 2nd entry for position 1 image which activates "Hide from Product Page" setting
$entityIdList = array();
foreach($productList as $key => $p) {
if ($p['position'] == 1) {
if (in_array($p['entity_id'], $entityIdList)) {
$productList[$key]['is2nd'] = true;
} else {
$entityIdList[] = $p['entity_id'];
}
}
}
// update entries
$value_id = $store_id = $entity_id = $position = $disabled = "";
$sql2 = "ALTER TABLE `catalog_product_entity_media_gallery_value` DISABLE KEYS";
$conn->query($sql2);
foreach($productList as $index => $p) {
$value_id = $p["value_id"];
$store_id = $p["store_id"];
$entity_id = $p["entity_id"];
$position = $p["position"];
$disabled = $p["disabled"];
$is2nd = $p["is2nd"];
if ($is2nd) {
$sql2 = "INSERT INTO `catalog_product_entity_media_gallery_value` (`value_id`, `store_id`, `entity_id`, `label`, `position`, `disabled`) VALUES (" . $value_id . "," . $store_id . "," . $entity_id . ",''," . $position . ",1)";
} else {
$sql2 = "INSERT INTO `catalog_product_entity_media_gallery_value` (`value_id`, `store_id`, `entity_id`, `label`, `position`, `disabled`) VALUES (" . $value_id . "," . $store_id . "," . $entity_id . ",''," . $position . ",0)";
}
$conn->query($sql2);
echo '+'; // progress indicator
}
$sql2 = "ALTER TABLE `catalog_product_entity_media_gallery_value` ENABLE KEYS";
$conn->query($sql2);
echo "\n";
// remove old entries
$sql3 = "ALTER TABLE `catalog_product_entity_media_gallery_value` DISABLE KEYS";
$conn->query($sql3);
foreach($productList as $index => $p) {
$value_id = $p["value_id"];
$store_id = $p["store_id"];
$entity_id = $p["entity_id"];
$position = $p["position"];
$disabled = $p["disabled"];
$record_id = $p["record_id"];
$sql3 = "DELETE FROM `catalog_product_entity_media_gallery_value` WHERE `value_id` = " . $value_id . " AND `store_id` = " . $store_id . " AND `entity_id` = " . $entity_id . " AND `position` = " . $position . " AND `record_id` = " . $record_id;
$conn->query($sql3);
echo '-'; // progress indicator
}
$sql3 = "ALTER TABLE `catalog_product_entity_media_gallery_value` ENABLE KEYS";
$conn->query($sql3);
echo "\n";
$conn->close();
There is a companion text file used with this PHP script. The file name is img-update-product-id-list.txt.
41
42
43
....
....
....
860
861
862
863
864
865
Place both img-update.php and img-update-product-id-list.txt file to the root directory of Magento 2 installation, and run the script as php img-update.php from your terminal.
Modify the .txt and .php files to suite your setup.