-
Notifications
You must be signed in to change notification settings - Fork 6
/
deleteCategory.php
53 lines (44 loc) · 1.7 KB
/
deleteCategory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require dirname(__FILE__) . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
class Outslide extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$ids = [10, 15, 20];
$categoryFactory = $this->_objectManager->get('Magento\Catalog\Model\CategoryFactory');
$_MyClass = $this->_objectManager->create('DeleteCategory');
$_MyClass->deleteCategories($ids);
return $this->_response;
}
}
class DeleteCategory{
protected $_objectManager;
protected $_registry;
public function __construct(
\Magento\Framework\Registry $registry
)
{
$this->_registry = $registry;
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
}
public function deleteCategories($ids = array()) {
$categoryFactory = $this->_objectManager->get('Magento\Catalog\Model\CategoryFactory');
$categories = $categoryFactory->create();
$collection = $categories->getCollection()->addAttributeToSelect('name');
$this->_registry->register("isSecureArea", true);
foreach($collection as $category) {
if(is_array($ids) && in_array($category->getId(), $ids))
{
$category->delete();
echo 'category ' . $category->getName() . ' deleted';
} else if($category->getId() == $ids) {
$category->delete();
echo 'category ' . $category->getName() . ' deleted';
}
}
}
}
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Outslide');
$bootstrap->run($app);