Created
October 30, 2023 07:57
-
-
Save magefast/3b3afebd3571d49bde2705441894f24d to your computer and use it in GitHub Desktop.
#Magento2 Working with product images, mass checking/updating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| ini_set('display_errors', 1); | |
| ini_set('display_startup_errors', 1); | |
| error_reporting(E_ALL); | |
| require_once __DIR__ . '/../app/bootstrap.php'; | |
| use Magento\Framework\App\Bootstrap; | |
| $bootstrap = Bootstrap::create(BP, $_SERVER); | |
| $objectManager = $bootstrap->getObjectManager(); | |
| $state = $objectManager->get('Magento\Framework\App\State'); | |
| $state->setAreaCode('adminhtml'); | |
| $collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); | |
| $collection = $collection->create(); | |
| $collection->addAttributeToSelect('entity_id')->addAttributeToSelect('sku'); | |
| $arrayMedia = []; | |
| foreach ($collection as $product) { | |
| if ($product->getStatus() == 1) { | |
| $galleryReadHandler = $objectManager->get('Magento\Catalog\Model\Product\Gallery\ReadHandler'); | |
| $galleryReadHandler->execute($product); | |
| $gallery = $product->getData('media_gallery'); | |
| if (isset($gallery['images'])) { | |
| foreach ($gallery['images'] as $key => $value) { | |
| $arrayMedia[$value['value_id']] = ['id' => $value['value_id'], 'sku' => $product->getSku(), 'label' => $value['label'], 'file' => $value['file']]; | |
| } | |
| } | |
| unset($galleryReadHandler, $gallery); | |
| } | |
| } | |
| unset($collection); | |
| $limit = 30000; | |
| echo '<pre>'; | |
| echo '<ul style="list-style: none; | |
| background: yellow;">'; | |
| $skip = 10000; | |
| foreach ($arrayMedia as $a) { | |
| if ($a['label'] == 'report') { | |
| continue; | |
| } | |
| if ($limit == 0) { | |
| // break; | |
| } | |
| $limit--; | |
| $skip--; | |
| //if($skip>0) {continue;} | |
| echo '<li style="float:left"> | |
| <input | |
| style="display: block;float: left;height: 100;overflow: hidden;" | |
| name="ids" | |
| type="checkbox" | |
| value="' . $a['id'] . '" | |
| id="checkbox' . $a['id'] . '" | |
| /><label for="checkbox' . $a['id'] . '"><img src="https://site.com/media/catalog/product/' . $a['file'] . '" width="100" height="100" /><strong>' . $a['label'] . '</strong></label> | |
| </li>'; | |
| } | |
| echo '</ul>'; | |
| echo ' | |
| <div> | |
| <button onclick="collect();">Get checked Checkbox</button> | |
| <textarea id="result" cols="100" rows="100"></textarea> | |
| </div> | |
| <script> | |
| function getCheckedBoxes(chkboxName) { | |
| let checkboxes = document.getElementsByName(chkboxName); | |
| let checkboxesChecked = []; | |
| for (let i=0; i<checkboxes.length; i++) { | |
| if (checkboxes[i].checked) { | |
| checkboxesChecked.push(checkboxes[i].value); | |
| } | |
| } | |
| return checkboxesChecked.length > 0 ? checkboxesChecked : null; | |
| } | |
| function collect() { | |
| let checkedBoxes = getCheckedBoxes("ids"); | |
| console.log(checkedBoxes); | |
| let textarea = document.getElementById("result"); | |
| for (let i = 0; i < checkedBoxes.length; i ++ ) | |
| { | |
| textarea.value += "UPDATE `catalog_product_entity_media_gallery_value` SET `label` = \'report\' WHERE `catalog_product_entity_media_gallery_value`.`value_id` = "+checkedBoxes[i]+";\n"; | |
| } | |
| } | |
| </script> | |
| '; | |
| die('----'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#Magento2 Working with product images, mass checking/updating