M2依据产品ID快速删除产品程序源码

作者:admi... 点击数: 0 收藏到会员中心
最后编辑时间: 2022-10-21 01:59

效果如下图所示:

10202221_01.jpg


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";
    } 
    
            }
     
        }


内容说明:
如您需要转载本文请保留以下信息是对作者发文的支持与尊重:

M2依据产品ID快速删除产品程序源码 来源于 https://www.magentola.com/news-read-326.html
上一篇:奇葩知识:部分M2(含最新2.4.5)版本在结账时会出现无法提交情况,并报错Internal Error. Details are available in Magento log file
下一篇:Magento2修复Page Builder不存在的问题
相关内容
产品推荐