效果如下图所示:
M2.4.5通用支持
<?php require __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('frontend'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $registry = $objectManager->get('Magento\Framework\Registry'); $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollection->create()->addAttributeToSelect('*')->load(); foreach ($collection as $product) { $id = $product->getId(); if($id > 15 && $id < 20) { $registry->register('isSecureArea','true'); $product->delete(); echo "Removed Product".$id."\r\n"; $registry->unregister('isSecureArea'); } }
在pub目录下新增加一PHP文件,并命名为 remove.php
通过shell 执行
php remove.php
执行后,可有效产品ID为15-20区间(此数字可自行修改)的所有产品,
可以有效解决后台删除大量产品时连接超时的问题。