Guest User

Untitled

a guest
Oct 20th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. $product->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);
  2. $product->save();
  3.  
  4. $product->setStoreId(0)->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);
  5. $product->save();
  6.  
  7. <?php
  8.  
  9. // create ID list for products
  10.  
  11. $idList = array();
  12. $handle = fopen("img-update-product-id-list.txt", "r");
  13. if ($handle) {
  14. while (($id = fgets($handle)) !== false) {
  15. $idList[] = rtrim($id);
  16. }
  17. fclose($handle);
  18. }
  19. $idListStr = join(",", $idList);
  20.  
  21. // create PHP array representing catalog_product_entity_media_gallery_value data
  22.  
  23. $productList = array();
  24.  
  25. $servername = "localhost";
  26. $username = "";
  27. $password = "";
  28. $dbname = "";
  29. $conn = new mysqli($servername, $username, $password, $dbname);
  30.  
  31. if ($conn->connect_error) {
  32. die("Connection failed: ". $conn->connect_error);
  33. }
  34. echo "Connection successn";
  35.  
  36. $sql1 = "SELECT * FROM `catalog_product_entity_media_gallery_value` WHERE `entity_id` IN (" . $idListStr . ")";
  37. $result = $conn->query($sql1);
  38. if ($result->num_rows > 0) {
  39. while ($row = $result->fetch_assoc()) {
  40. $productList[] = array(
  41. "value_id" => $row["value_id"],
  42. "store_id" => $row["store_id"],
  43. "entity_id" => $row["entity_id"],
  44. "position" => $row["position"],
  45. "disabled" => $row["disabled"],
  46. "record_id" => $row["record_id"],
  47. "is2nd" => false,
  48. );
  49. }
  50. } else {
  51. echo "0 results.n";
  52. }
  53.  
  54. // determine the 2nd entry for position 1 image which activates "Hide from Product Page" setting
  55.  
  56. $entityIdList = array();
  57.  
  58. foreach($productList as $key => $p) {
  59. if ($p['position'] == 1) {
  60. if (in_array($p['entity_id'], $entityIdList)) {
  61. $productList[$key]['is2nd'] = true;
  62. } else {
  63. $entityIdList[] = $p['entity_id'];
  64. }
  65. }
  66. }
  67.  
  68. // update entries
  69.  
  70. $value_id = $store_id = $entity_id = $position = $disabled = "";
  71.  
  72. $sql2 = "ALTER TABLE `catalog_product_entity_media_gallery_value` DISABLE KEYS";
  73. $conn->query($sql2);
  74. foreach($productList as $index => $p) {
  75.  
  76. $value_id = $p["value_id"];
  77. $store_id = $p["store_id"];
  78. $entity_id = $p["entity_id"];
  79. $position = $p["position"];
  80. $disabled = $p["disabled"];
  81.  
  82. $is2nd = $p["is2nd"];
  83.  
  84. if ($is2nd) {
  85. $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)";
  86. } else {
  87. $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)";
  88. }
  89.  
  90. $conn->query($sql2);
  91. echo '+'; // progress indicator
  92. }
  93. $sql2 = "ALTER TABLE `catalog_product_entity_media_gallery_value` ENABLE KEYS";
  94. $conn->query($sql2);
  95.  
  96. echo "n";
  97.  
  98. // remove old entries
  99.  
  100. $sql3 = "ALTER TABLE `catalog_product_entity_media_gallery_value` DISABLE KEYS";
  101. $conn->query($sql3);
  102. foreach($productList as $index => $p) {
  103.  
  104. $value_id = $p["value_id"];
  105. $store_id = $p["store_id"];
  106. $entity_id = $p["entity_id"];
  107. $position = $p["position"];
  108. $disabled = $p["disabled"];
  109.  
  110. $record_id = $p["record_id"];
  111.  
  112. $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;
  113. $conn->query($sql3);
  114. echo '-'; // progress indicator
  115. }
  116. $sql3 = "ALTER TABLE `catalog_product_entity_media_gallery_value` ENABLE KEYS";
  117. $conn->query($sql3);
  118.  
  119. echo "n";
  120.  
  121. $conn->close();
  122.  
  123. 41
  124. 42
  125. 43
  126. ....
  127. ....
  128. ....
  129. 860
  130. 861
  131. 862
  132. 863
  133. 864
  134. 865
Add Comment
Please, Sign In to add comment