效果如下图所示:
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区间(此数字可自行修改)的所有产品,
可以有效解决后台删除大量产品时连接超时的问题。
付费内容限时免费中...
美化代码及有可能后台的SESSIon记录的产品集有筛选。因此可以使用以下代码
<?php require __DIR__ . '../../app/bootstrap.php'; use Magento\Framework\App\Bootstrap; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $registry = $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true); $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollection->create()->addAttributeToSelect('*')->load(); $app_state = $objectManager->get('\Magento\Framework\App\State'); $app_state->setAreaCode('frontend'); foreach ($collection as $product) { $id = $product->getId(); if($id >105&& $id<200) { try { echo 'Deleted '.$product->getName()."\r\n"; $product->delete(); } catch (Exception $e) { echo 'Unable to delete product '.$product->getName()."\r\n"; echo $e->getMessage() . "\r\n"; } } }