1

I created 4 custom attributes for category. Now I want to remove it. I want to use object manager for delete attributes and its values.

See this image. Created attributes are display in this manner. enter image description here

Now I created one file delete_attribute.php in magento root folder.

<?php
ini_set('display_errors', 1);
//ini_set('memory_limit','-1');
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();  
$state = $objectManager->get('Magento\Framework\App\State');
//$state->setAreaCode('frontend');
$state->setAreaCode('adminhtml');

// 165
// attribute_code1
// \Magento\Catalog\Model\Category

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('attribute_code1'); //Get Current Category
$PCategory = $objectManager->get('Magento\Catalog\Model\CategoryFactory')->create()->load(165);

echo $PCategory->getIsCollectionPageListing();
print_r($PCategory);
?>

Does anyone know how to delete these 4 attributes using object manager ?

Thanks in advance.

1 Answer 1

0

Create ../Setup/UpgradeData.php file:

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
   private $eavSetupFactory;

   /**
    * UpgradeData constructor.
    * @param EavSetupFactory $eavSetupFactory
    */
   public function __construct(EavSetupFactory $eavSetupFactory)
   {
       $this->eavSetupFactory = $eavSetupFactory;
   }

   /**
    * @param ModuleDataSetupInterface $setup
    * @param ModuleContextInterface $context
    */
   public function upgrade(
       ModuleDataSetupInterface $setup,
       ModuleContextInterface $context
   ) {
       $setup->startSetup();

       if (version_compare($context->getVersion(), '1.0.1') < 0) {  // 1.0.1 is your new version in module.xml
           $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

           $eavSetup->removeAttribute(
               \Magento\Customer\Model\Customer::ENTITY,
               'sample_attribute'
           );
       }

       $setup->endSetup();
   }
}
1
  • I want it using object manager. Oct 24, 2019 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.