Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
categories_rollback.php
Go to the documentation of this file.
1 <?php
9 $registry = $objectManager->get(\Magento\Framework\Registry::class);
10 
11 $registry->unregister('isSecureArea');
12 $registry->register('isSecureArea', true);
13 
14 // Remove products
16 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
17 $productsToDelete = ['simple', '12345', 'simple-3', 'simple-4'];
18 
19 foreach ($productsToDelete as $sku) {
20  try {
21  $product = $productRepository->get($sku, false, null, true);
23  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
24  //Product already removed
25  }
26 }
27 
28 //Remove categories
30 $collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
31 foreach ($collection->addAttributeToFilter('level', ['in' => [2, 3, 4]]) as $category) {
33  $category->delete();
34 }
35 
36 $registry->unregister('isSecureArea');
37 $registry->register('isSecureArea', false);
$objectManager
$productRepository
$productsToDelete