Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $product->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);
- $product->save();
- $product->setStoreId(0)->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);
- $product->save();
- <?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 successn";
- $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();
- 41
- 42
- 43
- ....
- ....
- ....
- 860
- 861
- 862
- 863
- 864
- 865
Add Comment
Please, Sign In to add comment