Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
attribute_with_option_rollback.php
Go to the documentation of this file.
1 <?php
7 /* Create attribute */
10  \Magento\Catalog\Setup\CategorySetup::class
11 );
14  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
15 );
16 $attribute->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'attribute_with_option');
17 
18 /* Delete simple products per each option */
21  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
22 );
24 
25 $registry->unregister('isSecureArea');
26 $registry->register('isSecureArea', true);
27 $options->setAttributeFilter($attribute->getId());
28 
29 foreach ($options as $option) {
32  \Magento\Catalog\Model\Product::class
33  );
34  $product = $product->loadByAttribute('sku', 'simple_product_' . $option->getId());
35  if ($product->getId()) {
36  $product->delete();
37  }
38 }
39 
40 if ($attribute->getId()) {
41  $attribute->delete();
42 }
43 
44 $registry->unregister('isSecureArea');
45 $registry->register('isSecureArea', false);