TransWikia.com

Remove product attribute automatically

Magento Asked on October 1, 2021

hi i have a custom module where am adding custom attribute using the code below.

 <?php
    namespace VendorModuleSetup;
    use MagentoEavSetupEavSetup;
    use MagentoEavSetupEavSetupFactory;
    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    
    /**
     * @codeCoverageIgnore
     */
    class InstallData implements InstallDataInterface
    {
        /**
         * EAV setup factory
         *
         * @var EavSetupFactory
         */
        private $eavSetupFactory;
    
        /**
         * Init
         *
         * @param EavSetupFactory $eavSetupFactory
         */
        public function __construct(EavSetupFactory $eavSetupFactory)
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }
    
        /**
         * {@inheritdoc}
         * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
         */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    
            /**
             * Add attributes to the eav/attribute
             */
    
            $eavSetup->addAttribute(
                    MagentoCatalogModelProduct::ENTITY,
                    'no_free_shipping',
                    [
                        'group' => 'General',
                        'type' => 'int',
                        'backend' => '',
                        'frontend' => '',
                        'label' => 'no_free_shipping',
                        'input' => 'boolean',
                        'class' => '',
                        'source' => MagentoEavModelEntityAttributeSourceBoolean::class,
                        'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                        'visible' => true,
                        'required' => false,
                        'user_defined' => false,
                        'default' => '1',
                        'searchable' => false,
                        'filterable' => false,
                        'comparable' => false,
                        'visible_on_front' => false,
                        'used_in_product_listing' => false,
                        'unique' => false,
                        'apply_to' => 'simple,configurable,bundle'
                    ]
                );
    
                $eavSetup->addAttribute(
                    MagentoCatalogModelProduct::ENTITY,
                    'no_flat_rate',
                    [
                        'group' => 'General',
                        'type' => 'int',
                        'backend' => '',
                        'frontend' => '',
                        'label' => 'no_flat_rate',
                        'input' => 'boolean',
                        'class' => '',
                        'source' => MagentoEavModelEntityAttributeSourceBoolean::class,
                        'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                        'visible' => true,
                        'required' => false,
                        'user_defined' => false,
                        'default' => '1',
                        'searchable' => false,
                        'filterable' => false,
                        'comparable' => false,
                        'visible_on_front' => false,
                        'used_in_product_listing' => false,
                        'unique' => false,
                        'apply_to' => 'simple,configurable,bundle'
                    ]
                );
    
    
                $eavSetup->addAttribute(
                    MagentoCatalogModelProduct::ENTITY,
                    'no_table_rate',
                    [
                        'group' => 'General',
                        'type' => 'int',
                        'backend' => '',
                        'frontend' => '',
                        'label' => 'no_table_rate',
                        'input' => 'boolean',
                        'class' => '',
                        'source' => MagentoEavModelEntityAttributeSourceBoolean::class,
                        'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                        'visible' => true,
                        'required' => false,
                        'user_defined' => false,
                        'default' => '1',
                        'searchable' => false,
                        'filterable' => false,
                        'comparable' => false,
                        'visible_on_front' => false,
                        'used_in_product_listing' => false,
                        'unique' => false,
                        'apply_to' => 'simple,configurable,bundle'
                    ]
                );
            }
        }

what i want is when user removes the module or uninstalled it then these attributes should automatically removed from the catalog under add product section.
i know about using unistall.php but it only works with the module installed using composer. so am looking for a solution other than this.. thanks

2 Answers

Disable Or delete your custom module, than go to Databse and search attribute code in last entries in these tables and delete them.

  1. eav_attribute

  2. catalog_eav_attribute (remember attribute id form 'eav_attribute' table) enter image description here

enter image description here

Answered by Sanaullah Ahmad on October 1, 2021

No other way available to remove that attribute while removing /uninstalled the extension except bin/magento module:uninstall

Answered by Amit Bera on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP