Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_virtual_rollback.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
10 use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory;
15 
16 $objectManager = Bootstrap::getObjectManager();
18 $productRepository = $objectManager->create(ProductRepositoryInterface::class);
20 $registry = $objectManager->get(Registry::class);
22 $stockStatusRepository = $objectManager->create(StockStatusRepositoryInterface::class);
24 $stockStatusCriteriaFactory = $objectManager->create(StockStatusCriteriaInterfaceFactory::class);
25 
27 $searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
29  ProductInterface::SKU,
30  ['VIRT-1', 'VIRT-2', 'VIRT-3', 'VIRT-4'],
31  'in'
32 )->create();
34 
39 if (!empty($products)) {
40  $currentArea = $registry->registry('isSecureArea');
41  $registry->unregister('isSecureArea');
42  $registry->register('isSecureArea', true);
43 
44  foreach ($products as $product) {
45  $criteria = $stockStatusCriteriaFactory->create();
46  $criteria->setProductsFilter($product->getId());
47 
48  $result = $stockStatusRepository->getList($criteria);
49  if ($result->getTotalCount()) {
50  $stockStatus = current($result->getItems());
51  $stockStatusRepository->delete($stockStatus);
52  }
53 
55  }
56 
57  $registry->unregister('isSecureArea');
58  $registry->register('isSecureArea', $currentArea);
59 }